选择了两个RadioButtons

时间:2015-11-12 14:42:25

标签: android radio-button

为什么我选择了两个RadioButtons?

我希望一次只选择一个RadioButton

这是布局:

<RadioGroup
    android:orientation="vertical"
    android:id="@+id/radio_gender"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:checkedButton="@+id/radio_female"
    android:layout_alignRight="@+id/Button01"
    android:layout_alignTop="@+id/textView3"
    android:layout_toRightOf="@+id/button1" />

<RadioButton
    android:onClick="radioButtonClicked"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:id="@+id/radio_female"
    android:text="@string/radio_female" />

<RadioButton
    android:onClick="radioButtonClicked"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/radio_male"
    android:text="@string/radio_male" />

和代码:

public void radioButtonClicked(View view) {
    // Check that the button is  now checked?
    boolean checked = ((RadioButton) view).isChecked();

    // Check which radio button was clicked
    switch (view.getId()) {
        case R.id.radio_female:
            if (checked)
                userGender = "female";
            break;
        case R.id.radio_male:
            if (checked)
                userGender = "male";
            break;
    }
}

2 个答案:

答案 0 :(得分:2)

您需要将RadioButton放在RadioGroup内。像这样更改XML:

<RadioGroup
    android:orientation="vertical"
    android:id="@+id/radio_gender"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:checkedButton="@+id/radio_female"
    android:layout_alignRight="@+id/Button01"
    android:layout_alignTop="@+id/textView3"
    android:layout_toRightOf="@+id/button1">

    <RadioButton
        android:onClick="radioButtonClicked"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:id="@+id/radio_female"
        android:text="@string/radio_female" />

    <RadioButton
        android:onClick="radioButtonClicked"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/radio_male"
        android:text="@string/radio_male" />

</RadioGroup>

答案 1 :(得分:1)

关闭RadioGroup标记

<RadioGroup
        android:orientation="vertical"
        android:id="@+id/radio_gender"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:checkedButton="@+id/radio_female"
        android:layout_alignRight="@+id/Button01"
        android:layout_alignTop="@+id/textView3"
        android:layout_toRightOf="@+id/button1" />

        <RadioButton
            android:onClick="radioButtonClicked"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:id="@+id/radio_female"
            android:text="@string/radio_female" />

        <RadioButton
            android:onClick="radioButtonClicked"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/radio_male"
            android:text="@string/radio_male" />
</RadioGroup>