Jetpack Compose 入门:AlertDialog

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

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

confirmButton 确认按钮;

dismissButton 取消按钮;

title 标题;

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

示例代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
@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