》 When the user selects one of the radio buttons, the corresponding RadioButton
object receives an on-click event.
<?xml version="1.0" encoding="utf-8"?> <RadioGroup xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical"> <RadioButton android:id="@+id/radio_pirates" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/pirates" android:onClick="onRadioButtonClicked"/> <RadioButton android:id="@+id/radio_ninjas" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/ninjas" android:onClick="onRadioButtonClicked"/> </RadioGroup>Note: The
RadioGroup
is a subclass of
LinearLayout
that has a vertical orientation by default.
》Tip: If you need to change the radio button state yourself (such as when loading a savedCheckBoxPreference
), use the setChecked(boolean)
or toggle()
method.