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