Android RadiGroup 允许多选

Mer*_*aya 3 android radio-group

如果我最初预选多个 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>
Run Code Online (Sandbox Code Playgroud)

该应用程序启动如下:

在此处输入图片说明

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

在此处输入图片说明

Com*_*are 8

您的布局缺少小部件的android:idRadioButton。如果您没有从布局资源中预先检查的任何一个开始,那可以工作。如果要android:checked在布局资源中使用,则需要为小RadioButton部件分配小部件 ID 。这是一个长期存在的问题,不太可能改变,因此“这只是我们在 Android 应用程序开发中必须处理的事情之一”。