java - 如何在 Java 中为所有单选组中的所有单选按钮设置文本颜色

标签 java android loops for-loop onclicklistener

我正在尝试将所有单选按钮的文本颜色设置为红色,但来 self 拥有的所有单选组。当使用answer[i].setTextColor(Color.RED);时,这仅适用于最后一个单选组。

如果尝试使用 radioGroup[i].getChildAt(j).setBackgroundColor(Color.GREEN); 更改所有单选按钮的背景颜色,我收到以下错误:尝试在空对象引用上调用虚拟方法“android.view.View android.widget.RadioGroup.getChildAt(int)”

我该如何解决这个问题?还有其他方法可以做到这一点吗?
这是我的代码:

    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);
                }
            }
        }
    });

谢谢!

最佳答案

更新

试试这个代码:

    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);
                }
            }
        }
    });

关于java - 如何在 Java 中为所有单选组中的所有单选按钮设置文本颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33499149/

相关文章:

android - Kotlin 惰性属性和值重置 : a resettable lazy delegate

javascript - 如何使 javascript 显示随机名称而不重复?

android - 使用带有 ADB 的三星 galaxy s7 导航按钮

r - 如何用循环绑定(bind)多个数据帧?

loops - lua:遍历表中的所有对

java - Spring 配置文件配置

java - Jersey 2枚举作为方法参数抛出异常

java - Spring MVC : RequestMapping both class and method

java - 数组列表中不安全或未经检查的表达式

java - 使用 gson.fromJson 时如何修复 NumberFormatException