如果未选中单选按钮,则阻止表单提交

时间:2018-06-11 11:34:37

标签: android radio-group

使用教育Android应用程序。一个通过EditText输入,单选按钮和CheckBox提供答案。

我希望检查RadioGroup,当没有点击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
        }
    }

那个被困的地方。提前致谢

2 个答案:

答案 0 :(得分:0)

使用getCheckedRadioButtonId()代替getCheckedRadioButton()

if (rg.getCheckedRadioButtonId() == -1) {
    //no radio buttons are checked
}
else {
    //any one of the radio buttons is checked
}

答案 1 :(得分:0)

你需要radioGroup checked Change Listener:

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


            }
        });