Android:如何设计以实现“RadioGroup”影响?

发布于 2024-09-13 06:29:36 字数 989 浏览 2 评论 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 效果?

谢谢你们。

I'm trying to design an Android layout that would look like the following:

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

...

With the requirement that for each entity, one and only one option is allowed.

And I decided to use radio button with the intention of using RadioGroup to enforce that. I designed as follows to achieve the above:

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

And the pseudo-code would be:

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

And the problem I'm having is that I can't group the radio buttons under one RadioGroup so that only one option is selected. At run-time, Android complains that the radio button must be a direct child of the row. If I add the radio button to the row first, then adding the radio button to the radio group first would then throw a similar run-time error.

Can anyone advise me how to achieve the RadioGroup effect using the above design or otherwise?

Thanks guys.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不再让梦枯萎 2024-09-20 06:29:36

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

Why not just use the RadioGroup inside the layout like following:

<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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文