我想要阻止在 RadioGroup
中已经选中了一个 RadioButton
的情况下进行第二次点击。如果有人点击了两次,那么用户将无法更改所选的点击。我试图通过使用一个 boolean
来防止这种情况发生,但它并没有起作用。我错在哪里了?
这是我的代码:
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// checkedId is the RadioButton selected
rb = (RadioButton) findViewById(checkedId);
user_radio_answer = (String) rb.getText();
rb.setOnClickListener(new View.OnClickListener() {
private boolean isChecked = true;
@Override
public void onClick(View v) {
if (isChecked) {
if (rb.isChecked() ) {
isChecked = false;
}
}
if (correct_answer1.equals(user_radio_answer)) {
rb.setBackgroundColor(Color.GREEN);
// Toast.makeText(getApplicationContext(), "Correct ☺ : " + user_radio_answer, Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(), "Incorrect Answer :( ..Correct Answer : " + correct_answer1, Toast.LENGTH_LONG).show();
rb.setBackgroundColor(Color.parseColor("#FF5733"));
answer1 = rb_answer1.getText().toString();
answer2 = rb_answer2.getText().toString();
answer3 = rb_answer3.getText().toString();
answer4 = rb_answer4.getText().toString();
if (answer1.equals(correct_answer1)) {
rb_answer1.setBackgroundColor(Color.GREEN);
}
if (answer2.equals(correct_answer1)) {
rb_answer2.setBackgroundColor(Color.GREEN);
}
if (answer3.equals(correct_answer1)) {
rb_answer3.setBackgroundColor(Color.GREEN);
}
if (answer4.equals(correct_answer1)) {
rb_answer4.setBackgroundColor(Color.GREEN);
}
}
new RadioHttpAsyncTask().execute("http:xxxxxx-xxxxxxxxxx-xxxxxxxx");
}
});
}
});
onCheckedChanged
中设置OnClickListener
。这意味着在任何选项更改后,您当前的“RadioButton”将清除先前在其isChecked
字段中保存的所有信息。尝试在onCheckedChanged
方法之外设置它。 - Ircover