【发布时间】:2016-12-21 12:10:57
【问题描述】:
当我启动代码时,我正在尝试将单选按钮添加到具有表格布局的单选组中,但出现了一些错误。这是我的代码
RadioButton[] boxes = new RadioButton[30];
RadioGroup rg = (RadioGroup) findViewById(R.id.radiogroup);
for (int i = 0; i < 30; i++) {
boxes[i] = (RadioButton) findViewById(idArray[i]);
}
for (int i = 0; i < 30; i++) {
rg.addView(boxes[i]);
}
xml代码在这里`
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="11-12(T1)"
android:id="@+id/radioButton0"
android:layout_column="9"
android:checked="false" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="11-12(T2)"
android:id="@+id/radioButton10"
android:checked="false"
android:layout_marginLeft="40dp" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="11-12(T3)"
android:id="@+id/radioButton20"
android:layout_column="11"
android:checked="false"
android:layout_marginLeft="40dp" />
</TableRow>
</TableLayout>
</RadioGroup>
我没有发布整个 xml 代码,因为有 10 个表格行,每个表格行有 3 个单选按钮,所以我在 3*10 网格中有 30 个单选按钮。在 java 代码中我得到类似的错误“指定的孩子已经有一个父母。你必须先在孩子的父母上调用 removeView()。”
【问题讨论】:
标签: android radio-button radio-group