Android - RadioButton isn't uncheck with initial checked state

删除回忆录丶 提交于 2019-12-12 14:53:29

问题


I have a RadioButton within a RadioGroup,

the problem arises when i set the initial state of the button

android:checked = "true"

because if I press the RadioButton "F" the RadioButton "M" doesn't uncheck...

how can I do? what is wrong?

here's the code:

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

screen:

initial state (correct):

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

final state when i press RadioButton "F" (wrong):

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

thanks


回答1:


Assign an unique id to the radio buttons with android:id, then set the android:checkedButton attribute of the RadioGroup, like this:

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


来源:https://stackoverflow.com/questions/11505262/android-radiobutton-isnt-uncheck-with-initial-checked-state

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!