如何在没有 ID 的情况下默认检查 RadioGroup 中的 RadioButton?

bjs22538

所以我在 RadioGroup 中有一个 RadioButton,应该在不使用 Id 的情况下进行检查。

这是我的 XML 代码:

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

    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="RadioButton 1" />

    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="RadioButton 2" />
</RadioGroup>

在这里,我检查选定的 RadioButton:

RadioGroup radioGroup = findViewById(R.id.radioGroup);
int radioButtonId = radioGroup.getCheckedRadioButtonId();

switch (radioButtonId) {
    case 1:
        doSomething();
        break;
    case 2:
        doSomething();
        break;
}

我尝试过这个:

1) 向 RadioGroup 添加属性 android:android:checkedButton。

<RadioGroup
    android:id="@+id/radioGroup"
    android:checkedButton="@id/radioButton1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

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

    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="RadioButton 2" />
</RadioGroup>

doSometheing() 未执行,因为 radioButtonId 不是索引,而是 Id。

2)然后我在我的 RadioButton 中添加了一个属性 android:checked 为“true”。

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

    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:checked="true"
        android:text="RadioButton 1" />

    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="RadioButton 2" />
</RadioGroup>

doSometheing() 已执行,但现在可以同时检查两个 RadioButton。无法取消选中第一个 RadioButton。radioButtonId 保持不变 == 1。

我希望你能告诉我正确的方法。

谢谢你。祝你今天过得愉快。

阿布诺曼

您可以获得checked RadioButton如下索引

int radioButtonID = radioButtonGroup.getCheckedRadioButtonId();
View radioButton = radioButtonGroup.findViewById(radioButtonID);
int idx = radioButtonGroup.indexOfChild(radioButton);

switch (idx) {
    case 1:
        doSomething();
        break;
    case 2:
        doSomething();
        break;
}

注意:不要忘记idradio buttons.

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

RadioGroup中的自定义RadioButton没有独占选择

来自分类Dev

如何将 LineaLayout 中的 RadioButton 放在 RadioGroup 中

来自分类Dev

如何在 C# 中检查 Azure 数据工厂管道是否在没有运行 ID 的情况下运行

来自分类Dev

单击列表中的前两项,从Android的Adapter类中的radioGroup下禁用RadioButton,并选择其他radioButton

来自分类Dev

如何在没有_id的情况下将文档保存在表中

来自分类Dev

如何使用MVVMCross绑定Android RadioGroup的RadioButton

来自分类Dev

如何设置在radioGroup中检查的孩子[java]

来自分类Dev

如何在我的表单上禁用 RadioGroup 中三个中的一个 RadioButton

来自分类Dev

在Linux中,默认情况下如何自动计算新的用户ID?

来自分类Dev

RadioGroup中RadioButton之间的分隔线-与GMail相似

来自分类Dev

使用字符串数组中的 RadioButton 填充 RadioGroup

来自分类Dev

如何在没有ID的情况下通过getElementByClassName或querySelector在javascript中设置选择值dropdownlist

来自分类Dev

如何在没有任何ID的情况下以javascript DOM中的ul和li为目标元素

来自分类Dev

instagram 如何在没有像 PHP 中那样获取 ID 的情况下进行用户名系统

来自分类Dev

Android RadioGroup Radiobutton Java

来自分类Dev

如何在python中没有regex的情况下检查字符串的要求?

来自分类Dev

如何在 Recyclerview 中获取 RadioGroup 检查状态?

来自分类Dev

如何使radioGroup内的radioButton的整个区域都可单击?

来自分类Dev

Android RadioGroup/RadioButton 选择 - 在 Android API 16/17 中不同 - 无法选择 Radiobutton

来自分类Dev

没有应用程序ID的情况下如何访问默认应用程序?

来自分类Dev

默认情况下,如何在Emacs中反转视频?

来自分类Dev

默认情况下如何在Treeview中设置Checkbox

来自分类Dev

如何在没有尝试捕获的情况下检查Metro应用程序中是否存在文件

来自分类Dev

如何在QML中控制默认RadioButton的大小?

来自分类Dev

如何从nodeJS中的请求解析JSON?默认情况下(没有Express等packgs)

来自分类Dev

默认情况下,Java中的字段是否私有?

来自分类Dev

如何在没有@Id的情况下使用spring仓库?

来自分类Dev

如何在没有id的情况下将jQuery元素传递给JavaScript函数?

来自分类Dev

如何在没有资源的情况下创建资源ID

Related 相关文章

  1. 1

    RadioGroup中的自定义RadioButton没有独占选择

  2. 2

    如何将 LineaLayout 中的 RadioButton 放在 RadioGroup 中

  3. 3

    如何在 C# 中检查 Azure 数据工厂管道是否在没有运行 ID 的情况下运行

  4. 4

    单击列表中的前两项,从Android的Adapter类中的radioGroup下禁用RadioButton,并选择其他radioButton

  5. 5

    如何在没有_id的情况下将文档保存在表中

  6. 6

    如何使用MVVMCross绑定Android RadioGroup的RadioButton

  7. 7

    如何设置在radioGroup中检查的孩子[java]

  8. 8

    如何在我的表单上禁用 RadioGroup 中三个中的一个 RadioButton

  9. 9

    在Linux中,默认情况下如何自动计算新的用户ID?

  10. 10

    RadioGroup中RadioButton之间的分隔线-与GMail相似

  11. 11

    使用字符串数组中的 RadioButton 填充 RadioGroup

  12. 12

    如何在没有ID的情况下通过getElementByClassName或querySelector在javascript中设置选择值dropdownlist

  13. 13

    如何在没有任何ID的情况下以javascript DOM中的ul和li为目标元素

  14. 14

    instagram 如何在没有像 PHP 中那样获取 ID 的情况下进行用户名系统

  15. 15

    Android RadioGroup Radiobutton Java

  16. 16

    如何在python中没有regex的情况下检查字符串的要求?

  17. 17

    如何在 Recyclerview 中获取 RadioGroup 检查状态?

  18. 18

    如何使radioGroup内的radioButton的整个区域都可单击?

  19. 19

    Android RadioGroup/RadioButton 选择 - 在 Android API 16/17 中不同 - 无法选择 Radiobutton

  20. 20

    没有应用程序ID的情况下如何访问默认应用程序?

  21. 21

    默认情况下,如何在Emacs中反转视频?

  22. 22

    默认情况下如何在Treeview中设置Checkbox

  23. 23

    如何在没有尝试捕获的情况下检查Metro应用程序中是否存在文件

  24. 24

    如何在QML中控制默认RadioButton的大小?

  25. 25

    如何从nodeJS中的请求解析JSON?默认情况下(没有Express等packgs)

  26. 26

    默认情况下,Java中的字段是否私有?

  27. 27

    如何在没有@Id的情况下使用spring仓库?

  28. 28

    如何在没有id的情况下将jQuery元素传递给JavaScript函数?

  29. 29

    如何在没有资源的情况下创建资源ID

热门标签

归档