在托管此布局的 Activity 或 Fragment 中,找到您的单选按钮,并为每个单选按钮设置一个更改监听器,如下所示:
Kotlin
findViewById<RadioButton>(R.id.radio_pirates).setOnCheckedChangeListener{buttonView,isChecked->
Log.d("RADIO","Pirates is checked: $isChecked")}findViewById<RadioButton>(R.id.radio_ninjas).setOnCheckedChangeListener{buttonView,isChecked->
Log.d("RADIO","Ninjas is checked: $isChecked")}
Java
findViewById<RadioButton>(R.id.radio_pirates).setOnCheckedChangeListener{buttonView,isChecked->
Log.d("RADIO","Pirates is checked: $isChecked");}findViewById<RadioButton>(R.id.radio_ninjas).setOnCheckedChangeListener{buttonView,isChecked->
Log.d("RADIO","Ninjas is checked: $isChecked");}
[null,null,["最后更新时间 (UTC):2025-07-27。"],[],[],null,["# Add radio buttons to your app\n\nTry the Compose way \nJetpack Compose is the recommended UI toolkit for Android. Learn how to add components in Compose. \n[Radio buttons →](/develop/ui/compose/components/radio-button) \n\nRadio buttons let the user select one option from a set of mutually exclusive\noptions. Use radio buttons if the user needs to see all available options\nlisted. If it's not necessary to show all options, use a\n[spinner](/guide/topics/ui/controls/spinner) instead.\n| **Note:** For a better user experience, see the Material Design [Radio\n| button](https://m3.material.io/components/radio-button/overview) documentation.\n**Figure 1.** An example of radio buttons from [Material\nDesign](https://m3.material.io/components/radio-button/overview).\n\nTo create each radio button option, create a\n[RadioButton](/reference/android/widget/RadioButton)\nin your layout. Because radio buttons are mutually exclusive, group them inside\na\n[RadioGroup](/reference/android/widget/RadioGroup).\nThe system ensures that only one radio button within a group can be selected at\na time.\n\nRespond to click events\n-----------------------\n\nWhen the user selects a radio button, the corresponding\n`RadioButton` object receives an on-click event.\n\nThe following example shows a reaction to the user tapping a\n`RadioButton` object in a group: \n\n```xml\n\u003c?xml version=\"1.0\" encoding=\"utf-8\"?\u003e\n\u003cRadioGroup\n android:layout_width=\"match_parent\"\n android:layout_height=\"wrap_content\"\n android:orientation=\"vertical\"\u003e\n \u003cRadioButton android:id=\"@+id/radio_pirates\"\n android:layout_width=\"wrap_content\"\n android:layout_height=\"wrap_content\"\n android:text=\"Pirates\"/\u003e\n \u003cRadioButton android:id=\"@+id/radio_ninjas\"\n android:layout_width=\"wrap_content\"\n android:layout_height=\"wrap_content\"\n android:text=\"Ninjas\"/\u003e\n\u003c/RadioGroup\u003e\n```\n| **Note:** `RadioGroup` is a subclass of [LinearLayout](/reference/android/widget/LinearLayout) that has a vertical orientation by default.\n\nWithin the `Activity` or `Fragment` that hosts this\nlayout, find your radio buttons and set a change listener for each of them, as\nfollows: \n\n### Kotlin\n\n```kotlin\nfindViewById\u003cRadioButton\u003e(R.id.radio_pirates).setOnCheckedChangeListener { buttonView, isChecked -\u003e\n Log.d(\"RADIO\", \"Pirates is checked: $isChecked\")\n}\n\nfindViewById\u003cRadioButton\u003e(R.id.radio_ninjas).setOnCheckedChangeListener { buttonView, isChecked -\u003e\n Log.d(\"RADIO\", \"Ninjas is checked: $isChecked\")\n}\n```\n\n### Java\n\n```java\nfindViewById\u003cRadioButton\u003e(R.id.radio_pirates).setOnCheckedChangeListener { buttonView, isChecked -\u003e\n Log.d(\"RADIO\", \"Pirates is checked: $isChecked\");\n}\n\nfindViewById\u003cRadioButton\u003e(R.id.radio_ninjas).setOnCheckedChangeListener { buttonView, isChecked -\u003e\n Log.d(\"RADIO\", \"Ninjas is checked: $isChecked\");\n}\n```\n\nIn this example, when the user taps one of the radio buttons, a message\nprints in Logcat.\n| **Tip:** If you need to change the radio button state yourself, use the [setChecked(boolean)](/reference/android/widget/CompoundButton#setChecked(boolean)) or [toggle()](/reference/android/widget/CompoundButton#toggle()) method."]]