2017-08-04 85 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應用開發中處理的那些事情之一」。