تسمح أزرار الاختيار للمستخدم باختيار خيار واحد من مجموعة من الخيارات التي يستبعد أحدها الآخر. استخدِم أزرار الاختيار إذا كان المستخدم بحاجة إلى الاطّلاع على كل الخيارات المتاحة مرتبة. إذا لم يكن من الضروري عرض جميع الخيارات، استخدِم
أداة اختيار بدلاً من ذلك.
لإنشاء كل خيار من خيارات زر الاختيار، أنشئ رمزًا
RadioButton
في تنسيقك. بما أنّ أزرار الاختيار لا يمكن استخدامها مع بعضها، اجمعها داخل
a
RadioGroup.
يضمن النظام إمكانية اختيار زر اختيار واحد فقط ضمن مجموعة في كل مرة.
الردّ على أحداث النقر
عندما يختار المستخدم زر اختيار، يتلقّى عنصر
RadioButton المقابل حدث النقر.
يوضّح المثال التالي تفاعلًا مع نقر المستخدم على عنصر
RadioButton في مجموعة:
ضمن 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");}
في هذا المثال، عندما ينقر المستخدم على أحد الأزرار الاختيارية، تتم طباعة رسالة
في Logcat.
يخضع كل من المحتوى وعيّنات التعليمات البرمجية في هذه الصفحة للتراخيص الموضحّة في ترخيص استخدام المحتوى. إنّ Java وOpenJDK هما علامتان تجاريتان مسجَّلتان لشركة Oracle و/أو الشركات التابعة لها.
تاريخ التعديل الأخير: 2025-07-27 (حسب التوقيت العالمي المتفَّق عليه)
[[["يسهُل فهم المحتوى.","easyToUnderstand","thumb-up"],["ساعَدني المحتوى في حلّ مشكلتي.","solvedMyProblem","thumb-up"],["غير ذلك","otherUp","thumb-up"]],[["لا يحتوي على المعلومات التي أحتاج إليها.","missingTheInformationINeed","thumb-down"],["الخطوات معقدة للغاية / كثيرة جدًا.","tooComplicatedTooManySteps","thumb-down"],["المحتوى قديم.","outOfDate","thumb-down"],["ثمة مشكلة في الترجمة.","translationIssue","thumb-down"],["مشكلة في العيّنات / التعليمات البرمجية","samplesCodeIssue","thumb-down"],["غير ذلك","otherDown","thumb-down"]],["تاريخ التعديل الأخير: 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."]]