唯客微博客

专注于计算机,嵌入式领域的技术

0%

Jetpack Compose组件-对话框(Dialog)

Jetpack Compose组件-对话框(Dialog)

警告对话框(AlertDialog)

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
@Composable
fun 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()
): Unit

@Composable
fun 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()
): Unit

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
val openDialog = remember { mutableStateOf(false)  }

Button(onClick = {
openDialog.value = true
}) {
Text("Click me")
}

if (openDialog.value) {
AlertDialog(
onDismissRequest = {
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")
}
}
)
}

对话框(Dialog)

AlertDialog 在一些情况下有可能还是无法满足我们的业务要求,这时候我们就可以使用更底层的一个 @Composable 函数 —— Dialog

1
2
3
4
5
6
7
8
@Composable
fun Dialog(
// 试图关闭对话框时执行
onDismissRequest: () -> Unit,
// 对话框属性
properties: DialogProperties = DialogProperties(),
content: @Composable () -> Unit
): Unit
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
var flag by remember{ mutableStateOf(false) }
Box(
modifier = Modifier.fillMaxSize(),
contentAlignment = Alignment.Center
) {
Button(
onClick = { flag = true }
) {
Text("弹窗")
}
}
if(flag) {
Dialog(
onDismissRequest = { flag = false }
) {
Box(
modifier = Modifier
.size(300.dp)
.background(Color.White),
contentAlignment = Alignment.Center
) {
Column {
LinearProgressIndicator()
Text("加载中 ing...")
}
}
}
}

弹出窗口(Popup)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
@Composable
fun Popup(
// 对其方式
alignment: Alignment = Alignment.TopStart,
// 偏移
offset: IntOffset = IntOffset(0, 0),
// 试图关闭窗口时执行
onDismissRequest: (() -> Unit)? = null,
// 弹出窗口属性
properties: PopupProperties = PopupProperties(),
content: @Composable () -> Unit
): Unit

@Composable
fun Popup(
// 弹出窗口的屏幕位置
popupPositionProvider: PopupPositionProvider,
// 试图关闭窗口时执行
onDismissRequest: (() -> Unit)? = null,
// 弹出窗口属性
properties: PopupProperties = PopupProperties(),
content: @Composable () -> Unit
): Unit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
Box {
val popupWidth = 200.dp
val popupHeight = 50.dp
val cornerSize = 16.dp

Popup(alignment = Alignment.Center) {
// Draw a rectangle shape with rounded corners inside the popup
Box(
Modifier
.size(popupWidth, popupHeight)
.background(Color.White, RoundedCornerShape(cornerSize))
)
}
}
-------------本文结束感谢您的阅读-------------

本文标题:Jetpack Compose组件-对话框(Dialog)

文章作者:Vinx

发布时间:2023年01月13日 - 10:46

最后更新:2023年09月19日 - 08:40

原始链接:https://blog.vinkvin.com/post/72/

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。