2017-02-26 20 views
-1

我有一個TextView和一個帶有四個單選按鈕的無線電組ListView。當用戶選擇單選按鈕時,如果答案正確並且答案錯誤,文本顏色會變爲綠色,但文本顏色會變爲紅色,這可以很好地工作,但如果答案錯誤,我想要它也會將正確答案文本更改爲綠色。如何將正確的答案文本更改爲綠色,同時將錯誤的答案文本顏色更改爲紅色

if (opt1.getText.toString.equals(correctans)) { 
       opt1.setTextColor(getResources().getColor(R.color.correct_ans)); 
       iv_optA.setImageResource(R.drawable.correctans); 
       iv_optB.setImageResource(R.drawable.dontknowans); 
       iv_optC.setImageResource(R.drawable.dontknowans); 
       iv_optD.setImageResource(R.drawable.dontknowans); 


      } else { 
       opt1.setTextColor(getResources().getColor(R.color.wrong_ans)); 
       iv_optA.setImageResource(R.drawable.wrongans); 
       iv_optB.setImageResource(R.drawable.dontknowans); 
       iv_optC.setImageResource(R.drawable.dontknowans); 
       iv_optD.setImageResource(R.drawable.dontknowans); 
      } 
+0

你有什麼麻煩?你知道哪個視圖包含正確的答案嗎? –

+0

'opt1'和'opt_1'有什麼區別?這個變量名稱非常相似,很容易混淆它們。我建議你將它們改爲更有意義,更容易區分的東西。 –

+0

我有2個文本視圖和4個單選按鈕。問題textview和答案文本視圖。我的答案存儲在答案文本視圖中。 –

回答

0

我自己解決了。

String opt_1 = opt1.getText().toString(); 
    if (opt_1.equals(correctans)) { 
       opt1.setTextColor(getResources().getColor(R.color.correct_ans)); 
       iv_optA.setImageResource(R.drawable.correctans); 
      } else { 
       opt1.setTextColor(getResources().getColor(R.color.wrong_ans)); 
       iv_optA.setImageResource(R.drawable.wrongans); 
       if (opt_1.equals(correctans)) 
        opt1.setTextColor(Color.BLUE); 
       if (opt_2.equals(correctans)) 
        opt2.setTextColor(Color.BLUE); 
       if (opt_3.equals(correctans)) 
        opt3.setTextColor(Color.BLUE); 
       if (opt_4.equals(correctans)) 
        opt4.setTextColor(Color.BLUE); 
      } 
相關問題