【问题标题】:Android RadiGroup allows multiple selectionAndroid RadiGroup 允许多选
【发布时间】:2018-01-12 19:19:33
【问题描述】:

如果我最初预选多个 RadioButton,Android API Level 24(模拟器)似乎允许多选。我只是想知道这是否是一个错误?

这是布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

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

       <RadioButton
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:text="@string/metal"
           android:checked="true"/>

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/classical"
            android:checked="true"/>

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/jazz"/>

    </RadioGroup>

</LinearLayout>

应用启动如下:

如果我点击 Jazz,它会变成这样:

【问题讨论】:

  • 可能不是一个错误,我会解释,所以你在 xml 中检查了两个但没有设置单选,因为如果你希望用户只选择一个使单选组成为单选,用户可以选择他想要的选择。
  • 尝试将android:id 值分配给RadioButton 小部件。见this issue
  • @CommonsWare,谢谢,分配 id 成功了,最后一个设置为检查在 UI 中检查。顺便说一句,我正在研究你的书:)

标签: android radio-group


【解决方案1】:

您的布局缺少RadioButton 小部件的android:id 值。如果您没有从布局资源中预先检查的任何一个开始,这可以工作。如果要在布局资源中使用android:checked,则需要为RadioButton 小部件分配小部件ID。这是a long-standing issue,不太可能改变,所以“这只是我们在 Android 应用开发中必须处理的事情之一”。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-01-23
    • 2011-07-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-25
    • 2013-06-14
    • 2014-01-25
    相关资源
    最近更新 更多