สร้างการแจ้งเตือนด้วยแถบข้อมูลเล็กๆ
จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน
บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
คอมโพเนนต์ Snackbar ทำหน้าที่เป็นการแจ้งเตือนสั้นๆ ที่ปรากฏที่ด้านล่างของหน้าจอ โดยแสดงผลป้อนกลับเกี่ยวกับการดำเนินการหรือการดำเนินการโดยไม่ขัดจังหวะประสบการณ์ของผู้ใช้ แซนคบาร์จะหายไปหลังจากผ่านไป 2-3 วินาที นอกจากนี้ผู้ใช้ยังปิดการแจ้งเตือนด้วยการดำเนินการต่างๆ เช่น การแตะปุ่มได้ด้วย
ลองพิจารณากรณีการใช้งาน 3 รูปแบบต่อไปนี้ที่คุณอาจใช้แถบข้อมูล
- การยืนยันการดำเนินการ: หลังจากผู้ใช้ลบอีเมลหรือข้อความแล้ว แถบข้อมูลเล็กจะปรากฏขึ้นเพื่อยืนยันการดำเนินการและเสนอตัวเลือก "เลิกทำ"
- สถานะเครือข่าย: เมื่อแอปไม่มีการเชื่อมต่ออินเทอร์เน็ต แบนเนอร์ข้อมูลข่าวสารจะปรากฏขึ้นเพื่อแจ้งว่าแอปออฟไลน์
- การส่งข้อมูล: เมื่อส่งแบบฟอร์มหรืออัปเดตการตั้งค่าเรียบร้อยแล้ว แบนเนอร์ข้อมูลแจ้งจะแสดงว่าบันทึกการเปลี่ยนแปลงเรียบร้อยแล้ว
ความเข้ากันได้ของเวอร์ชัน
การติดตั้งใช้งานนี้กำหนดให้ต้องตั้งค่า minSDK ของโปรเจ็กต์เป็น API ระดับ 21 ขึ้นไป
ทรัพยากร Dependency
สร้างแถบข้อมูลข่าวสารพื้นฐาน
หากต้องการใช้แถบข้อมูล ให้สร้าง SnackbarHost
ก่อน ซึ่งจะมีพร็อพเพอร์ตี้ SnackbarHostState
SnackbarHostState
ให้สิทธิ์เข้าถึงฟังก์ชัน showSnackbar()
ซึ่งคุณใช้เพื่อแสดง Snackbar ได้
ฟังก์ชันการระงับนี้ต้องใช้ CoroutineScope
เช่น การใช้ rememberCoroutineScope
และสามารถเรียกให้แสดงเพื่อตอบสนองต่อเหตุการณ์ UI เพื่อแสดง Snackbar
ภายใน Scaffold
สร้างแถบข้อมูลพร้อมการดำเนินการ
คุณสามารถระบุการดำเนินการที่ไม่บังคับและปรับระยะเวลาของ Snackbar
ได้
ฟังก์ชัน snackbarHostState.showSnackbar()
ยอมรับพารามิเตอร์ actionLabel
และ duration
เพิ่มเติม และแสดงผล SnackbarResult
คุณสามารถระบุ Snackbar
ที่กําหนดเองได้โดยใช้พารามิเตอร์ snackbarHost
ดูข้อมูลเพิ่มเติมได้ที่เอกสารอ้างอิงSnackbarHost
API
ผลลัพธ์
รูปที่ 1 การแจ้งเตือนแบบแถบข้อมูลที่มีการดำเนินการ
คอลเล็กชันที่มีคู่มือนี้
คู่มือนี้เป็นส่วนหนึ่งของคอลเล็กชันคู่มือฉบับย่อที่มีการดูแลจัดการ ซึ่งครอบคลุมเป้าหมายการพัฒนา Android ที่กว้างขึ้น ดังนี้
แสดงคอมโพเนนต์แบบอินเทอร์แอกทีฟ
ดูวิธีที่ฟังก์ชันคอมโพสิเบิลช่วยให้คุณสร้างคอมโพเนนต์ UI ที่สวยสะดุดตาตามระบบการออกแบบ Material Design ได้อย่างง่ายดาย
ตัวอย่างเนื้อหาและโค้ดในหน้าเว็บนี้ขึ้นอยู่กับใบอนุญาตที่อธิบายไว้ในใบอนุญาตการใช้เนื้อหา Java และ OpenJDK เป็นเครื่องหมายการค้าหรือเครื่องหมายการค้าจดทะเบียนของ Oracle และ/หรือบริษัทในเครือ
อัปเดตล่าสุด 2025-02-06 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-02-06 UTC"],[],[],null,["\u003cbr /\u003e\n\nThe [snackbar component](https://material.io/components/snackbars) 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\nVersion compatibility\n\nThis implementation requires that your project minSDK be set to API level 21\nor higher.\n\nDependencies\n\nCreate a basic snackbar\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\nCreate a snackbar 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\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\nResults\n\n\n**Figure 1.** Snackbar notifications with action.\n\n\u003cbr /\u003e\n\nCollections that contain this guide\n\nThis guide is part of these curated Quick Guide collections that cover\nbroader Android development goals: \n\nDisplay interactive components \nLearn how composable functions can enable you to easily create beautiful UI components based on the Material Design design system. \n[Quick guide collection](/develop/ui/compose/quick-guides/collections/display-interactive-components) \n\nHave questions or feedback \nGo to our frequently asked questions page and learn about quick guides or reach out and let us know your thoughts. \n[Go to FAQ](/quick-guides/faq) [Leave feedback](https://issuetracker.google.com/issues/new?component=1573691&template=1993320)"]]