如何取消选中组中的单选按钮?
我正在做一个测验应用程序我有一个收音机组与4个单选按钮,我有一个下一个按钮。如何取消选中组中的单选按钮?
在显示时第一个问题并没有问题,当用户给下一个时,收音机组应该取消选中。如何实现这一点?
当用户选择它应该验证答案是正确的或不正确的选项。我已经将正确答案存储在一个数组中。当用户切换选项时,它会在切换按钮中显示错误。如何实现这一点?
btn_practicerg.setOnCheckedChangeListener(new OnCheckedChangeListener(){ @Override public void onCheckedChanged(RadioGroup group, int checkedId) { RadioButton radioButton = (RadioButton)group. findViewById(checkedId); String temp = radioButton.getText().toString(); switch(btn_practicerg.getCheckedRadioButtonId()){ case R.id.RB1: if (btn_practice1.isChecked()){ btn_practice2.setChecked(false); btn_practice3.setChecked(false); btn_practice4.setChecked(false); } break; case R.id.RB2: if (btn_practice2.isChecked()){ btn_practice1.setChecked(false); btn_practice3.setChecked(false); btn_practice4.setChecked(false); } break; case R.id.RB3: if (btn_practice3.isChecked()){ btn_practice1.setChecked(false); btn_practice2.setChecked(false); btn_practice4.setChecked(false); } break; case R.id.RB4: if (btn_practice4.isChecked()){ btn_practice1.setChecked(false); btn_practice2.setChecked(false); btn_practice3.setChecked(false); } break; } crrtans=new ArrayList<String>(new ArrayList<String>(crrtans)); if (temp.equals(crrtans.get(l))){ TextView txtRadio = (TextView) findViewById(R.id.rdtxt); txtRadio.setText("" + radioButton.getText() + " IS CORRECT"); txtRadio.setTextColor(Color.parseColor("#008000")); }else{ TextView txtRadio = (TextView) findViewById(R.id.rdtxt); txtRadio.setText("" + radioButton.getText() + "is INCORRECT"); txtRadio.setTextColor(Color.parseColor("#FF0000")); } } }); Button nextBtn = (Button) findViewById(R.id.nxt_btn); nextBtn.setOnClickListener(new Button.OnClickListener(){ public void onClick(View v){ if (j == ques1.size() -1) { showAlert1(); } else{ ++j; ++num; TextView txtque = (TextView) findViewById(R.id.que_txt); txtque.setText("Q" + num +ques1.get(j)); } });
要清除所有单选按钮,也就是没有一个组中的单选按钮的要被检查使用
clearCheck()
例如乌尔
radiogroupname.clearCheck();
它显示错误.. E/AndroidRuntime(712):\t在com.example.finalpractice.Question $ LoadQuestions $ 1.onCheckedChanged(Question.java:236) E/AndroidRuntime(712 ):\t在android.widget.RadioGroup.setCheckedId(RadioGroup.java:172) E/AndroidRuntime(712):\t在android.widget.RadioGroup.check(RadioGroup.java:166) E/AndroidRuntime(712): \t at android.widget.RadioGroup.clearCheck(RadioGroup.java:205) – 2013-02-13 07:46:43
问题在您的public void onCheckedChanged(RadioGroup group, int checkedId)
里面,您正在创建一个已选中的收音机实例n基于checkedId,一旦你致电clearCheck
,它可以为null。实际情况是,当您单击下一个按钮时,您在收音机组上拨打clearCheck
,将触发onCheckedChanged
事件并收到空checkedId。因此,您得到一个空单选按钮,当你这样做:RadioButton radioButton = (RadioButton)group. findViewById(checkedId);
EDIT(更好的解释):
加入此方法类:
private void doProcessingWithRadioButton(int checkedId)
{
RadioButton radioButton = (RadioButton)group. findViewById(checkedId);
String temp = radioButton.getText().toString();
switch(btn_practicerg.getCheckedRadioButtonId()){
case R.id.RB1:
if (btn_practice1.isChecked()){
btn_practice2.setChecked(false);
btn_practice3.setChecked(false);
btn_practice4.setChecked(false);
}
break;
case R.id.RB2:
if (btn_practice2.isChecked()){
btn_practice1.setChecked(false);
btn_practice3.setChecked(false);
btn_practice4.setChecked(false);
}
break;
case R.id.RB3:
if (btn_practice3.isChecked()){
btn_practice1.setChecked(false);
btn_practice2.setChecked(false);
btn_practice4.setChecked(false);
}
break;
case R.id.RB4:
if (btn_practice4.isChecked()){
btn_practice1.setChecked(false);
btn_practice2.setChecked(false);
btn_practice3.setChecked(false);
}
break;
}
crrtans=new ArrayList<String>(new ArrayList<String>(crrtans));
if (temp.equals(crrtans.get(l))){
TextView txtRadio = (TextView) findViewById(R.id.rdtxt);
txtRadio.setText("" + radioButton.getText() + " IS CORRECT");
txtRadio.setTextColor(Color.parseColor("#008000"));
}else{
TextView txtRadio = (TextView) findViewById(R.id.rdtxt);
txtRadio.setText("" + radioButton.getText() + "is INCORRECT");
txtRadio.setTextColor(Color.parseColor("#FF0000"));
}
}
现在改变你的代码像这个:
btn_practicerg.setOnCheckedChangeListener(new OnCheckedChangeListener(){
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if(checkedId != -1)
{
doProcessingWithRadioButton(checkedId);
}
});
希望这对我有所帮助。如果它解决了您的问题,请不要忘记标记为答案。
什么?真的,我不能理解你的代码...请以另一种方式引导我..我想取消选中单选按钮,而用户单击下一步按钮... – 2013-02-13 08:26:34
看看你的代码,'onCheckedChanged'监听器里面你正在创建一个RadioButton已经通过'checkedId'检查过了所以如果你的checkedId为null,RadioButton为空,所以后面的代码会产生NullException。所以在做所有这些检查checkedId是否不为null。为了更好的可读性,我已经将所有代码封装在onCheckedChange中并添加了此验证。 – 2013-02-13 08:31:50
好吧,让我为你添加整个代码。 – 2013-02-13 08:32:38
检查我的答案.... – 2013-02-13 08:10:30