我正在尝试设计一个如下所示的 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 效果?
多谢你们。