从android中的每个radiogroup中选择至少一个单选按钮?

and*_*ter 5 java android radio-button

如何确保用户从每个放射组中至少检查一个radiobutton.就像我有这个无线电组:

<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>
Run Code Online (Sandbox Code Playgroud)

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

小智 15

获取该参考,RadioGroup然后getCheckedRadioButtonId()在没有选择任何内容的情况下呼叫无线电组,然后呼叫将返回-1.

看到 public int getCheckedRadioButtonId ()


Dig*_*tel 6

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();
Run Code Online (Sandbox Code Playgroud)


kar*_*ran 5

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
}
Run Code Online (Sandbox Code Playgroud)