Les cases d'option permettent à l'utilisateur de sélectionner une option parmi un ensemble d'options s'excluant mutuellement. Utilisez des cases d'option si l'utilisateur doit voir toutes les options disponibles. Si vous n'avez pas besoin d'afficher toutes les options, utilisez plutôt un sélecteur.
Pour créer chaque option de bouton radio, créez un RadioButton dans votre mise en page. Étant donné que les cases d'option s'excluent mutuellement, regroupez-les dans un élément RadioGroup.
Le système garantit qu'une seule case d'option d'un groupe peut être sélectionnée à la fois.
Répondre aux événements de clic
Lorsque l'utilisateur sélectionne une case d'option, l'objet RadioButton correspondant reçoit un événement de clic.
L'exemple suivant montre une réaction lorsque l'utilisateur appuie sur un objet RadioButton dans un groupe:
Dans le Activity ou le Fragment qui héberge cette mise en page, recherchez vos boutons d'option et définissez un écouteur de modification pour chacun d'eux, comme suit:
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");}
Dans cet exemple, lorsqu'un utilisateur appuie sur l'un des boutons radio, un message s'affiche dans Logcat.
Le contenu et les exemples de code de cette page sont soumis aux licences décrites dans la Licence de contenu. Java et OpenJDK sont des marques ou des marques déposées d'Oracle et/ou de ses sociétés affiliées.
Dernière mise à jour le 2025/07/27 (UTC).
[null,null,["Dernière mise à jour le 2025/07/27 (UTC)."],[],[],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."]]