Jetpack Compose 入门:AlertDialog

onDismissRequest` 点击 AlertDialog 之外的屏幕,一般情况下是 AlertDialog 消失;confirmButton 确认按钮;dismissButton 取消按钮;title 标题;text 内容区域,这里面可以放置 Text`、TextField`等各种组件,而不仅仅是 Text;

hefengbao 发布于 2023.09.17 ,最后更新于 2023.09.18

AlertDialog 是 material3 组件 androidx.compose.material3.AlertDialog, 用于显示对话框。

onDismissRequest : 点击 AlertDialog 之外的屏幕,一般情况下是 AlertDialog 消失;

confirmButton 确认按钮;

dismissButton 取消按钮;

title 标题;

text 内容区域,这里面可以放置 TextTextField 等各种组件,而不仅仅是 Text;

示例代码:

@Composable
fun AlertDialogScreen(
    modifier: Modifier = Modifier
) {
    var showDialog by remember { mutableStateOf(false) }

    Button(onClick = { showDialog = true }) {
        Text(text = "显示对话框")
    }

    if (showDialog){
        AlertDialog(
            modifier = modifier,
            onDismissRequest = { showDialog = false },
            confirmButton = {
                Button(onClick = { showDialog = false }) {
                    Text(text = "确认")
                }
            },
            dismissButton = {
                TextButton(onClick = { showDialog = false }) {
                    Text(text = "取消")
                }
            },
            icon = {
                Icon(imageVector = Icons.Default.PrivacyTip, contentDescription = null)
            },
            title = {
                Text(text = "用户协议")
            },
            text = {
                Column {
                    Text(
                        text = """
                        1、关于我们;
                        2、账号安全;
                        3、隐私政策
                        4、生效时间
                    """.trimIndent()
                    )
                    
                    Row(
                        verticalAlignment = Alignment.CenterVertically
                    ) {
                        Checkbox(checked = false, onCheckedChange = {})
                        Text(text = "请阅读并同意用于协议")
                    }
                }
            },
        )
    }
}

Demo:https://github.com/hefengbao/jetpack-compose-demo ,获取代码:

git clone git@github.com:hefengbao/jetpack-compose-demo.git

Android    Android   Jetpack Compose Tutorial  

hefengbao

暂无个人简介

有0条评论

发表评论

您的电子邮箱地址不会被公开。 必填项已用 * 标注

来源:

https://www.8ug.icu/articles/jetpack-compose-tutorial-alertdialog-OZDb4Gp0WN