2015-01-15 55 views
0

我有點新的Android和已使用該代碼來更改Button的背景顏色點擊時:設置按鈕的背景顏色瞬間 - 安卓

but3.setBackgroundColor(Color.GREEN); 

但它保持這種方式不點擊後返回原來的顏色。我希望它改回來。請幫忙。這是更多的代碼。

but3.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v){ Button answerButton = ((Button) v); String answer = answerButton.getText().toString(); if(currentQ.getANSWER().equals(answerButton.getText())) { score++; Log.d("score", "Your score"+score); but3.setBackgroundColor(Color.GREEN); but3.invalidate(); } if(qid<20){ currentQ=quesList1.get(qid); setQuestionView(); } else{
Intent intent = new Intent(ScratchActivity1.this, ResultActivity.class); Bundle b = new Bundle(); b.putInt("score", score); intent.putExtras(b); startActivity(intent); overridePendingTransition(R.anim.activity_in, R.anim.activity_out); finish(); }

+0

這取決於你想將它改回來這種情況下。發佈更多細節。 – localhost

+0

上面有更多細節。我想要正確答案的按鈕(這是一個測驗應用程序)即刻更改爲特定顏色以通知用戶。同樣的錯誤答案。 –

回答

0

如果您想在按下按鈕或重點檢查here就看你如何創建每個州的自定義的背景(當按下一個按鈕,突出重點,選擇等狀態)改變背景。

如果你只是想改變一些時間後畫面的背景爲使用處理程序:

final Handler handler = new Handler(); 
handler.postDelayed(new Runnable() { 
    @Override 
    public void run() { 
    //This will run after 1000 (defined below) milliseconds has passed 
    } 
}, 1000); 
+0

處理程序在嘗試解決問題時非常有用。我使用下面的代碼來更改按鈕背景:'but3.setBackgroundColor(Color.GREEN);'然後在處理程序的run()方法中將背景設置爲按鈕的原始顏色:例如。 'but3.setBackgroundColor(Color.BLACK);'我得到的問題是原始按鈕背景必須設置爲黑色才能正常工作。其他顏色給出了奇怪的結果,也許有人有這個問題的答案。 –