Jetpack Compose——AlertDialog的简单使用
程序员文章站
2022-05-14 20:30:02
...
参数:
AlertDialog(
onDismissRequest: () -> Unit,//当用户点击对话框外部或者按下返回按钮的时候会执行。注意:点击对话框的关闭按钮时并不会执行
buttons: @Composable () -> Unit,//可以自定义按钮的摆放位置及功能
modifier: Modifier = Modifier,//布局修饰
title: (@Composable () -> Unit)? = null,//对话框的标题
text: @Composable (() -> Unit)? = null,//对话框的内容
shape: Shape = MaterialTheme.shapes.medium,//对话框的形状
backgroundColor: Color = MaterialTheme.colors.surface,//对话框的背景色
contentColor: Color = contentColorFor(backgroundColor),//提供给其子级的首选内容颜色
properties: DialogProperties = DialogProperties()//用于进一步配置特定属性的对话框
)
AlertDialog(
onDismissRequest: () -> Unit,
confirmButton: @Composable () -> Unit,
modifier: Modifier = Modifier,//布局修饰
dismissButton: @Composable (() -> Unit)? = null,
title: @Composable (() -> Unit)? = null,//对话框的标题
text: @Composable (() -> Unit)? = null,//对话框的内容
shape: Shape = MaterialTheme.shapes.medium,//对话框的形状
backgroundColor: Color = MaterialTheme.colors.surface,//对话框的背景色
contentColor: Color = contentColorFor(backgroundColor),//提供给其子级的首选内容颜色
properties: DialogProperties = DialogProperties()//用于进一步配置特定属性的对话框
)
使用:
val openDialog = remember {
mutableStateOf(true)
}
if (openDialog.value) {
AlertDialog(
onDismissRequest = { openDialog.value = false },
title = { Text(text = "标题") },
text = {
Text(
text = "文本内容"
)
}, confirmButton = {
TextButton(onClick = { openDialog.value = false }) {
Text(text = "确认")
}
}, dismissButton = {
TextButton(onClick = { openDialog.value = false }) {
Text(text = "取消")
}
})
}
效果如图: