如何取消选中或重置单选按钮? [英] How can I uncheck or reset the radio button?
问题描述
我正在开发基于测验的应用程序.当用户打开此应用程序单选按钮时,将有1个问题和4个选项(单选按钮)被取消选中,但是当用户回答1个任务并且他去进行下一个任务单选按钮时,将出现问题.我想取消/重置每个问题的单选按钮.我该怎么办?
I am developing an quiz based app. There will be 1 question and 4 option (radio buttons) when user opens this app radio button will be unchecked but the problem comes when the user answers 1 quest and when he goes for next quest radio button will be checked. I want to uncheck/reset the radio buttons for every question. How can I do it?
answ1=new ArrayList<String>(new ArrayList<String>(answ1));
btn_practice1.setText(answ1.get(0));
btn_practice2.setText(answ1.get(1));
btn_practice3.setText(answ1.get(2));
btn_practice4.setText(answ1.get(3));
btn_practicerg.setOnCheckedChangeListener(new OnCheckedChangeListener(){
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
RadioButton radioButton = (RadioButton)group. findViewById(checkedId); //line 262
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;
default:
btn_practice1.setChecked(false);
btn_practice2.setChecked(false);
btn_practice3.setChecked(false);
btn_practice4.setChecked(false);
}
ImageView nextBtn = (ImageView) findViewById(R.id.nxt_btn);
nextBtn.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v){
btn_practicerg.clearCheck(); //line 355
}
});
Logcat
E/AndroidRuntime(729):at
com.example.TEENEINSTIEN.Question$LoadQuestions$2.onCheckedChanged(Question.java:262)
E/AndroidRuntime(729):at android.widget.RadioGroup.setCheckedId(RadioGroup.java:172)
E/AndroidRuntime(729):at android.widget.RadioGroup.check(RadioGroup.java:166)
E/AndroidRuntime(729):at android.widget.RadioGroup.clearCheck(RadioGroup.java:205)
E/AndroidRuntime(729):at
com.example.TEENEINSTIEN.Question$LoadQuestions$3.onClick(Question.java:355)
E/AndroidRuntime(729):at android.view.View.performClick(View.java:2485)
E/AndroidRuntime(729):at android.view.View$PerformClick.run(View.java:9080)
我尝试这样,但是我不能取消选中..
I try like this, but I can't uncheck..
推荐答案
我认为问题是使用btn_practicerg 对象,即如果您每次显示新问题时都在创建新的RadioGroup对象,则必须在新的 btn_practicerg 对象上执行 RadioGroup.clearCheck(),而不是我认为您当前正在执行的旧对象.
I think the problem is with btn_practicerg object i.e if you are creating new RadioGroup object everytime you display a new question then you have to execute RadioGroup.clearCheck() on new btn_practicerg object instead of old one that I think you are doing currently.
更好:
btn_practice1.setText(answ1.get(0));
btn_practice2.setText(answ1.get(1));
btn_practice3.setText(answ1.get(2));
btn_practice4.setText(answ1.get(3));
btn_practice1.setChecked(false);
btn_practice2.setChecked(false);
btn_practice3.setChecked(false);
btn_practice4.setChecked(false);
取消选中开头的所有按钮.我希望这能解决您的问题.
to uncheck all the buttons in the beginning. I hope this will solve your problem.
这篇关于如何取消选中或重置单选按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!