有沒有辦法讓下面的腳本更高效? 我想讓這個代碼易於維護,這就是爲什麼我想擺脫if if else。 我希望你們能幫助我。在底部是我想看到的東西,如果它可能的話。爲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