在Android-multiple radioButtons中一次性检查RadioGroup

Gab*_*lle 5 android radio-group radio-button android-inflate

RadioGroup用一个可变数量的radioButtons 做了一个.问题是可以检查许多radiobutton,它不是一个单一的RadioButton检查RadioGroup(据我所知)我的错误在哪里?这是我的代码:

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

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

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

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

radioGroup有一个奇怪的功能..如果我检查第一个RadioGroup,然后是第二个,第一个变为未选中...在此之后,如果我想再次选择第一个,我不能这样做..它不再检查了.谁能帮我 ?欢迎任何想法!提前致谢.

Gab*_*lle 9

我找到了解决方案:问题是所有按钮都具有相同的ID.所以我为每个radioButton设置了一个唯一的id.