java - 在 Android 中按下按钮时如何设置单选按钮的文本颜色?

标签 java android loops if-statement onclicklistener

我有 5 个单选组,每个组有 4 个单选按钮。每个单选按钮代表一个问题的答案。我想当有人按下按钮时,如果选中正确答案,将该文本的颜色设置为绿色。使用此代码,当我按下按钮时,什么也没有发生。有任何想法吗? 这是我的代码:

boolean isChecked;
int CorrectAnswer;
RadioButton checkedRadioButton;

    answer[j].setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        RadioButton checkedRadioButton = ((RadioButton) v);
        int CorrectAnswer = Integer.parseInt(checkedRadioButton.getTag().toString());
        isChecked = true;
        if (isChecked) {
            if (checkedRadioButton.isChecked() & CorrectAnswer == 1) {
                score++;
                isChecked = false;
            }
        }
    }

    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++) {
                radioGroup[i].getChildAt(j).setEnabled(false);
                    if (CorrectAnswer == 1) {
                        checkedRadioButton.setTextColor(Color.GREEN);
                    }
                }
            }
        }
    });

谢谢!

最佳答案

与选定的单选按钮交互的一种更简单的方法是不使用 V,而是直接从 RadioGroup 中获取 ID。你会做这样的事情:

int selectedId = yourRadioGroup.getCheckedRadioButtonId();
selected = (RadioButton) findViewById(selectedId);
selected.setTextColor(Color.Green);

一旦您选择了上面 selected 所引用的单选按钮,您甚至可以从其他听众那里做任何您想做的事情。另外,您唯一要更改文本颜色的地方是在 finishButton 监听器中。单击右侧的 RadioButton 时是否要更改颜色?您正在增加其他听众的分数等,所以为什么不在那里而不是在 finishButton() 中设置颜色;现在您仍然可以在那里设置它,只需确保您正确选择了单选按钮。

编辑

好吧,这是我所指的一个非常简单的例子。请注意,我创建了一个临时全局变量来保存所选设备。现在这不是嵌套的 for 循环,但原理是 100% 相同的。

public class MainActivity extends AppCompatActivity {
Button btn, btn2;
RadioGroup radioButtonGroup;
RadioButton selected;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    btn = (Button) findViewById(R.id.button);
    btn2 = (Button) findViewById(R.id.button2);
    radioButtonGroup = (RadioGroup) findViewById(R.id.rad);
    boolean isValid = false;
    btn.setOnClickListener(new View.OnClickListener() {
        RadioButton rad;
        @Override
        public void onClick(View v) {
            if(selected != null) {
                selected.setTextColor(Color.BLACK);

            }
            int selectedId = radioButtonGroup.getCheckedRadioButtonId();
            selected = (RadioButton) findViewById(selectedId);
            selected.setTextColor(Color.GREEN);
        }
    });

    btn2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(selected != null) {
                selected.setTextColor(Color.BLACK);

            }
            int selectedId = radioButtonGroup.getCheckedRadioButtonId();
            selected = (RadioButton)findViewById(selectedId);
            selected.setTextColor(Color.RED);
        }
    });
  }
}

这是它的样子

enter image description here

点击两次

enter image description here

关于java - 在 Android 中按下按钮时如何设置单选按钮的文本颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33617580/

相关文章:

java - 在 Eclipse 中设置系统属性

java - 跨域javascript函数调用?

java - 我的 onResume 取决于 `onViewCreated` 。我怎样才能重新设计它?

python - 虽然真正的循环没有结束

java - Eclipse 无法正确复制上下文

java - 组合 n 个列表列表保存顺序(Java)

Android Lollipop 在完成后调用 onCreate()

android - 以编程方式截取屏幕截图不会捕获 surfaceVIew 的内容

c# - 不知道为什么我的循环不循环

loops - 是否确保更新并行 do 循环中的变量?