Android 如何取消选中组中的单选按钮?

Android 如何取消选中组中的单选按钮?,android,radio-group,Android,Radio Group,我正在做一个测验应用程序,我有一个带有4个单选按钮的广播组,还有一个“下一步”按钮 当显示第一个问题和答案时,检查中没有问题。当用户给出下一个问题时,无线电组应取消选中。如何实现这一点 当用户选择该选项时,应验证答案是否正确。我已将正确答案存储在数组中。当用户切换选项时,切换按钮中会显示错误。如何实现这一点 btn_practicerg.setOnCheckedChangeListener(新的OnCheckedChangeListener(){ @凌驾 已更改checkedChanged(Ra

我正在做一个测验应用程序,我有一个带有4个单选按钮的广播组,还有一个“下一步”按钮

当显示第一个问题和答案时,检查中没有问题。当用户给出下一个问题时,无线电组应取消选中。如何实现这一点

当用户选择该选项时,应验证答案是否正确。我已将正确答案存储在数组中。当用户切换选项时,切换按钮中会显示错误。如何实现这一点

btn_practicerg.setOnCheckedChangeListener(新的OnCheckedChangeListener(){
@凌驾
已更改checkedChanged(RadioGroup组,int checkedId)时的公共void{
RadioButton RadioButton=(RadioButton)组。findViewById(checkedId);
字符串温度=radioButton.getText().toString();
开关(btn_practicerg.getCheckedRadioButtonId()){
案例R.id.RB1:
如果(btn_practice1.isChecked()){
btn_practice2.设置检查(错误);
btn_practice3.设置检查(错误);
btn_practice4.设置检查(错误);
}
打破
案例R.id.RB2:
如果(btn_practice2.isChecked()){
btn_practice1.设置检查(错误);
btn_practice3.设置检查(错误);
btn_practice4.设置检查(错误);
}
打破
案例R.id.RB3:
如果(btn_practice3.isChecked()){
btn_practice1.设置检查(错误);
btn_practice2.设置检查(错误);
btn_practice4.设置检查(错误);
}
打破
案例R.id.RB4:
如果(btn_practice4.isChecked()){
btn_practice1.设置检查(错误);
btn_practice2.设置检查(错误);
btn_practice3.设置检查(错误);
}
打破
}
crrtans=新阵列列表(新阵列列表(crrtans));
如果(温度等于(crrtans.get(l))){
TextView txtRadio=(TextView)findViewById(R.id.rdtxt);
txtRadio.setText(“+radioButton.getText()+”正确”);
txtRadio.setTextColor(Color.parseColor(“#008000”);
}否则{
TextView txtRadio=(TextView)findViewById(R.id.rdtxt);
txtRadio.setText(“+radioButton.getText()+”不正确”);
txtRadio.setTextColor(Color.parseColor(“#FF0000”);
}
}
});
按钮nextBtn=(按钮)findviewbyd(R.id.nxt\u btn);
nextBtn.setOnClickListener(新建按钮.OnClickListener(){
公共void onClick(视图v){
如果(j==ques1.size()-1){
showAlert1();
}
否则{
++j;
++num;
TextView txtque=(TextView)findViewById(R.id.que_txt);
setText(“Q”+num+ques1.get(j));
}      
});

要清除所有单选按钮,即组中没有要检查的单选按钮

clearCheck() 
例如乌尔

radiogroupname.clearCheck();

问题出在您的
public void onCheckedChanged(RadioGroup组,int checkedId)
您正在基于checkedId创建选中单选按钮的实例,一旦调用
clearCheck
,该单选按钮就可以为null。事实上,当单击下一个按钮时,在单选组上调用
clearCheck
,就会触发
onCheckedChanged
事件,并获得null checkedId。因此,您会得到一个null单选按钮执行此操作时单击按钮:
RadioButton RadioButton=(RadioButton)group.findViewById(checkedd);

编辑(更好的解释):

将此方法添加到类:

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")); 
       }
    }

希望这有帮助。如果它解决了您的问题,请不要忘记将其标记为答案。

它显示错误..E/AndroidRuntime(712):在com.example.finalpractice.Question$LoadQuestions$1.onCheckedChanged(Question.java:236)E/AndroidRuntime(712):在android.widget.RadioGroup.setCheckedId(RadioGroup.java:172)E/AndroidRuntime(712):at android.widget.RadioGroup.check(RadioGroup.java:166)E/AndroidRuntime(712):at android.widget.RadioGroup.clearCheck(RadioGroup.java:205)什么?我真的无法理解你的代码…请用另一种方式指导我..我想在用户单击下一步按钮时取消选中单选按钮…查看你的代码,在
onCheckedChanged
侦听器中你正在创建一个RadioButton实例,该实例已被
checkedId
检查过,因此如果你的checkedId为空,则RadioButton为null因此后面的代码会引发NullException。因此,在执行所有这些操作之前,请检查checkedId是否为null。为了更好的可读性,我已将所有代码封装在onCheckedChange中,并添加了此验证。它显示了这两个错误。.运算符!=未定义参数类型int、null和void对于变量doProcessingWithRadioButtoncheck是无效的类型,如果它不起作用,请给我整个类。我将在需要的地方编辑并发送回您。请参阅链接
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);
            }
        });