开发者

Can this be done: Variable number of radio buttons in a radio group inside a table?

I currently have code that creates rows in a table that hold a string in column 1 with a RadioButton in column 2.

There can be a variable number of these rows. That all works just great, but I want to add them to a RadioGroup so only one button can be toggled at a time.

When I tried to add the dynamic RadioButton to the RadioGroup AFTER I added it to the table row, I got an error saying that the child (the RadioButton) already had a parent. I agree, it does have one, the TableRow.

My question is, can you have radio buttons tied to a radio group inside of a row or, should I just code my own toggle mechanism and avoid RadioGroup all together? I mean, I could code the onClick to unclick all other radio buttons, but I would rather not do this if I can use the build in RadioGroup.

Here is the layout:

                <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>

Here is the Java code snippet of what I am doing:

  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);
        floa开发者_高级运维t 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();

        }
    }


RadioButtons can only exist inside a RadioGroup.


Actually I was creating RadioButtons just fine before I had a RadioGroup so I'm not sure what you mean by that.

The buttons were being displayed perfectly, and I added the RadioGroup to both the layout XML and the code after that to get exclusivity on the button clicks.


It looks like a RadioGroup has to physically contain all of its RadioButtons to work, so you can't have them split across table rows.

I think you will have to either write your own grouping code, or change the way you build the view. You could have a vertical RadioGroup to hold the buttons and have the text views in some other container, but you would have to track them in parallel and figure out a good way to keep the text and the buttons aligned.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜