【发布时间】:2021-09-16 05:24:27
【问题描述】:
代码参考:https://foso.github.io/Jetpack-Compose-Playground/material/alertdialog/
@Composable
fun AlertDialogSample() {
MaterialTheme {
Column {
val openDialog = remember { mutableStateOf(false) }
Button(onClick = {
openDialog.value = true
}) {
Text("Click me")
}
if (openDialog.value) {
AlertDialog(
onDismissRequest = {
// Dismiss the dialog when the user clicks outside the dialog or on the back
// button. If you want to disable that functionality, simply use an empty
// onCloseRequest.
openDialog.value = false
},
title = {
Text(text = "Dialog Title")
},
text = {
Text("Here is a text ")
},
confirmButton = {
Button(
onClick = {
openDialog.value = false
}) {
Text("This is the Confirm Button")
}
},
dismissButton = {
Button(
onClick = {
openDialog.value = false
}) {
Text("This is the dismiss Button")
}
}
)
}
}
}
}
我已经了解了 remember 和 mutableStateOf 关键字。 但在这段代码中,我认为“记住”关键字不是必需的。
如果按钮被点击,变量 openDialog 实现 如果 onDismissRequest,变量 openDialog 为 false
所有功能都由代码编写者被动控制..(不是由记住关键字自动管理) 那么,为什么在这个代码案例中使用“remember”呢?
【问题讨论】:
标签: android kotlin android-jetpack-compose