所以现在我正在整理一些代码,并且我还有很多其他/ ifs按钮,并想知道什么是一种很好的方法来做到这一点,并使其更整洁?Android更好的方法来做按钮点击?
所以我喜欢12个按钮,每个按钮播放声音并在点击时改变颜色。我有一个方法,但我想知道是否有一个好方法来检测按钮,而不是if/else?
public void onClick(View v) {
int id = v.getId();
changeToWhite();
if (id == R.id.a_button) {
currentButton(a, 81);
} else if (id == R.id.aSharp_button) {
currentButton(aSharp, 82);
} else if (id == R.id.b_button) {
currentButton(b, 83);
} else if (id == R.id.c_button) {
currentButton(c, 72);
}
等...
那么,有没有有这个更好的办法?我知道有很多其他/ ifs是坏的,所以我想尝试改进它。 谢谢!
对于初学者,您可以使用'switch'语句。 – 323go 2013-03-24 02:39:51