【发布时间】:2021-11-13 03:01:26
【问题描述】:
下面用于显示 Compose Snackbar 的简单代码
此代码在 onClick 事件发生时正确显示 Snackbar。
val scaffoldState = rememberScaffoldState() // this contains the `SnackbarHostState`
val coroutineScope = rememberCoroutineScope()
Scaffold(
modifier = Modifier,
scaffoldState = scaffoldState // attaching `scaffoldState` to the `Scaffold`
) {
Button(
onClick = {
coroutineScope.launch { // using the `coroutineScope` to `launch` showing the snackbar
// taking the `snackbarHostState` from the attached `scaffoldState`
val snackbarResult = scaffoldState.snackbarHostState.showSnackbar(
message = "This is your message",
actionLabel = "Do something."
)
when (snackbarResult) {
SnackbarResult.Dismissed -> Log.d("SnackbarDemo", "Dismissed")
SnackbarResult.ActionPerformed -> Log.d("SnackbarDemo", "Snackbar's button clicked")
}
}
}
) {
Text(text = "A button that shows a Snackbar")
}
}
如何在左右滑动时关闭小吃栏?
【问题讨论】:
-
嗨。我的回答解决了你的问题吗?如果是这样,请使用投票柜台下的复选标记接受它。否则,如果您有任何问题,请告诉我。
标签: android android-jetpack-compose android-snackbar