다음 스크립트를보다 효율적으로 만들 수있는 방법이 있습니까? 이 코드를 쉽게 유지 보수 할 수있게 만들고 싶습니다. 왜 내가 if else if를 없애고 싶습니다. 여러분이 나를 도와 줄 수 있기를 바랍니다. 밑바닥에는 내가보고 싶은 무언가가 있습니다. 물론 그렇게 할 수 있다면.문자열을 R.color에 사용하십시오. "string"
if (category.equals("infusion")){
layout.setBackgroundResource(R.color.infusion);
title.setText(R.string.title_infusion);
} else if (category.equals("pills")){
layout.setBackgroundResource(R.color.pills);
title.setText(R.string.title_pills);
} else if (category.equals("pumps")){
layout.setBackgroundResource(R.color.pumps);
title.setText(R.string.title_pumps);
} else if (category.equals("oxygen")){
layout.setBackgroundResource(R.color.oxygen);
title.setText(R.string.title_oxygen);
} else if (category.equals("scores")){
layout.setBackgroundResource(R.color.scores);
title.setText(R.string.title_scores);
} else if (category.equals("converters")){
layout.setBackgroundResource(R.color.converters);
title.setText(R.string.title_converters);
}
이와 비슷한 기능이 있습니까?
layout.setBackgroundResource(R.color.*category*);
title.setText(R.string.title_*category*);
가 – JoeC