JetpackCompose从入门到实战学习笔记7—Dialog的简单使用
创始人
2024-05-24 14:35:06
0

JetpackCompose从入门到实战学习笔记7—Dialog的简单使用

1.Dialog对话框

Dialog的参数如下:

@Composable
fun Dialog(onDismissRequest: (() -> Unit)?, //关闭对话框的回调properties: DialogProperties! = DialogProperties(), //自定义对话框的属性content: (@ Compose () -> Unit) ?//对话框的内容    
):Unitproperties 参数定制一些对话框特有的行为:
DialogProperties(dismissOnBackPress: Boolean!, //是否在按下系统返回键的时候取消对话框dismissOnClickOutside: Boolean!, //是否可以在点击对话框以外的区域取消对话框securePolicy: SecurePolicy!,userPlatformDefaultWidth: Boolean! //对话框的内容是否需要被限制在平台默认的范围内
)

2.Dialog的简单示例:

    @OptIn(ExperimentalComposeUiApi::class)@Composablefun dialogDemo() {val openDialog = remember {mutableStateOf(true)}val dialogWidth = 300.dpval dialogHeight = 100.dpif (openDialog.value) {Dialog(onDismissRequest = { openDialog.value = false },properties = DialogProperties(usePlatformDefaultWidth = false),) {Box(Modifier.size(dialogWidth,dialogHeight).background(Color.White).padding(10.dp),contentAlignment = Alignment.Center){Text(text = "这是一个Compose实现的dialog", color = Color.Red)}}}}

3.Dialog效果如下:

在这里插入图片描述

4.AlertDialog简介:

AlertDialog组件是Dialog组件的更高级别的封装,同时遵循着Material Design 设计标准。它已经帮我们定位好了标题、内容文本、按钮组的位置。

5.AlertDialog的使用示例:

@Preview@Composablefun AlertDialogSample() {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 = "取消")}})}}

6.AlertDialog的实现效果如下:

在这里插入图片描述

7.完整代码如下:

package com.example.composemodifiterdemoimport android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.material.AlertDialog
import androidx.compose.material.Text
import androidx.compose.material.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.window.Dialog
import androidx.compose.ui.window.DialogProperties/***@author: njb*@date:  2023/2/7 19:27*@desc:*/
class DialogSampleActivity : ComponentActivity() {override fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)setContent {dialogDemo()AlertDialogSample()}}@Preview@OptIn(ExperimentalComposeUiApi::class)@Composablefun dialogDemo() {val openDialog = remember {mutableStateOf(true)}val dialogWidth = 300.dpval dialogHeight = 100.dpif (openDialog.value) {Dialog(onDismissRequest = { openDialog.value = false },properties = DialogProperties(usePlatformDefaultWidth = false),) {Box(Modifier.size(dialogWidth,dialogHeight).background(Color.White).padding(10.dp),contentAlignment = Alignment.Center){Text(text = "这是一个Compose实现的dialog", color = Color.Red)}}}}@Preview@Composablefun AlertDialogSample() {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 = "取消")}})}}
}

8.完整的效果预览如下:

在这里插入图片描述

相关内容

热门资讯

监控摄像头接入GB28181平... 流程简介将监控摄像头的视频在网站和APP中直播,要解决的几个问题是:1&...
Windows10添加群晖磁盘... 在使用群晖NAS时,我们需要通过本地映射的方式把NAS映射成本地的一块磁盘使用。 通过...
protocol buffer... 目录 目录 什么是protocol buffer 1.protobuf 1.1安装  1.2使用...
在Word、WPS中插入AxM... 引言 我最近需要写一些文章,在排版时发现AxMath插入的公式竟然会导致行间距异常&#...
【PdgCntEditor】解... 一、问题背景 大部分的图书对应的PDF,目录中的页码并非PDF中直接索引的页码...
Fluent中创建监测点 1 概述某些仿真问题,需要创建监测点,用于获取空间定点的数据࿰...
educoder数据结构与算法...                                                   ...
MySQL下载和安装(Wind... 前言:刚换了一台电脑,里面所有东西都需要重新配置,习惯了所...
修复 爱普生 EPSON L4... L4151 L4153 L4156 L4158 L4163 L4165 L4166 L4168 L4...
MFC文件操作  MFC提供了一个文件操作的基类CFile,这个类提供了一个没有缓存的二进制格式的磁盘...