2017-08-04 88 views
2

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

这里是布局文件:

<?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> 

启动应用这样的:

enter image description here

如果我点击爵士,就变成这样:

enter image description here

+0

可能不是一个错误,我会解释所以你检查了两个XML但没有设置单一的选择,因为用户可以选择他想要的,如果你想让用户只选择一个使广播组单选。 –

+0

尝试将'android:id'值赋给'RadioButton'小部件。请参阅[此问题](https://issuetracker.google.com/issues/36910867)。 – CommonsWare

+0

@CommonsWare,谢谢,分配id做了窍门,在UI中检查最后一个设置为检查。顺便说一句,我正在研究你的书:) –

回答

2

您的布局缺少01为RadioButton小部件设置的值为。如果您没有在布局资源中预先选中其中的任何一个,那么这可以起作用。如果您要在布局资源中使用android:checked,则需要将窗口小部件ID分配给RadioButton小部件。这是不可能改变的a long-standing issue,所以“这只是我们在Android应用开发中处理的那些事情之一”。