在Android中膨胀RadioGroup-同时选中多个radioButton

5

我制作了一个包含变量数量单选按钮的RadioGroup。问题在于,许多单选按钮可以被选中,而不是像我所知道的那样在RadioGroup中只能选中一个RadioButton。我的错误在哪里?以下是我的代码:

View view = inflater.inflate(R.layout.questionnaire_check_question, null);
RadioGroup radioGroup = (RadioGroup) view.findViewById(R.id.radioGroup);
    for (int k = 0; k < answerValues.size(); k++) {
         View radioButtonView = inflater.inflate(R.layout.check_radio_button, null);
         RadioButton radioButton = (RadioButton) radioButtonView
                            .findViewById(R.id.radio_button);
         radioButton.setText(answerValues.get(k));
         radioGroup.addView(radioButtonView);
    }
questionBody.addView(view);

questionnaire_check_question.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="5dp"
    android:background="#A4A7A6" >

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

</LinearLayout>

check_radio_button.xml

<?xml version="1.0" encoding="utf-8"?>
<RadioButton xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/radio_button"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:layout_marginLeft="5dp"
    android:button="@drawable/checkbox"
 >

</RadioButton>

drawable/checkbox.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >

    <item android:drawable="@drawable/icon_checkbox_on" android:state_checked="true"/>
    <item android:drawable="@drawable/icon_checkbox_off" android:state_checked="false"/>

</selector>

radioGroup有一个奇怪的功能...如果我选中第一个radioGroup,然后选中第二个,第一个就会变成未选中状态...之后,如果我想再次选择第一个,我就无法这样做了...它不再选中。 有人能帮忙吗?任何想法都受欢迎! 提前致谢。

2个回答

11

我找到了解决方案:问题在于所有按钮都具有相同的ID。因此,我为每个单选按钮设置了唯一的ID。


-2
我将举个例子来说明这是如何完成的,然后您可以相应地修改您的代码。 在您的 XML 中,要添加单选按钮,您需要定义一个类似下面的布局 -
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <RadioGroup
        android:id="@+id/radioSex"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <RadioButton
            android:id="@+id/radioMale"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/radio_male" 
            android:checked="true" />

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

    </RadioGroup>

    <Button
        android:id="@+id/btnDisplay"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/btn_display" />

</LinearLayout>

在您的Java文件中,执行以下步骤: i)像下面这样声明单选按钮和单选按钮组 -

 private RadioGroup radioSexGroup;
  private RadioButton radioSexButton;
  private Button btn;

ii)像下面这样在xml布局中注册您的单选按钮和单选组 -

radioSexGroup = (RadioGroup) findViewById(R.id.radioSex);
    btnDisplay = (Button) findViewById(R.id.btnDisplay);

iii)在按钮上添加onclicklistner,如下所示 -

btn.setOnClickListner(this);

iv) 实现OnClickListner接口并覆盖未实现的方法

v) 在重写的OnClick方法中编写以下代码 -

int selectedId = radioSexGroup.getCheckedRadioButtonId();

            // find the radiobutton by returned id
                radioSexButton = (RadioButton) findViewById(selectedId);

            Toast.makeText(MyAndroidAppActivity.this,
                radioSexButton.getText(), Toast.LENGTH_SHORT).show();

这就是如何完成的...希望这能帮到你。


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