2015-11-03 19 views
1

我試圖設置文本顏色爲紅色,以所有單選按鈕,但是從我有所有的無線電組。什麼時候使用answer[i].setTextColor(Color.RED);這隻適用於最後一個無線電組。如何文本顏色設置爲從所有無線電組中的所有單選按鈕在Java中

如果嘗試將背景顏色更改爲與radioGroup[i].getChildAt(j).setBackgroundColor(Color.GREEN);的所有單選按鈕,則會出現以下錯誤:Attempt to invoke virtual method 'android.view.View android.widget.RadioGroup.getChildAt(int)' on a null object reference

我該如何解決這個問題?有沒有其他方法可以做到這一點?
這裏是我的代碼:

radioGroup = new RadioGroup[4]; 
    answer = new RadioButton[4]; 
    for (Question qn : questions) { 
     int i = 0; 
     radioGroup[i] = new RadioGroup(this); 
     radioGroup[i].setOrientation(RadioGroup.VERTICAL); 
     int j = 0; 
     for (Answer an : answers) { 
      if (qn.getID() == an.getQuestion_id_answer()) { 
       String answers_log = " " + an.getAnswer(); 
       answer[j] = new RadioButton(this); 
       answer[j].setText(answers_log); 
       radioGroup[i].addView(answer[j]); 
       j++; 
      } 
     } 
     linearLayout.addView(radioGroup[i]); 
     i++; 
    } 

    finishButton = new Button(this); 
    linearLayout.addView(finishButton); 

    finishButton.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      for (int i = 0; i < radioGroup.length; i++) { 
       for (int j = 0; j < answer.length; j++) { 
        answer[i].setTextColor(Color.RED); 
       } 
      } 
     } 
    }); 

謝謝!

+0

也許它更容易使用的風格,反正我不能看到你調用getChildAt() – Nanoc

+0

感謝行,但我需要它動態。 –

+0

沒有關於setTextColor的代碼? –

回答

1

UPDATE

試試這個代碼:

radioGroup = new RadioGroup[4]; 
    answer = new RadioButton[4]; 
    int i = 0; 
    for (Question qn : questions) { 
     radioGroup[i] = new RadioGroup(this); 
     radioGroup[i].setOrientation(RadioGroup.VERTICAL); 
     int j = 0; 
     for (Answer an : answers) { 
      if (qn.getID() == an.getQuestion_id_answer()) { 
       String answers_log = " " + an.getAnswer(); 
       answer[j] = new RadioButton(this); 
       answer[j].setText(answers_log); 
       radioGroup[i].addView(answer[j]); 
       j++; 
      } 
     } 
     linearLayout.addView(radioGroup[i]); 
     i++; 
    } 

    finishButton = new Button(this); 
    linearLayout.addView(finishButton); 

    finishButton.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      for (int i = 0; i < radioGroup.length; i++) { 
       for (int j = 0; j < radioGroup[i].getChildCount(); j++) { 
        radioGroup[i].getChildAt(j).setBackgroundColor(Color.GREEN); 
       } 
      } 
     } 
    }); 
+0

如果我使用'radioGroup [i] 。長度;'我得到這個錯誤:'無法解析符號「lenght'' –

+1

膏'radioGroup中[I] .getChildCount()''代替radioGroup中[I] .length',並移動'INT I = 0;'到前'爲(問題qn:問題)'就像在我更新的帖子裏。 – walkmn

+0

它工作的人,非常感謝你!這個問題讓我煩惱了差不多3天。 –

0

試試這個:

finishButton.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View v) { 
     for (int i = 0; i < 4; i++) { 
      radioGroup[i].clearCheck(); 
      for (int j = 0; j < 4; j++) { 
       radioGroup[i].getChildAt(j).setEnabled(true); 
       ((RadioButton)radioGroup[i].getChildAt(j)).setTextColor(Color.RED); 
       ((RadioButton)radioGroup[i].getChildAt(j)).setChecked(false); 
      } 
     } 
    } 
}); 
+0

同樣的錯誤:'嘗試調用虛擬方法'void android.widget.RadioGroup.clearCheck()'null對象引用' –

-1

我很神奇!

ü只想得到4 RadioGroup和4 RadioButton

變化for(b: bs)for(int i = 0;i<bs.size();i++)

finishButton.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
     try{ 
      for (int i = 0; i < radioGroup.length; i++) { 
       int len = radioGroup[i].getChildCount(); 
       for (int j = 0; j < len; j++) { 
        radioGroup[i].getChildAt(j).setBackgroundColor(
          Color.GREEN); 
       } 
      } 
     }catch(Throwable e){ 
      } 

     } 
    }); 
+0

同樣的錯誤:'嘗試調用虛擬方法'int android.widget.RadioGroup.getChildCount()'null對象引用' –

+0

使用answer [i] .setTextColor(Color.RED)!而擷取畫面PLZ –

+0

我想知道的問題的個數和答案 –

相關問題