@ComposablefunCheckboxMinimalExample(){varcheckedbyremember{mutableStateOf(true)}Row(verticalAlignment=Alignment.CenterVertically,){Text("Minimal checkbox")Checkbox(checked=checked,onCheckedChange={checked=it})}Text(if(checked)"Checkbox is checked"else"Checkbox is unchecked")}
@ComposablefunCheckboxParentExample(){// Initialize states for the child checkboxesvalchildCheckedStates=remember{mutableStateListOf(false,false,false)}// Compute the parent state based on children's statesvalparentState=when{childCheckedStates.all{it}->ToggleableState.OnchildCheckedStates.none{it}->ToggleableState.Offelse->ToggleableState.Indeterminate}Column{// Parent TriStateCheckboxRow(verticalAlignment=Alignment.CenterVertically,){Text("Select all")TriStateCheckbox(state=parentState,onClick={// Determine new state based on current statevalnewState=parentState!=ToggleableState.OnchildCheckedStates.forEachIndexed{index,_->
childCheckedStates[index]=newState}})}// Child CheckboxeschildCheckedStates.forEachIndexed{index,checked->
Row(verticalAlignment=Alignment.CenterVertically,){Text("Option ${index+1}")Checkbox(checked=checked,onCheckedChange={isChecked->
// Update the individual child statechildCheckedStates[index]=isChecked})}}}if(childCheckedStates.all{it}){Text("All options selected")}}
[null,null,["最后更新时间 (UTC):2025-08-23。"],[],[],null,["# Checkboxes let users select one or more items from a list. You might use a\ncheckbox to let the user do the following:\n\n- Turn an item on or off.\n- Select from multiple options in a list.\n- Indicate agreement or acceptance.\n\n| **Note:** Use checkboxes instead of [switches](/develop/ui/compose/components/switch) or [radio buttons](https://m3.material.io/components/radio-button) if the user can select multiple options in a list.\n\nAnatomy\n-------\n\nA checkbox consists of the following elements:\n\n- **Box**: This is the container for the checkbox.\n- **Check**: This is the visual indicator that shows whether the checkbox is selected or not.\n- **Label**: This is the text that describes the checkbox.\n\nStates\n------\n\nA checkbox can be in one of three states:\n\n- **Unselected**: The checkbox is not selected. The box is empty.\n- **Indeterminate**: The checkbox is in an indeterminate state. The box contains a dash.\n- **Selected**: The checkbox is selected. The box contains a checkmark.\n\nThe following image demonstrates the three states of a checkbox.\n**Figure 1.** The three states of a checkbox. Unselected, indeterminate, and selected.\n\nImplementation\n--------------\n\nYou can use the [`Checkbox`](/reference/kotlin/androidx/compose/material3/package-summary#Checkbox(kotlin.Boolean,kotlin.Function1,androidx.compose.ui.Modifier,kotlin.Boolean,androidx.compose.material3.CheckboxColors,androidx.compose.foundation.interaction.MutableInteractionSource)) composable to create a checkbox in your app.\nThere are just a few key parameters to keep in mind:\n\n- `checked`: The boolean that captures whether the checkbox is checked or unchecked.\n- `onCheckedChange()`: The function that the app calls when the user taps the checkbox.\n\nThe following snippet demonstrates how to use the `Checkbox` composable:\n\n\n```kotlin\n@Composable\nfun CheckboxMinimalExample() {\n var checked by remember { mutableStateOf(true) }\n\n Row(\n verticalAlignment = Alignment.CenterVertically,\n ) {\n Text(\n \"Minimal checkbox\"\n )\n Checkbox(\n checked = checked,\n onCheckedChange = { checked = it }\n )\n }\n\n Text(\n if (checked) \"Checkbox is checked\" else \"Checkbox is unchecked\"\n )\n}https://github.com/android/snippets/blob/dd30aee903e8c247786c064faab1a9ca8d10b46e/compose/snippets/src/main/java/com/example/compose/snippets/components/Checkbox.kt#L62-L81\n```\n\n\u003cbr /\u003e\n\n### Explanation\n\nThis code creates a checkbox that is initially unchecked. When the user clicks\non the checkbox, the `onCheckedChange` lambda updates the `checked` state.\n\n### Result\n\nThis example produces the following component when unchecked:\n**Figure 2.** Unchecked checkbox\n\nAnd this is how the same checkbox appears when checked:\n**Figure 3.** Checked checkbox\n\nAdvanced example\n----------------\n\nThe following is a more complex example of how you can implement checkboxes in\nyour app. In this snippet, there is a parent checkbox and a series of child\ncheckboxes. When the user taps the parent checkbox, the app checks all child\ncheckboxes.\n\n\n```kotlin\n@Composable\nfun CheckboxParentExample() {\n // Initialize states for the child checkboxes\n val childCheckedStates = remember { mutableStateListOf(false, false, false) }\n\n // Compute the parent state based on children's states\n val parentState = when {\n childCheckedStates.all { it } -\u003e ToggleableState.On\n childCheckedStates.none { it } -\u003e ToggleableState.Off\n else -\u003e ToggleableState.Indeterminate\n }\n\n Column {\n // Parent TriStateCheckbox\n Row(\n verticalAlignment = Alignment.CenterVertically,\n ) {\n Text(\"Select all\")\n TriStateCheckbox(\n state = parentState,\n onClick = {\n // Determine new state based on current state\n val newState = parentState != ToggleableState.On\n childCheckedStates.forEachIndexed { index, _ -\u003e\n childCheckedStates[index] = newState\n }\n }\n )\n }\n\n // Child Checkboxes\n childCheckedStates.forEachIndexed { index, checked -\u003e\n Row(\n verticalAlignment = Alignment.CenterVertically,\n ) {\n Text(\"Option ${index + 1}\")\n Checkbox(\n checked = checked,\n onCheckedChange = { isChecked -\u003e\n // Update the individual child state\n childCheckedStates[index] = isChecked\n }\n )\n }\n }\n }\n\n if (childCheckedStates.all { it }) {\n Text(\"All options selected\")\n }\n}https://github.com/android/snippets/blob/dd30aee903e8c247786c064faab1a9ca8d10b46e/compose/snippets/src/main/java/com/example/compose/snippets/components/Checkbox.kt#L86-L136\n```\n\n\u003cbr /\u003e\n\n### Explanation\n\nThe following are several points you should note from this example:\n\n- **State management** :\n - `childCheckedStates`: A list of booleans using `mutableStateOf()` to track the checked state of each child checkbox.\n - `parentState`: A [`ToggleableState`](/reference/kotlin/androidx/compose/ui/state/ToggleableState?_gl=1*1nllj9c*_up*MQ..*_ga*MTQ4MjE3NjI1Ny4xNzE1MzM1Nzc0*_ga_6HH9YJMN9M*MTcxNTMzNTc3NC4xLjAuMTcxNTMzNTc3NC4wLjAuMA..) whose value derives from the child checkboxes' states.\n- **UI components** :\n - [`TriStateCheckbox`](/reference/kotlin/androidx/compose/material3/package-summary#TriStateCheckbox(androidx.compose.ui.state.ToggleableState,kotlin.Function0,androidx.compose.ui.Modifier,kotlin.Boolean,androidx.compose.material3.CheckboxColors,androidx.compose.foundation.interaction.MutableInteractionSource)): Is necessary for the parent checkbox as it has a `state` param that lets you set it to indeterminate.\n - [`Checkbox`](/reference/kotlin/androidx/compose/material3/package-summary#Checkbox(kotlin.Boolean,kotlin.Function1,androidx.compose.ui.Modifier,kotlin.Boolean,androidx.compose.material3.CheckboxColors,androidx.compose.foundation.interaction.MutableInteractionSource)): Used for each child checkbox with its state linked to the corresponding element in `childCheckedStates`.\n - `Text`: Displays labels and messages (\"Select all\", \"Option X\", \"All options selected\").\n- **Logic** :\n - The parent checkbox's `onClick` updates all child checkboxes to the opposite of the current parent state.\n - Each child checkbox's `onCheckedChange` updates its corresponding state in the `childCheckedStates` list.\n - The code displays \"`All options selected`\" when all child checkboxes are checked.\n\n| **Note:** If you want to have a checkbox with an indeterminate state, you must use `TriStateCheckbox`. This is because it has a `state` parameter of type `ToggleableState`, whereas `Checkbox` does not.\n\n### Result\n\nThis example produces the following component when all checkboxes are unchecked.\n**Figure 4.** Unchecked checkboxes\n\nLikewise, this is how the component appears when all options are checked, as\nwhen the user taps select all:\n**Figure 5.** Checked checkboxes\n\nWhen only one option is checked the parent checkbox display the indeterminate\nstate:\n**Figure 6.** Indeterminate checkbox\n\n### Additional resources\n\n- [Material Design Checkboxes](https://m3.material.io/components/checkbox/overview)"]]