在Android中检查每个单选按钮组中至少选择了一个单选按钮?

6

我该怎样确保用户至少从每个单选按钮组中选取一个单选按钮。就像我有这样的单选按钮组:

<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>

我想通过程序检查用户是否选中了至少一个单选按钮?
5个回答

15

获取RadioGroup的引用,然后在该单选按钮组上调用getCheckedRadioButtonId(),如果没有选择任何选项,则该调用将返回-1

请参阅public int getCheckedRadioButtonId ()


8
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 中获取,我需要通过编程方式获取。 - androidcodehunter

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
}

2

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

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;

    }

1

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接