Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/234.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 是否可以这样做:表格内单选组中的单选按钮数量可变?_Android - Fatal编程技术网

Android 是否可以这样做:表格内单选组中的单选按钮数量可变?

Android 是否可以这样做:表格内单选组中的单选按钮数量可变?,android,Android,我目前有代码在表中创建行,该行在第1列中包含一个字符串,在第2列中包含一个单选按钮 这些行的数量可以是可变的。这一切都很好,但我想将它们添加到一个RadioGroup中,以便一次只能切换一个按钮 在我将动态RadioButton添加到表行之后,当我尝试将其添加到RadioGroup时,我得到一个错误,即该子项(RadioButton)已经有一个父项。我同意,它确实有一个,桌子 我的问题是,你能把单选按钮绑定到一行中的一个单选组吗?或者,我应该只编写自己的切换机制,避免将所有的单选组都绑定在一起吗

我目前有代码在表中创建行,该行在第1列中包含一个字符串,在第2列中包含一个单选按钮

这些行的数量可以是可变的。这一切都很好,但我想将它们添加到一个RadioGroup中,以便一次只能切换一个按钮

在我将动态RadioButton添加到表行之后,当我尝试将其添加到RadioGroup时,我得到一个错误,即该子项(RadioButton)已经有一个父项。我同意,它确实有一个,桌子

我的问题是,你能把单选按钮绑定到一行中的一个单选组吗?或者,我应该只编写自己的切换机制,避免将所有的单选组都绑定在一起吗?我的意思是,我可以编写onClick来解开所有其他单选按钮,但是如果我可以使用内置的RadioGroup,我宁愿不这样做

布局如下:

                <ScrollView
                    android:id="@+id/ScrollViewModifyGroups"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:scrollbars="vertical">
                    <TableLayout
                        android:id="@+id/TableLayout_ModifyGroups"
                        android:layout_width="fill_parent"
                        android:layout_height="fill_parent"
                        android:stretchColumns="*">
                        <TableRow>
                            <RadioGroup
                                android:id="@+id/radiogroup"
                                android:layout_width="fill_parent"
                                android:layout_height="wrap_content"
                                android:orientation="vertical">
                             </RadioGroup>
                        </TableRow>

                    </TableLayout>
                </ScrollView>

单选按钮只能存在于放射组中。

事实上,在我创建放射组之前,我创建单选按钮很好,所以我不确定这是什么意思


按钮显示得非常完美,我将RadioGroup添加到布局XML和之后的代码中,以获得按钮单击的排他性。

看起来RadioGroup必须实际包含其所有RadioButton才能工作,因此不能让它们跨表行拆分


我认为您必须编写自己的分组代码,或者更改构建视图的方式。你可以用一个垂直的放射组来固定按钮,并将文本视图放在其他容器中,但你必须并行跟踪它们,并找出一种保持文本和按钮对齐的好方法。

我也开始这样做了。这应该一点也不难做到……只要将所有单选按钮保留在一个列表中,迭代该列表,并关闭除最近检查的单选按钮之外的所有单选按钮。
  TableLayout modifyGroupTable = (TableLayout)findViewById(R.id.TableLayout_ModifyGroups);

  RadioButton groupButton = new RadioButton(this);

  insertGroupRow(modifyGroupTable, "SOME ID", groupButton);




    private void insertGroupRow(final TableLayout groupTable, String groupName, RadioButton radioButton)
    {
        final TableRow newRow = new TableRow(ReplayerCreateGroupsActivity.this);

        int textColor = getResources().getColor(R.color.title_color);
        float textSize = getResources().getDimension(R.dimen.help_text_size);

        addTextToRowWithValues(newRow, groupName, textColor, textSize);

        newRow.addView(radioButton);         

        groupTable.addView(newRow);

        try
        {
         radioGroup.addView(radioButton);
        }
        catch(Exception e)
        {
         e.printStackTrace();

        }
    }