קל לארגן דפים בעזרת אוספים
אפשר לשמור ולסווג תוכן על סמך ההעדפות שלך.
רכיב Switch מאפשר למשתמשים לעבור בין שני מצבים: מסומן ולא מסומן. באפליקציה שלכם, אתם יכולים להשתמש במתג כדי לאפשר למשתמשים לבצע אחת מהפעולות הבאות:
מפעילים או משביתים את ההגדרה.
מפעילים או משביתים תכונה.
בוחרים באחת מהאפשרויות.
הרכיב מורכב משני חלקים: האגודל והמסלול. החלק שאפשר לגרור הוא האגודל, והרקע הוא המסילה. המשתמש יכול לגרור את האגודל שמאלה או ימינה כדי לשנות את מצב המתג. הם יכולים גם להקיש על המתג כדי לסמן את התיבה ולבטל את הסימון שלה.
איור 1. רכיב המתג.
הטמעה בסיסית
הגדרה מלאה של ה-API מופיעה בהפניה Switch. אלה כמה מהפרמטרים העיקריים שבהם תצטרכו להשתמש:
checked: המצב ההתחלתי של המתג.
onCheckedChange: פונקציית קריאה חוזרת שמופעלת כשמצב המתג משתנה.
דוגמאות התוכן והקוד שבדף הזה כפופות לרישיונות המפורטים בקטע רישיון לתוכן. Java ו-OpenJDK הם סימנים מסחריים או סימנים מסחריים רשומים של חברת Oracle ו/או של השותפים העצמאיים שלה.
עדכון אחרון: 2025-08-27 (שעון UTC).
[[["התוכן קל להבנה","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-27 (שעון UTC)."],[],[],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/5673ffc60b614daf028ee936227128eb8c4f9781/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/5673ffc60b614daf028ee936227128eb8c4f9781/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/5673ffc60b614daf028ee936227128eb8c4f9781/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)"]]