【问题标题】:Prevent form submission if radio button isn't checked如果未选中单选按钮,则阻止提交表单
【发布时间】:2018-11-20 15:32:40
【问题描述】:

正在开发一款教育类安卓应用。一个通过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
        }
    }

这就是卡住的地方。提前致谢 .

【问题讨论】:

标签: android radio-group


【解决方案1】:

使用getCheckedRadioButtonId() 代替getCheckedRadioButton()。

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

【讨论】:

    【解决方案2】:

    你需要radioGroup选中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
    
    
                }
            });
    

    【讨论】:

      猜你喜欢
      • 2017-10-10
      • 2016-03-14
      • 2020-01-30
      • 1970-01-01
      • 2021-12-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-01
      相关资源
      最近更新 更多