0
有些專家告訴我爲什麼這段代碼不起作用,目的很簡單,就是在一段時間內改變按鈕的顏色,在那段時間之後,你必須着色返回到原來的顏色。問題是沒有按順序運行,應該是一個按鈕來改變顏色,等待1秒,回到原來的顏色,然後相同的下一個按鈕來完成序列。Android - 改變按鈕的顏色一段時間
對不起我英語不好,並提前致謝。
int temp[] = new int[game.getLevel()];
Handler handler = new Handler();
temp = game.getSequence();
for(int i = 0; i < game.getLevel(); i++)
{
switch (temp[i])
{
case RED:
handler.postDelayed(new Runnable() {
public void run() {
redButton.setBackgroundColor(Color.rgb(255, 0, 0));
}
}, 1000);
redButton.setBackgroundColor(Color.rgb(109, 0, 0));
break;
case GREEN:
handler.postDelayed(new Runnable() {
public void run() {
greenButton.setBackgroundColor(Color.rgb(0, 255, 0));
}
}, 1000);
greenButton.setBackgroundColor(Color.rgb(0, 109, 0));
break;
case YELLOW:
handler.postDelayed(new Runnable() {
public void run() {
yellowButton.setBackgroundColor(Color.rgb(255, 255, 0));
}
}, 1000);
yellowButton.setBackgroundColor(Color.rgb(109, 109, 0));
break;
case BLUE:
handler.postDelayed(new Runnable() {
public void run() {
blueButton.setBackgroundColor(Color.rgb(0, 0, 255));
}
}, 1000);
blueButton.setBackgroundColor(Color.rgb(0, 0, 255));
break;
default:
break;
}
}
}