当有人点击了RadioButton
中的RadioButton
时,我想停止对RadioGroup
的第二次点击。如果有人单击2次,则用户无法更改所选的单击。我试图用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");
}
});
}
});
答案 0 :(得分:2)
请记住他们是否回答了问题,并记住产生第一个答案的视图(RadioButton),并在点击RadioButton时再次检查RadioButton。这假设RadioGroup中的所有RadioButtons都具有相同的onClick
方法。
boolean answered = false;
RadioButton radioAnswer;
public void answer1(View view) {
if (!answered) {
radioAnswer = (RadioButton) view;
answered = true;
} else {
radioAnswer.setChecked(true);
}
}
答案 1 :(得分:0)
您可以在点击停止收听点击事件后将其停用
rb.setEnabled(false);
编辑:
或者,您可以在点击后点击
删除点击监听器rb.setOnClickListener(null);
答案 2 :(得分:0)
您可以添加两种方法,然后在需要时调用它们。您可以添加条件,如果选中了radiobutton,则禁用该组,如果未选中,则将其设置为启用。清除无线电组时,也可以设置按钮启用。
public void disableRG() {
for(int i = 0; i < radioGroup.getChildCount(); i++){
((RadioButton)radioGroup.getChildAt(i)).setEnabled(false);
}
}
public void enableRG() {
for(int i = 0; i < radioGroup.getChildCount(); i++){
((RadioButton)radioGroup.getChildAt(i)).setEnabled(true);
}
}
答案 3 :(得分:0)
也许你可以设置一个全局布尔标志。
`private boolean alreadyChecked = false;
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();
if (!alreadyChecked) {
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");
}
alreadyChecked = true;
});
}
}
});`