【问题标题】:Android: How to design to achive the "RadioGroup" effect?Android:如何设计实现“RadioGroup”效果?
【发布时间】:2011-03-26 15:01:36
【问题描述】:

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

谢谢各位。

【问题讨论】:

    标签: android radio-button tablelayout


    【解决方案1】:

    为什么不在布局中使用 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>
    

    【讨论】:

    • 糟糕:D 谢谢。我会尽力回复你,但我认为它会奏效。
    • @hungh3,如果这解决了你的问题,我鼓励你接受我的回答,这是 StackOverflow 的方式。
    • 完成!再次,谢谢。顺便说一句,你知道我们如何获得文本按钮而不是按钮文本作为正常的 RadioButton 布局吗?
    • 我不熟悉。如果 RadioButton 类不支持它,您可能只想在 RadioButton 旁边使用 TextView。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-14
    • 2015-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多