با مجموعهها، منظم بمانید
ذخیره و طبقهبندی محتوا براساس اولویتهای شما.
یک دکمه رادیویی به کاربر این امکان را می دهد که یک گزینه را از میان مجموعه ای از گزینه ها انتخاب کند. زمانی از دکمه رادیویی استفاده می کنید که فقط یک مورد از یک لیست انتخاب شود. اگر کاربران نیاز به انتخاب بیش از یک مورد دارند، به جای آن از یک سوئیچ استفاده کنید.
شکل 1. یک جفت دکمه رادیویی با یک گزینه انتخاب شده است.
سطح API
از RadioButton composable برای فهرست کردن گزینه های موجود استفاده کنید. هر گزینه RadioButton و برچسب آن را درون یک جزء Row قرار دهید تا آنها را با هم گروه بندی کنید.
RadioButton شامل پارامترهای کلیدی زیر است:
selected : نشان می دهد که آیا دکمه رادیویی انتخاب شده است یا خیر.
onClick : یک تابع لامبدا که برنامه شما زمانی که کاربر روی دکمه رادیویی کلیک میکند، اجرا میکند. اگر null باشد، کاربر نمیتواند مستقیماً با دکمه رادیویی تعامل داشته باشد.
enabled : فعال یا غیرفعال بودن دکمه رادیویی را کنترل می کند. کاربران نمی توانند با دکمه های رادیویی غیرفعال تعامل داشته باشند.
interactionSource : به شما امکان می دهد وضعیت تعامل دکمه را مشاهده کنید، مثلاً فشار داده شده، شناور یا فوکوس شده باشد.
یک دکمه رادیویی اولیه ایجاد کنید
قطعه کد زیر فهرستی از دکمههای رادیویی را در یک Column ارائه میکند:
@ComposablefunRadioButtonSingleSelection(modifier:Modifier=Modifier){valradioOptions=listOf("Calls","Missed","Friends")val(selectedOption,onOptionSelected)=remember{mutableStateOf(radioOptions[0])}// Note that Modifier.selectableGroup() is essential to ensure correct accessibility behaviorColumn(modifier.selectableGroup()){radioOptions.forEach{text->
Row(Modifier.fillMaxWidth().height(56.dp).selectable(selected=(text==selectedOption),onClick={onOptionSelected(text)},role=Role.RadioButton).padding(horizontal=16.dp),verticalAlignment=Alignment.CenterVertically){RadioButton(selected=(text==selectedOption),onClick=null// null recommended for accessibility with screen readers)Text(text=text,style=MaterialTheme.typography.bodyLarge,modifier=Modifier.padding(start=16.dp))}}}}
radioOptions نشان دهنده برچسب های دکمه های رادیویی است.
تابع remember composable یک متغیر حالت selectedOption و یک تابع برای به روز رسانی آن حالت به نام onOptionSelected ایجاد می کند. این حالت گزینه دکمه رادیویی انتخاب شده را نگه می دارد.
mutableStateOf(radioOptions[0]) حالت را به اولین مورد در لیست مقداردهی می کند. "تماس ها" اولین مورد است، بنابراین دکمه رادیویی به طور پیش فرض انتخاب شده است.
Modifier.selectableGroup() رفتار دسترسی مناسب را برای صفحه خوان ها تضمین می کند. به سیستم اطلاع میدهد که عناصر موجود در این Column بخشی از یک گروه قابل انتخاب هستند که پشتیبانی مناسب از صفحهخوان را ممکن میسازد.
selected نشان می دهد که آیا Row فعلی بر اساس حالت selectedOption انتخاب شده است یا خیر.
تابع onClick lambda وضعیت selectedOption را به گزینه کلیک شده به روز می کند وقتی روی Row کلیک می شود.
role = Role.RadioButton به خدمات دسترسی اطلاع می دهد که Row به عنوان یک دکمه رادیویی عمل می کند.
RadioButton(...)RadioButton قابل ترکیب را ایجاد می کند.
onClick = null در RadioButton دسترسی را بهبود می بخشد. این امر از مدیریت مستقیم رویداد کلیک توسط دکمه رادیویی جلوگیری می کند و به اصلاح کننده selectableRow اجازه می دهد تا وضعیت انتخاب و رفتار دسترسی را مدیریت کند.
نتیجه
شکل 2. سه دکمه رادیویی با گزینه "Friends" انتخاب شده است.
محتوا و نمونه کدها در این صفحه مشمول پروانههای توصیفشده در پروانه محتوا هستند. جاوا و OpenJDK علامتهای تجاری یا علامتهای تجاری ثبتشده Oracle و/یا وابستههای آن هستند.
تاریخ آخرین بهروزرسانی 2025-08-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-08-27 بهوقت ساعت هماهنگ جهانی."],[],[],null,["A [radio button](https://m3.material.io/components/radio-button/overview) lets a user select an option from a set of\noptions. You use a radio button when only one item can be selected from a\nlist. If users need to select more than one item, use a [switch](https://m3.material.io/components/switch/overview)\ninstead.\n**Figure 1.** A pair of radio buttons with one option selected.\n\nAPI surface\n\nUse the [`RadioButton`](/reference/kotlin/androidx/compose/material3/package-summary#RadioButton(kotlin.Boolean,kotlin.Function0,androidx.compose.ui.Modifier,kotlin.Boolean,androidx.compose.material3.RadioButtonColors,androidx.compose.foundation.interaction.MutableInteractionSource)) composable to list the available options. Wrap each\n`RadioButton` option and its label inside a `Row` component to group them\ntogether.\n\n`RadioButton` includes the following key parameters:\n\n- `selected`: Indicates whether the radio button is selected.\n- `onClick`: A lambda function that your app executes when the user clicks the radio button. If this is `null`, the user can't interact directly with the radio button.\n- `enabled`: Controls whether the radio button is enabled or disabled. Users can't interact with disabled radio buttons.\n- `interactionSource`: Lets you observe the interaction state of the button, for example, whether it's pressed, hovered, or focused.\n\nCreate a basic radio button\n\nThe following code snippet renders a list of radio buttons within a `Column`:\n\n\n```kotlin\n@Composable\nfun RadioButtonSingleSelection(modifier: Modifier = Modifier) {\n val radioOptions = listOf(\"Calls\", \"Missed\", \"Friends\")\n val (selectedOption, onOptionSelected) = remember { mutableStateOf(radioOptions[0]) }\n // Note that Modifier.selectableGroup() is essential to ensure correct accessibility behavior\n Column(modifier.selectableGroup()) {\n radioOptions.forEach { text -\u003e\n Row(\n Modifier\n .fillMaxWidth()\n .height(56.dp)\n .selectable(\n selected = (text == selectedOption),\n onClick = { onOptionSelected(text) },\n role = Role.RadioButton\n )\n .padding(horizontal = 16.dp),\n verticalAlignment = Alignment.CenterVertically\n ) {\n RadioButton(\n selected = (text == selectedOption),\n onClick = null // null recommended for accessibility with screen readers\n )\n Text(\n text = text,\n style = MaterialTheme.typography.bodyLarge,\n modifier = Modifier.padding(start = 16.dp)\n )\n }\n }\n }\n}https://github.com/android/snippets/blob/5673ffc60b614daf028ee936227128eb8c4f9781/compose/snippets/src/main/java/com/example/compose/snippets/components/RadioButton.kt#L39-L70\n```\n\n\u003cbr /\u003e\n\nKey points about the code\n\n- `radioOptions` represents the labels for the radio buttons.\n- The `remember` composable function creates a state variable `selectedOption` and a function to update that state called `onOptionSelected`. This state holds the selected radio button option.\n - `mutableStateOf(radioOptions[0])` initializes the state to the first item in the list. \"Calls\" is the first item, so it's the radio button selected by default.\n- [`Modifier.selectableGroup()`](/reference/kotlin/androidx/compose/ui/Modifier#(androidx.compose.ui.Modifier).selectableGroup()) ensures proper accessibility behavior for screen readers. It informs the system that the elements within this `Column` are part of a selectable group, which enables proper screen reader support.\n- [`Modifier.selectable()`](/reference/kotlin/androidx/compose/ui/Modifier#(androidx.compose.ui.Modifier).selectable(kotlin.Boolean,kotlin.Boolean,androidx.compose.ui.semantics.Role,kotlin.Function0)) makes the entire `Row` act as a single selectable item.\n - `selected` indicates whether the current `Row` is selected based on the `selectedOption` state.\n - The `onClick` lambda function updates the `selectedOption` state to the clicked option when the `Row` is clicked.\n - `role = Role.RadioButton` informs accessibility services that the `Row` functions as a radio button.\n- `RadioButton(...)` creates the `RadioButton` composable.\n - `onClick = null` on the `RadioButton` improves accessibility. This prevents the radio button from handling the click event directly, and allows the `Row`'s `selectable` modifier to manage the selection state and accessibility behavior.\n\nResult **Figure 2.** Three radio buttons with the \"Friends\" option selected.\n\nAdditional resources\n\n- Material Design: [Buttons](https://m3.material.io/components/buttons/overview)"]]