Android 如果单选按钮不为';t检查

Android 如果单选按钮不为';t检查,android,radio-group,Android,Radio Group,正在开发教育android应用程序。一个通过EditText输入、单选按钮和复选框提供答案 我想检查一下RadioGroup,当单击noRadioButton时,它将提示学生并阻止提交。我成功地编辑了EditText,但对RadioButton不成功 public void checkEmptyButtons(View view){ RadioGroup rg = (RadioGroup) findViewById(R.id.myRg); if (rg.getCheckedRadio

正在开发教育android应用程序。一个通过
EditText
输入、单选按钮和
复选框提供答案

我想检查一下
RadioGroup
,当单击no
RadioButton
时,它将提示学生并阻止提交。我成功地编辑了
EditText
,但对
RadioButton
不成功

public void checkEmptyButtons(View view){
  RadioGroup rg = (RadioGroup) findViewById(R.id.myRg);
    if (rg.getCheckedRadioButton()==-1{

    // this is where I have a problem
    //I don't even know how to search on the                     //dev.anderoid site
        }
    }
if (rg.getCheckedRadioButtonId() == -1) {
    //no radio buttons are checked
}
else {
    //any one of the radio buttons is checked
}
那就是我被绊倒的地方。提前谢谢
.

使用
getCheckedRadioButtonId()
代替
getCheckedRadioButton()


您需要选中radioGroup更改侦听器:

rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
              //here you will get callback everytime radio button is checked along with id. You can have boolean checked here to know if any of your radio button is checked


            }
        });
可能的重复可能的重复