تنظيم صفحاتك في مجموعات
يمكنك حفظ المحتوى وتصنيفه حسب إعداداتك المفضّلة.
تتيح مربّعات الاختيار للمستخدمين اختيار عنصر واحد أو أكثر من القائمة. يمكنك استخدام
مربّع اختيار للسماح للمستخدم بإجراء ما يلي:
فعِّل عنصرًا أو أوقِفه.
الاختيار من بين خيارات متعدّدة في قائمة
الإشارة إلى الموافقة أو القبول
البنية
يتألف مربّع الاختيار من العناصر التالية:
المربّع: هو الحاوية التي تضمّ مربّع الاختيار.
مربّع الاختيار: هذا هو المؤشر المرئي الذي يوضّح ما إذا كان مربّع الاختيار
محدّدًا أم لا.
التصنيف: هو النص الذي يصف مربّع الاختيار.
الحالات
يمكن أن تكون خانة الاختيار في إحدى الحالات الثلاث التالية:
بلا علامة: لم يتم وضع علامة في مربّع الاختيار. المربّع فارغ.
غير محدّد: يكون مربع الاختيار في حالة غير محدّدة. يحتوي المربّع
على شرطة.
تم وضع علامة: تم وضع علامة في مربّع الاختيار. يحتوي المربّع على علامة اختيار.
توضّح الصورة التالية حالات مربّع الاختيار الثلاث.
الشكل 1. حالات مربّع الاختيار الثلاث غير محدّد ومحدّد وغير محدّد.
التنفيذ
يمكنك استخدام العنصر Checkbox القابل للإنشاء لإنشاء مربّع اختيار في تطبيقك.
هناك بضع مَعلمات رئيسية يجب وضعها في الاعتبار:
checked: القيمة المنطقية التي تحدّد ما إذا كان مربّع الاختيار محدّدًا أم لا.
onCheckedChange(): الدالة التي يستدعيها التطبيق عندما ينقر المستخدم على مربّع الاختيار.
يوضّح المقتطف التالي كيفية استخدام الدالة البرمجية القابلة للإنشاء Checkbox:
@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 Lambda الحالة 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")}}
يخضع كل من المحتوى وعيّنات التعليمات البرمجية في هذه الصفحة للتراخيص الموضحّة في ترخيص استخدام المحتوى. إنّ Java وOpenJDK هما علامتان تجاريتان مسجَّلتان لشركة Oracle و/أو الشركات التابعة لها.
تاريخ التعديل الأخير: 2025-08-23 (حسب التوقيت العالمي المتفَّق عليه)
[[["يسهُل فهم المحتوى.","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-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\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/5673ffc60b614daf028ee936227128eb8c4f9781/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/5673ffc60b614daf028ee936227128eb8c4f9781/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)"]]