Android - 初始选中状态下的单选按钮无法取消选中状态

3

我有一个RadioButton位于RadioGroup中,

当我设置按钮的初始状态时,问题就出现了

android:checked = "true"

因为如果我按下RadioButton“F”,RadioButton“M”不会取消选中状态...

我该怎么办?出了什么问题?

以下是代码:

<RadioGroup
   android:id="@+id/registrazione_utente_sesso"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:orientation="horizontal" >

   <RadioButton
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:checked="true"
      android:text="M"
      android:textColor="#ff7415"
      android:textSize="18sp" />

   <RadioButton
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_marginLeft="13.33dp"
      android:text="F"
      android:textColor="#ff7415"
      android:textSize="18sp" />
</RadioGroup>

屏幕:

初始状态(正确):

http://i.imgur.com/YsUIg.png

当我按下RadioButton“F”时的最终状态(错误):

http://i.imgur.com/YJms9.png

谢谢


如果您想设置按钮的初始状态,您必须手动管理。 - AkashG
你需要设置如果F被选中,则M未被选中。 - AkashG
1个回答

5

为单选按钮分配一个唯一的id,使用android:id属性,然后设置RadioGroup的android:checkedButton属性,如下所示:

<RadioGroup
   android:id="@+id/registrazione_utente_sesso"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:orientation="horizontal"
   android:checkedButton="@+id/radiobutton_m" >

   <RadioButton
      android:id="@+id/radiobutton_m"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="M"
      android:textColor="#ff7415"
      android:textSize="18sp" />

   <RadioButton
      android:id="@+id/radiobutton_f"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_marginLeft="13.33dp"
      android:text="F"
      android:textColor="#ff7415"
      android:textSize="18sp" />
</RadioGroup>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接