تنظيم صفحاتك في مجموعات
يمكنك حفظ المحتوى وتصنيفه حسب إعداداتك المفضّلة.
يتيح عنصر Switch للمستخدمين التبديل بين حالتين: تم وضع علامة
في المربّع أو لم يتم وضع علامة فيه. في تطبيقك، يمكنك استخدام مفتاح تبديل للسماح للمستخدم بتنفيذ أحد الإجراءات التالية:
فعِّل أحد الإعدادات أو أوقِفه.
تفعيل ميزة أو إيقافها
حدِّد خيارًا.
يتكوّن هذا العنصر من جزأين: الإبهام والمسار. الإبهام هو الجزء القابل للسحب في زر التبديل، أما المسار فهو الخلفية. يمكن للمستخدم سحب الإبهام إلى اليسار أو اليمين لتغيير حالة مفتاح التبديل. يمكنهم أيضًا النقر على زر التبديل للتحقّق من المربّع وإزالة العلامة منه.
الشكل 1. مكوِّن مفتاح التبديل
التنفيذ الأساسي
اطّلِع على مرجع Switch للحصول على تعريف كامل لواجهة برمجة التطبيقات. في ما يلي بعض المَعلمات الرئيسية التي قد تحتاج إلى استخدامها:
checked: تمثّل هذه السمة الحالة الأولية للمفتاح.
onCheckedChange: دالة ردّ يتم استدعاؤها عند تغيُّر حالة زر التبديل.
enabled: ما إذا كان مفتاح التبديل مفعَّلاً أو غير مفعَّل.
colors: الألوان المستخدَمة للمفتاح
المثال التالي هو عملية تنفيذ بسيطة للدالة البرمجية القابلة للإنشاء Switch.
يخضع كل من المحتوى وعيّنات التعليمات البرمجية في هذه الصفحة للتراخيص الموضحّة في ترخيص استخدام المحتوى. إنّ Java وOpenJDK هما علامتان تجاريتان مسجَّلتان لشركة Oracle و/أو الشركات التابعة لها.
تاريخ التعديل الأخير: 2025-08-30 (حسب التوقيت العالمي المتفَّق عليه)
[[["يسهُل فهم المحتوى.","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-30 (حسب التوقيت العالمي المتفَّق عليه)"],[],[],null,["The [`Switch`](/reference/kotlin/androidx/compose/material3/package-summary#Switch(kotlin.Boolean,kotlin.Function1,androidx.compose.ui.Modifier,kotlin.Function0,kotlin.Boolean,androidx.compose.material3.SwitchColors,androidx.compose.foundation.interaction.MutableInteractionSource)) component allows users to toggle between two states: checked\nand unchecked. In your app you may use a switch to let the user to do one of the\nfollowing:\n\n- Toggle a setting on or off.\n- Enable or disable a feature.\n- Select an option.\n\nThe component has two parts: the thumb and the track. The thumb is the draggable\npart of the switch, and the track is the background. The user can drag the thumb\nto the left or right to change the state of the switch. They can also tap the\nswitch to check and clear it.\n**Figure 1.** The switch component.\n\nBasic implementation\n\nSee the [`Switch`](/reference/kotlin/androidx/compose/material3/package-summary#Switch(kotlin.Boolean,kotlin.Function1,androidx.compose.ui.Modifier,kotlin.Function0,kotlin.Boolean,androidx.compose.material3.SwitchColors,androidx.compose.foundation.interaction.MutableInteractionSource)) reference for a full API definition. The following are\nsome of the key parameters you might need to use:\n\n- **`checked`**: The initial state of the switch.\n- **`onCheckedChange`**: A callback that is called when the state of the switch changes.\n- **`enabled`**: Whether the switch is enabled or disabled.\n- **`colors`**: The colors used for the switch.\n\nThe following example is a minimal implementation of the `Switch` composable.\n\n\n```kotlin\n@Composable\nfun SwitchMinimalExample() {\n var checked by remember { mutableStateOf(true) }\n\n Switch(\n checked = checked,\n onCheckedChange = {\n checked = it\n }\n )\n}https://github.com/android/snippets/blob/f95ab59fad80aeaf5d6a90bab8a01a126f20f44e/compose/snippets/src/main/java/com/example/compose/snippets/components/Switch.kt#L65-L75\n```\n\n\u003cbr /\u003e\n\nThis implementation appears as follows when unchecked:\n**Figure 2.** An unchecked switch.\n\nThis is the appearance when checked:\n**Figure 3.** A checked switch.\n\nAdvanced implementation\n\nThe primary parameters you might want to use when implementing a more advanced\nswitch are the following:\n\n- **`thumbContent`**: Use this to customize the appearance of the thumb when it is checked.\n- **`colors`**: Use this to customize the color of the track and thumb.\n\nCustom thumb\n\nYou can pass any composable for the `thumbContent` parameter to create a custom\nthumb. The following is an example of a switch that uses a custom icon for its\nthumb:\n\n\n```kotlin\n@Composable\nfun SwitchWithIconExample() {\n var checked by remember { mutableStateOf(true) }\n\n Switch(\n checked = checked,\n onCheckedChange = {\n checked = it\n },\n thumbContent = if (checked) {\n {\n Icon(\n imageVector = Icons.Filled.Check,\n contentDescription = null,\n modifier = Modifier.size(SwitchDefaults.IconSize),\n )\n }\n } else {\n null\n }\n )\n}https://github.com/android/snippets/blob/f95ab59fad80aeaf5d6a90bab8a01a126f20f44e/compose/snippets/src/main/java/com/example/compose/snippets/components/Switch.kt#L80-L101\n```\n\n\u003cbr /\u003e\n\nIn this implementation, the unchecked appearance is the same as the example in\nthe preceding section. However, when checked, this implementation appears as\nfollows:\n**Figure 4.** A switch with a custom checked icon.\n\nCustom colors\n\nThe following example demonstrates how you can use the colors parameter to\nchange the color of a switch's thumb and track, taking into account whether the\nswitch is checked.\n\n\n```kotlin\n@Composable\nfun SwitchWithCustomColors() {\n var checked by remember { mutableStateOf(true) }\n\n Switch(\n checked = checked,\n onCheckedChange = {\n checked = it\n },\n colors = SwitchDefaults.colors(\n checkedThumbColor = MaterialTheme.colorScheme.primary,\n checkedTrackColor = MaterialTheme.colorScheme.primaryContainer,\n uncheckedThumbColor = MaterialTheme.colorScheme.secondary,\n uncheckedTrackColor = MaterialTheme.colorScheme.secondaryContainer,\n )\n )\n}https://github.com/android/snippets/blob/f95ab59fad80aeaf5d6a90bab8a01a126f20f44e/compose/snippets/src/main/java/com/example/compose/snippets/components/Switch.kt#L129-L145\n```\n\n\u003cbr /\u003e\n\nThis implementation appears as follows:\n**Figure 5.** A switch with custom colors.\n\nAdditional resources\n\n- [Material UI docs](https://m3.material.io/components/switch/overview)"]]