【问题标题】:Checking at least one radio button is selected from each radiogroup in android?检查从android中的每个radiogroup中至少选择一个单选按钮?
【发布时间】:2014-02-07 19:48:28
【问题描述】:

如何确保每个单选组中至少检查了一个单选按钮。就像我有这个广播组:

<RadioGroup
    android:id="@+id/myradio_group_one"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="first"
        android:checked="true" />

    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="second" />

    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="third" />

    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="fourt" />
</RadioGroup>

<RadioGroup
    android:id="@+id/myradio_group_two"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="first"
        android:checked="true" />

    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="second" />

    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="third" />

    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="fourt" />
</RadioGroup>

我想以编程方式检查用户是否至少选择了一个单选按钮?

【问题讨论】:

    标签: java android radio-button


    【解决方案1】:

    获取RadioGroup 的引用,然后在无线电组上调用getCheckedRadioButtonId(),如果未选择任何内容,则调用将返回-1

    public int getCheckedRadioButtonId ()

    【讨论】:

      【解决方案2】:
      RadioGroup g = (RadioGroup) findViewById(R.id.rBtnDigits); 
      
      // Returns an integer which represents the selected radio button's ID
      int selected = g.getCheckedRadioButtonId();
      
      // Gets a reference to our "selected" radio button
      RadioButton b = (RadioButton) findViewById(selected);
      
      // Now you can get the text or whatever you want from the "selected" radio button
      b.getText();
      

      【讨论】:

      • 不是来自 xml,我需要以编程方式。
      【解决方案3】:
      if (radioGroup.getCheckedRadioButtonId() != -1)
      {
        // hurray at-least on radio button is checked. 
      }
      else
      {
        // pls select at-least one radio button.. since id is -1 means no button is check
      }
      

      【讨论】:

        【解决方案4】:

        这是我在我的一个测验应用程序中使用的代码。如果有帮助,请查看代码。

        private boolean checkAnswer(){
                String answer = getSelection();
                if(answer==null) {
                    Log.d("Questions", "No checkbox selected");
                    return false;
                }
                else {
                //  Do your stuff here
                    return true;
                }
        
            }
        
        
            private String getSelection(){
        
        
                if (c1.isChecked())
                {
                    return c1.getText().toString();
                }
                if (c2.isChecked())
                {
                    return c2.getText().toString();
                }
                if (c3.isChecked())
                {
                    return c3.getText().toString();
                }
                if (c4.isChecked())
                {
                    return c4.getText().toString();
                }
        
                return null;
        
            }
        

        【讨论】:

          【解决方案5】:

          你可以用这个方法

          radioGroup.getCheckedRadioButtonId();
          

          它返回该组中选定单选按钮的标识符。空选时,返回值为-1。

          如文档https://developer.android.com/reference/android/widget/RadioGroup.html#getCheckedRadioButtonId()中所述

          【讨论】:

            猜你喜欢
            • 2011-05-01
            • 1970-01-01
            • 1970-01-01
            • 2021-11-08
            • 2018-05-11
            • 1970-01-01
            • 1970-01-01
            • 2012-09-13
            • 2021-06-11
            相关资源
            最近更新 更多