Sử dụng bộ sưu tập để sắp xếp ngăn nắp các trang
Lưu và phân loại nội dung dựa trên lựa chọn ưu tiên của bạn.
Nút chọn cho phép người dùng chọn một lựa chọn trong một tập hợp các lựa chọn. Bạn sử dụng nút chọn khi chỉ có thể chọn một mục trong danh sách. Nếu người dùng cần chọn nhiều mục, hãy sử dụng công tắc.
Hình 1. Một cặp nút chọn có một lựa chọn được chọn.
Nền tảng API
Sử dụng thành phần kết hợp RadioButton để liệt kê các lựa chọn có sẵn. Bọc từng lựa chọn RadioButton và nhãn của lựa chọn đó bên trong một thành phần Row để nhóm chúng lại với nhau.
RadioButton bao gồm các thông số chính sau:
selected: Cho biết liệu nút chọn có được chọn hay không.
onClick: Một hàm lambda mà ứng dụng của bạn thực thi khi người dùng nhấp vào nút chọn. Nếu đây là null, người dùng sẽ không thể tương tác trực tiếp với nút chọn.
enabled: Kiểm soát xem nút chọn có được bật hay tắt. Người dùng không thể tương tác với các nút chọn bị vô hiệu hoá.
interactionSource: Cho phép bạn quan sát trạng thái tương tác của nút, ví dụ: liệu nút có được nhấn, di chuột hoặc lấy làm tâm điểm hay không.
Tạo nút chọn cơ bản
Đoạn mã sau đây hiển thị danh sách các nút chọn trong một 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))}}}}
Hàm có khả năng kết hợp remember tạo ra một biến trạng thái selectedOption và một hàm để cập nhật trạng thái đó có tên là onOptionSelected. Trạng thái này giữ lựa chọn nút chọn đã chọn.
mutableStateOf(radioOptions[0]) khởi chạy trạng thái thành mục đầu tiên trong danh sách. "Cuộc gọi" là mục đầu tiên, nên đây là nút chọn được chọn theo mặc định.
Modifier.selectableGroup() đảm bảo hành vi hỗ trợ tiếp cận phù hợp cho trình đọc màn hình. Thuộc tính này thông báo cho hệ thống rằng các phần tử trong Column này là một phần của nhóm có thể chọn, giúp hỗ trợ trình đọc màn hình đúng cách.
Modifier.selectable() làm cho toàn bộ Row hoạt động như một mục duy nhất có thể chọn.
selected cho biết liệu Row hiện tại có được chọn hay không dựa trên trạng thái selectedOption.
Hàm lambda onClick cập nhật trạng thái selectedOption thành lựa chọn đã nhấp khi người dùng nhấp vào Row.
role = Role.RadioButton thông báo cho các dịch vụ hỗ trợ tiếp cận rằng Row hoạt động như một nút chọn.
RadioButton(...) tạo thành phần kết hợp RadioButton.
onClick = null trên RadioButton giúp cải thiện khả năng hỗ trợ tiếp cận. Việc này ngăn nút chọn xử lý trực tiếp sự kiện nhấp và cho phép đối tượng sửa đổi selectable của Row quản lý trạng thái lựa chọn và hành vi hỗ trợ tiếp cận.
Kết quả
Hình 2. Ba nút chọn, trong đó nút "Bạn bè" được chọn.
Nội dung và mã mẫu trên trang này phải tuân thủ các giấy phép như mô tả trong phần Giấy phép nội dung. Java và OpenJDK là nhãn hiệu hoặc nhãn hiệu đã đăng ký của Oracle và/hoặc đơn vị liên kết của Oracle.
Cập nhật lần gần đây nhất: 2025-08-23 UTC.
[null,null,["Cập nhật lần gần đây nhất: 2025-08-23 UTC."],[],[],null,["# Radio button\n\nA [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-----------\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---------------------------\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/dd30aee903e8c247786c064faab1a9ca8d10b46e/compose/snippets/src/main/java/com/example/compose/snippets/components/RadioButton.kt#L39-L70\n```\n\n\u003cbr /\u003e\n\n### Key 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\n### Result\n\n**Figure 2.** Three radio buttons with the \"Friends\" option selected.\n\nAdditional resources\n--------------------\n\n- Material Design: [Buttons](https://m3.material.io/components/buttons/overview)"]]