我制作了一个具有可变数量的单选按钮的 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,然后检查第二个,第一个变为未选中...之后,如果我想再次选择第一个,我做不到这个..它不再检查了。 谁能帮我 ?欢迎任何想法! 提前致谢。
最佳答案
我找到了解决方案:问题是所有按钮都具有相同的 ID。所以我为每个radioButton 设置了一个唯一的id。
关于android - Inflating RadioGroup in Android-一次检查多个radioButtons,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17693940/