我試圖設置文本顏色爲紅色,以所有單選按鈕,但是從我有所有的無線電組。什麼時候使用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);
}
}
}
});
謝謝!
也許它更容易使用的風格,反正我不能看到你調用getChildAt() – Nanoc
感謝行,但我需要它動態。 –
沒有關於setTextColor的代碼? –