0

我正在尝试设计一个如下所示的 Android 布局:

Entity1
option1 for Entity1
option2
...
optionN
Entity2
option1 for Entity2
...
optionN
Entity3

...

要求对于每个实体,只允许一个选项。

我决定使用单选按钮,目的是使用 RadioGroup 来强制执行。为了达到上述目的,我设计如下:

<table layout>
    <TextView, content = "Entity1"/>
    <TextView, content = "option1 for Entity1"/> <RadioButton/>
    <TextView, content = "option2"> <RadioButton/>
   ...
</table layout>

伪代码是:

 RadioGroup raGroup = createEmptyRaGroup()
    ...
    row = new TableRow(); 
    row.add(TextView-entity1);  
    row.add(TextView-op1ForEntity1); 
    RadioButtton raBtn = createRadioButton(); 
    raGroup.addView(raBtn); 
    row.addView(raBtn);  
    ...

我遇到的问题是我无法将单选按钮分组到一个 RadioGroup 下,以便只选择一个选项。在运行时,Android 抱怨单选按钮必须是该行的直接子级。如果我先将单选按钮添加到行,然后先将单选按钮添加到单选组,则会引发类似的运行时错误。

谁能告诉我如何使用上述设计或其他方式实现 RadioGroup 效果?

多谢你们。

4

1 回答 1

2

为什么不直接在布局中使用 RadioGroup,如下所示:

<RadioGroup
    android:id="@+id/RadioGroup01"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

<RadioButton 
    android:text="Choice 1"
    android:id="@+id/RadioButton01"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>

<RadioButton
    android:text="Choice 2"
    android:id="@+id/RadioButton02" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"/>
</RadioGroup>
于 2010-08-07T03:06:10.997 回答