קל לארגן דפים בעזרת אוספים
אפשר לשמור ולסווג תוכן על סמך ההעדפות שלך.
רכיב הסנאקבר משמש כהתראה קצרה שמופיעה בחלק התחתון של המסך. הוא מספק משוב על פעולה בלי להפריע לחוויית המשתמש. ההודעות הקצרות נעלמות אחרי כמה שניות. המשתמש יכול גם לסגור אותן באמצעות פעולה, כמו הקשה על לחצן.
הנה שלושה תרחישים לדוגמה שבהם כדאי להשתמש בחטיף:
אישור פעולה: אחרי שמשתמש מוחק אימייל או הודעה, מופיעה הודעה קצרה כדי לאשר את הפעולה ומוצעת האפשרות 'ביטול'.
סטטוס הרשת: כשהחיבור לאינטרנט של האפליקציה מתנתק, מופיעה הודעה קצרה שמודיעה שהאפליקציה במצב אופליין.
שליחת נתונים: אחרי שליחת טופס או עדכון הגדרות, מוצגת הודעה קצרה שמאשרת שהשינוי נשמר בהצלחה.
איור 1. דוגמאות לשימוש בבר חטיפים.
דוגמה בסיסית
כדי להטמיע סרגל חטיפים, קודם יוצרים SnackbarHost, שכולל מאפיין SnackbarHostState. SnackbarHostState מספק גישה לפונקציה showSnackbar() שבה אפשר להשתמש כדי להציג את חלונית החטיף.
פונקציית ההשעיה הזו דורשת CoroutineScope, כמו במקרה של שימוש ב-rememberCoroutineScope. אפשר להפעיל אותה בתגובה לאירועים בממשק המשתמש כדי להציג Snackbar בתוך Scaffold.
אפשר לספק פעולה אופציונלית ולשנות את משך הזמן של Snackbar.
הפונקציה snackbarHostState.showSnackbar() מקבלת פרמטרים נוספים של actionLabel
ו-duration, ומחזירה SnackbarResult.
valscope=rememberCoroutineScope()valsnackbarHostState=remember{SnackbarHostState()}Scaffold(snackbarHost={SnackbarHost(hostState=snackbarHostState)},floatingActionButton={ExtendedFloatingActionButton(text={Text("Show snackbar")},icon={Icon(Icons.Filled.Image,contentDescription="")},onClick={scope.launch{valresult=snackbarHostState.showSnackbar(message="Snackbar",actionLabel="Action",// Defaults to SnackbarDuration.Shortduration=SnackbarDuration.Indefinite)when(result){SnackbarResult.ActionPerformed->{/* Handle snackbar action performed */}SnackbarResult.Dismissed->{/* Handle snackbar dismissed */}}}})}){contentPadding->
// Screen content}
actionLabel = "Action": הגדרת הטקסט של לחצן הפעולה.
withDismissAction = true: מוצג צלב בחלונית המידע. מקישים עליו כדי לסגור את ההודעה הקצרה.
duration = SnackbarDuration.Indefinite: שומר את ההודעה בתחתית המסך עד שהמשתמש או התוכנית סוגרים אותה.
SnackbarResult.ActionPerformed: מציין שהמשתמש לחץ על לחצן הפעולה של חטיף המידע.
SnackbarResult.Dismissed: מציין שהמשתמש סגר את ההודעה הקצרה בלי ללחוץ על לחצן הפעולה.
דוגמאות התוכן והקוד שבדף הזה כפופות לרישיונות המפורטים בקטע רישיון לתוכן. 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 [snackbar component](https://m3.material.io/components/snackbar/overview) serves as a brief notification that appears at the\nbottom of the screen. It provides feedback about an operation or action without\ninterrupting the user experience. Snackbars disappear after a few seconds. The\nuser can also dismiss them with an action, such as tapping a button.\n\nConsider these three use cases where you might use a snackbar:\n\n- **Action Confirmation:** After a user deletes an email or message, a snackbar appears to confirm the action and offer an \"Undo\" option.\n- **Network Status:** When the app loses its internet connection, a snackbar pops up to note that it is now offline.\n- **Data Submission:** Upon successfully submitting a form or updating settings, a snackbar notes that the change has saved successfully.\n\n**Figure 1.** Snackbar examples.\n\nBasic example\n\nTo implement a snackbar, you first create [`SnackbarHost`](/reference/kotlin/androidx/compose/material3/package-summary#SnackbarHost(androidx.compose.material3.SnackbarHostState,androidx.compose.ui.Modifier,kotlin.Function1)), which includes a\n[`SnackbarHostState`](/reference/kotlin/androidx/compose/material3/SnackbarHostState) property. `SnackbarHostState` provides access to the\n[`showSnackbar()`](/reference/kotlin/androidx/compose/material3/SnackbarHostState#showsnackbar) function which you can use to display your snackbar.\n\nThis suspending function requires a `CoroutineScope` such as with using\n[`rememberCoroutineScope`](/reference/kotlin/androidx/compose/runtime/package-summary#remembercoroutinescope) --- and can be called in response to UI events to\nshow a [`Snackbar`](/reference/kotlin/androidx/compose/material3/package-summary#snackbar) within `Scaffold`.\n\n\n```kotlin\nval scope = rememberCoroutineScope()\nval snackbarHostState = remember { SnackbarHostState() }\nScaffold(\n snackbarHost = {\n SnackbarHost(hostState = snackbarHostState)\n },\n floatingActionButton = {\n ExtendedFloatingActionButton(\n text = { Text(\"Show snackbar\") },\n icon = { Icon(Icons.Filled.Image, contentDescription = \"\") },\n onClick = {\n scope.launch {\n snackbarHostState.showSnackbar(\"Snackbar\")\n }\n }\n )\n }\n) { contentPadding -\u003e\n // Screen content\n}https://github.com/android/snippets/blob/5673ffc60b614daf028ee936227128eb8c4f9781/compose/snippets/src/main/java/com/example/compose/snippets/layouts/MaterialLayoutSnippets.kt#L218-L240\n```\n\n\u003cbr /\u003e\n\nSnackbar with action\n\nYou can provide an optional action and adjust the duration of the `Snackbar`.\nThe `snackbarHostState.showSnackbar()` function accepts additional `actionLabel`\nand `duration` parameters, and returns a [`SnackbarResult`](/reference/kotlin/androidx/compose/material3/SnackbarResult).\n\n\n```kotlin\nval scope = rememberCoroutineScope()\nval snackbarHostState = remember { SnackbarHostState() }\nScaffold(\n snackbarHost = {\n SnackbarHost(hostState = snackbarHostState)\n },\n floatingActionButton = {\n ExtendedFloatingActionButton(\n text = { Text(\"Show snackbar\") },\n icon = { Icon(Icons.Filled.Image, contentDescription = \"\") },\n onClick = {\n scope.launch {\n val result = snackbarHostState\n .showSnackbar(\n message = \"Snackbar\",\n actionLabel = \"Action\",\n // Defaults to SnackbarDuration.Short\n duration = SnackbarDuration.Indefinite\n )\n when (result) {\n SnackbarResult.ActionPerformed -\u003e {\n /* Handle snackbar action performed */\n }\n SnackbarResult.Dismissed -\u003e {\n /* Handle snackbar dismissed */\n }\n }\n }\n }\n )\n }\n) { contentPadding -\u003e\n // Screen content\n}https://github.com/android/snippets/blob/5673ffc60b614daf028ee936227128eb8c4f9781/compose/snippets/src/main/java/com/example/compose/snippets/layouts/MaterialLayoutSnippets.kt#L247-L283\n```\n\n\u003cbr /\u003e\n\nYou can provide a custom `Snackbar` with the `snackbarHost` parameter. See the\n[`SnackbarHost` API reference docs](/reference/kotlin/androidx/compose/material/package-summary#snackbarhost) for more information.\n\nKey points\n\n- `actionLabel = \"Action\"`: Sets the action button text.\n- `withDismissAction = true`: Provides a cross in the snackbar. Tap it to dismiss the snackbar.\n- `duration = SnackbarDuration.Indefinite`: Keeps the snackbar displayed until the user or program dismisses it.\n- `SnackbarResult.ActionPerformed`: Signifies that the user clicked the snackbar's action button.\n- `SnackbarResult.Dismissed`: Signifies that the user dismissed the snackbar without clicking the action button."]]