دکمه های رادیویی به کاربر این امکان را می دهند که یک گزینه را از میان مجموعه ای از گزینه های منحصر به فرد متقابل انتخاب کند. اگر کاربر نیاز دارد همه گزینه های موجود را مشاهده کند، از دکمه های رادیویی استفاده کنید. اگر لازم نیست همه گزینه ها را نشان دهید، به جای آن از اسپینر استفاده کنید.
برای ایجاد هر گزینه دکمه رادیویی، یک RadioButton در طرح خود ایجاد کنید. از آنجایی که دکمه های رادیویی متقابلاً منحصر به فرد هستند، آنها را در یک RadioGroup گروه بندی کنید. این سیستم تضمین می کند که در یک زمان تنها یک دکمه رادیویی در یک گروه می تواند انتخاب شود.
به رویدادهای کلیکی پاسخ دهید
هنگامی که کاربر یک دکمه رادیویی را انتخاب می کند، شی RadioButton مربوطه یک رویداد روی کلیک دریافت می کند.
مثال زیر واکنشی را به ضربه زدن کاربر روی یک شی RadioButton در یک گروه نشان می دهد:
در Activity یا Fragment که این طرحبندی را میزبانی میکند، دکمههای رادیویی خود را پیدا کنید و برای هر یک از آنها شنونده تغییر تنظیم کنید، به شرح زیر:
کاتلین
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")}
جاوا
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 چاپ میشود.
محتوا و نمونه کدها در این صفحه مشمول پروانههای توصیفشده در پروانه محتوا هستند. جاوا و OpenJDK علامتهای تجاری یا علامتهای تجاری ثبتشده Oracle و/یا وابستههای آن هستند.
تاریخ آخرین بهروزرسانی 2025-07-29 بهوقت ساعت هماهنگ جهانی.
[[["درک آسان","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-29 بهوقت ساعت هماهنگ جهانی."],[],[],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."]]