با مجموعهها، منظم بمانید
ذخیره و طبقهبندی محتوا براساس اولویتهای شما.
مولفه Switch به کاربران اجازه می دهد تا بین دو حالت جابجا شوند: علامت زده و بدون علامت. در برنامه خود می توانید از یک سوئیچ استفاده کنید تا به کاربر اجازه دهید یکی از موارد زیر را انجام دهد:
یک تنظیم را روشن یا خاموش کنید.
یک ویژگی را فعال یا غیرفعال کنید.
یک گزینه را انتخاب کنید.
کامپوننت دارای دو بخش است: شست و مسیر. انگشت شست قسمت قابل کشیدن سوئیچ است و آهنگ پس زمینه است. کاربر می تواند انگشت شست را به سمت چپ یا راست بکشد تا وضعیت سوئیچ را تغییر دهد. آنها همچنین می توانند برای بررسی و پاک کردن سوئیچ ضربه بزنند.
شکل 1. جزء سوئیچ.
پیاده سازی اساسی
برای تعریف کامل API به مرجع Switch مراجعه کنید. برخی از پارامترهای کلیدی که ممکن است نیاز به استفاده از آنها داشته باشید به شرح زیر است:
checked : وضعیت اولیه سوئیچ.
onCheckedChange : یک تماس برگشتی که زمانی فراخوانی می شود که وضعیت سوئیچ تغییر کند.
این پیاده سازی با برداشتن علامت به صورت زیر ظاهر می شود:
شکل 2. یک سوئیچ بدون علامت.
این ظاهر هنگام بررسی است:
شکل 3. یک سوئیچ چک شده.
پیاده سازی پیشرفته
پارامترهای اولیه ای که ممکن است بخواهید هنگام اجرای سوئیچ پیشرفته تر استفاده کنید به شرح زیر است:
thumbContent : از این برای سفارشی کردن ظاهر انگشت شست هنگام علامت زدن استفاده کنید.
colors : از این برای سفارشی کردن رنگ مسیر و انگشت شست استفاده کنید.
انگشت شست سفارشی
شما می توانید هر composable را برای پارامتر thumbContent برای ایجاد یک شست سفارشی ارسال کنید. در زیر نمونه ای از سوئیچ است که از یک نماد سفارشی برای انگشت شست خود استفاده می کند:
در این پیاده سازی، ظاهر بدون علامت مانند مثال در قسمت قبل است. با این حال، هنگامی که بررسی می شود، این پیاده سازی به صورت زیر ظاهر می شود:
شکل 4. یک سوئیچ با نماد علامت گذاری شده سفارشی.
رنگ های سفارشی
مثال زیر نشان میدهد که چگونه میتوانید از پارامتر رنگها برای تغییر رنگ انگشت شست و مسیر سوئیچ استفاده کنید، با در نظر گرفتن اینکه آیا سوئیچ بررسی شده است یا خیر.
محتوا و نمونه کدها در این صفحه مشمول پروانههای توصیفشده در پروانه محتوا هستند. جاوا و 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,["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/7a0ebbee11495f628cf9d574f6b6069c2867232a/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/7a0ebbee11495f628cf9d574f6b6069c2867232a/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/7a0ebbee11495f628cf9d574f6b6069c2867232a/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)"]]