با مجموعهها، منظم بمانید
ذخیره و طبقهبندی محتوا براساس اولویتهای شما.
چک باکس ها به کاربران اجازه می دهند یک یا چند مورد را از یک لیست انتخاب کنند. ممکن است از یک چک باکس استفاده کنید تا به کاربر اجازه دهید کارهای زیر را انجام دهد:
یک مورد را روشن یا خاموش کنید.
از بین چندین گزینه در یک لیست انتخاب کنید.
موافقت یا پذیرش را نشان دهید.
آناتومی
یک چک باکس از عناصر زیر تشکیل شده است:
کادر : این ظرف برای چک باکس است.
بررسی : این نشانگر بصری است که نشان میدهد آیا کادر انتخاب انتخاب شده است یا خیر.
برچسب : این متنی است که چک باکس را توصیف می کند.
ایالات
یک چک باکس می تواند در یکی از سه حالت باشد:
انتخاب نشده : کادر انتخاب انتخاب نشده است. جعبه خالی است.
نامشخص : چک باکس در حالت نامشخص است. جعبه حاوی یک خط تیره است.
انتخاب شده : کادر انتخاب انتخاب شده است. کادر حاوی یک علامت است.
تصویر زیر سه حالت یک چک باکس را نشان می دهد.
شکل 1. سه حالت یک چک باکس. انتخاب نشده، نامشخص و انتخاب شده.
پیاده سازی
میتوانید از Checkbox Composable برای ایجاد یک چک باکس در برنامه خود استفاده کنید. فقط چند پارامتر کلیدی وجود دارد که باید در نظر داشت:
checked : بولی که نشان می دهد که چک باکس علامت زده شده یا بدون علامت.
onCheckedChange() : عملکردی که برنامه با ضربه زدن روی چک باکس آن را فراخوانی می کند.
قطعه زیر نحوه استفاده از Checkbox Composable را نشان می دهد:
@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")}
این کد یک چک باکس ایجاد می کند که در ابتدا تیک آن را بردارید. هنگامی که کاربر بر روی چک باکس کلیک می کند، لامبدا onCheckedChange وضعیت checked را به روز می کند.
نتیجه
این مثال وقتی تیک آن را بردارید، مؤلفه زیر را تولید می کند:
شکل 2. چک باکس را بردارید
و به این صورت است که همان چک باکس هنگام علامت زدن ظاهر می شود:
شکل 3. چک باکس علامت زده شده است
نمونه پیشرفته
در زیر یک مثال پیچیده تر از نحوه پیاده سازی چک باکس ها در برنامه خود آورده شده است. در این قطعه یک چک باکس والدین و یک سری چک باکس فرزند وجود دارد. هنگامی که کاربر روی کادر چک والد ضربه می زند، برنامه همه چک باکس های فرزند را بررسی می کند.
@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")}}
محتوا و نمونه کدها در این صفحه مشمول پروانههای توصیفشده در پروانه محتوا هستند. جاوا و OpenJDK علامتهای تجاری یا علامتهای تجاری ثبتشده Oracle و/یا وابستههای آن هستند.
تاریخ آخرین بهروزرسانی 2025-08-28 بهوقت ساعت هماهنگ جهانی.
[[["درک آسان","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-28 بهوقت ساعت هماهنگ جهانی."],[],[],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\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\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\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/7a0ebbee11495f628cf9d574f6b6069c2867232a/compose/snippets/src/main/java/com/example/compose/snippets/components/Checkbox.kt#L62-L81\n```\n\n\u003cbr /\u003e\n\nExplanation\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\nResult\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\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/7a0ebbee11495f628cf9d574f6b6069c2867232a/compose/snippets/src/main/java/com/example/compose/snippets/components/Checkbox.kt#L86-L136\n```\n\n\u003cbr /\u003e\n\nExplanation\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\nResult\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\nAdditional resources\n\n- [Material Design Checkboxes](https://m3.material.io/components/checkbox/overview)"]]