2015-10-20 75 views
0

我想將按鈕的文本顏色更改爲綠色2秒,然後將其重新轉回原始。我怎樣才能做到這一點? 我的方法是:Android更改按鈕的文本顏色2秒

private void changeColors() { 
    Button option1Button = (Button) findViewById(R.id.option1Button); 
    Button option2Button = (Button) findViewById(R.id.option2Button); 
    Button option3Button = (Button) findViewById(R.id.option3Button); 
    Button option4Button = (Button) findViewById(R.id.option4Button); 

    Button[] buttons = {option1Button, option2Button, option3Button, option4Button}; 

    buttons[correctAnswerButtonID].setTextColor(Color.GREEN); 
    SystemClock.sleep(2000); 
//later changing it back to original 

} 

這不起作用,文字變爲綠色睡眠方法之後。我希望它變成綠色,然後開始睡眠過程。 謝謝你的回答!

+0

你有什麼用呢?爲什麼你想改變文字顏色2秒? –

+0

這是一個問答遊戲。它生成一個圖像(作爲問題)和四個答案。我需要以下內容: 如果用戶單擊正確的答案按鈕,它將變爲綠色2秒 - 顯示它是正確的答案。 如果否,點擊的按鈕變爲紅色,並且正確的答案按鈕變成綠色。我需要2秒才能在遊戲中再次出現回合,所以玩家可以看到他的回答。 – user230682

回答

0

可以使用動畫

buttons[correctAnswerButtonID].setTextColor(Color.GREEN); 
buttons[correctAnswerButtonID].animate().setDuration(2000).withEndAction(new Runnable() { 
      @Override 
      public void run() { 
       // set color back to normal 
       buttons[correctAnswerButtonID].setTextColor(Color.BLACK); 
      } 
     }).start(); 
+0

這不是我想要的解決方案。程序在此之後執行方法,然後在2秒鐘後將顏色更改爲綠色。 – user230682

+0

你能更具體嗎?我的方法將顏色更改爲綠色,等待2秒,然後將其更改回黑色,就像您在問題中提問的那樣。 –

相關問題