所以現在我正在整理一些代碼,並且我還有很多其他/ 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