【问题标题】:How to check is the button is selected in each radio group?如何检查是否在每个单选组中选择了按钮?
【发布时间】:2017-07-24 17:54:50
【问题描述】:

我有 3 个单选组,我想检查每个单选组中的按钮是否被选中。

例如我想要类似的东西

if (in each radio group one button is selected){
do something 
}

if ( one of RadioGroups have no selected button) {
Show something like Toast 
}

【问题讨论】:

标签: java android android-studio radio-button android-radiogroup


【解决方案1】:

按名称阅读三个组。然后迭代其他它。使用条件语句检查 .checked。

var radios = document.getElementsByTagName('input1');
var value;
for (var i = 0; i < radios.length; i++) {
    if (radios[i].type === 'radio' && radios[i].checked) {
       // get value, set checked flag or do whatever you need to
       value = radios[i].value;       
    }
}

【讨论】:

    【解决方案2】:

    假设您有三个单选按钮,即一二和三。你可以这样做。

    if(one.isChecked() || two.isChecked() || three.isChecked()){
    
          //do something 
    
    
     }else if(!one.isChecked() || !two.isChecked() || !three.isChecked()){
         //do something
                        Toast.makeText(TourClass.this, "Passwords do not match", 
    Toast.LENGTH_LONG).show();
    
    
    
    
      }
    

    【讨论】:

      【解决方案3】:

      使用getCheckedRadioButtonId()RadioGroup 中查找选中的RadioButton

      【讨论】:

        【解决方案4】:

        为每个 RadioGroup 编写以下代码:

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

        你也可以在这里查看答案How to check if a radiobutton is checked in a radiogroup in Android?

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2021-10-21
          • 1970-01-01
          • 2014-04-09
          • 2011-03-28
          • 2012-05-03
          • 1970-01-01
          • 1970-01-01
          • 2014-07-11
          相关资源
          最近更新 更多