网站首页 > 博客文章 正文
任何应用都离不开弹窗,今天我们就基于ArkTS开发语言,介绍下弹窗的实现方式。鸿蒙弹窗分为:
警告弹窗
警告弹窗AlertDialog,由标题、内容、操作按钮三部分组成,具体代码为:
Button('点击显示警告弹窗')
.onClick(() => {
AlertDialog.show(
{
title: '这是标题', // 标题
message: '这里是内容啊,这里是内容', // 内容
autoCancel: false, // 点击遮障层时,是否关闭弹窗。
alignment: DialogAlignment.Bottom, // 弹窗在竖直方向的对齐方式
offset: { dx: 0, dy: -20 }, // 弹窗相对alignment位置的偏移量
primaryButton: {
value: '取消',
action: () => {
console.info('Callback when the first button is clicked');
}
},
secondaryButton: {
value: '删除',
fontColor: '#D94838',
action: () => {
console.info('Callback when the second button is clicked');
}
},
cancel: () => { // 点击遮障层关闭dialog时的回调
console.info('Closed callbacks');
}
}
)
})
选择类弹窗
这类弹窗,多数应用于性别、出生日期、地区等选择。比如:
- TextPickerDialog
- DatePickerDialog
文本选择窗TextPickerDialog,代码为:
日期选择窗DatePickerDialog,代码为:
如果以上不能满足需求,我们还可以自定义自己的弹窗。
自定义弹窗
自定义弹窗的使用更加灵活,适用于更多的业务场景,在自定义弹窗中您可以自定义弹窗内容,构建更加丰富的弹窗界面。自定义弹窗的界面可以通过装饰器@CustomDialog定义的组件来实现,然后结合CustomDialogController来控制自定义弹窗的显示和隐藏。
上图可以看出,自定义弹窗其实是加载了一个自定义的构建结构体,也就是builder后面的结构体,本例中的AddTargetDialog,代码为:
import { CommonConstants } from '../common/constant/CommonConstant';
@Preview
@CustomDialog
export default struct AddTargetDialog {
private controller?: CustomDialogController;
private taskName: string = ''
private onClickOk ?: (value: string) => void;
build() {
Column() {
Text('添加子目录')
.width(CommonConstants.FULL_WIDTH)
.fontSize(20)
.fontWeight(CommonConstants.FONT_WEIGHT)
.fontColor('#182431')
.textAlign(TextAlign.Center)
TextInput({ placeholder: '请输入子目标名称' })
.placeholderColor(Color.Gray)
.placeholderFont({ size: 16 })
.caretColor(Color.Blue)
.backgroundColor('#0d182431')
.width(CommonConstants.FULL_WIDTH)
.height(CommonConstants.DIALOG_INPUT_HEIGHT)
.margin({ top: CommonConstants.DIALOG_INPUT_MARGIN })
.fontSize(16)
.onChange((value) => {
this.taskName = value
})
Blank()
Row() {
Button('取消')
.dialogButtonStyle()
.onClick(() => {
this.controller.close()
})
Divider().vertical(true)
Button('确定')
.dialogButtonStyle()
.onClick(() => {
if (this.onClickOk !== undefined) {
this.onClickOk(this.taskName)
}
})
}
.width(CommonConstants.DIALOG_OPERATION_WIDTH)
.height(CommonConstants.DIALOG_OPERATION_HEIGHT)
.justifyContent(FlexAlign.SpaceBetween)
}
.padding('24vp')
.width(CommonConstants.DIALOG_WIDTH)
.height('168vp')
.borderRadius(32)
.backgroundColor(Color.White)
}
}
@Extend(Button) function dialogButtonStyle() {
.fontSize(16)
.height(32)
.width(96)
.backgroundColor(Color.White)
.fontColor('#007dff')
}
看着很多,其实很简单,首先是Column布局,从上到下分为:标题、输入框、操作按钮。其实它就是一个UI组件,嵌入到了CustomDialogController这个容器中,形成了一个弹窗。至于其他的属性,和上面两种类型的弹窗都差不多。
原创不易,鉴于本人水平有限,文中如有错误之处欢迎大家指正。欢迎大家点个关注,我将继续为大家分享相关的技术内容。
猜你喜欢
- 2025-01-26 Qt——常用控件(qtcreator控件)
- 2025-01-26 Flet 布局控件:ft.Page(页面),是根容器,是控件的容器View
- 2025-01-26 33个非常实用的JavaScript一行代码,建议收藏
- 2025-01-26 XtraReport控件VS微软RDLC报表,谁更胜一筹?
- 2025-01-26 Google官方梳理,Android 多返回栈技术详解
- 2025-01-26 Python入坑系列-pyside6桌面开发之QmainWindow自定义标题栏
- 2025-01-26 「3D建模」3Dmax快捷键大全(3dmax建模常用快捷键)
- 2025-01-26 第3天 | 28天学会PyQt5,基本窗口
- 2025-01-26 有办法了!批量删除多个Word页眉页脚
- 2025-01-26 鸿蒙开发:实现popup弹窗(鸿蒙系统没弹幕)
你 发表评论:
欢迎- 367℃用AI Agent治理微服务的复杂性问题|QCon
- 360℃手把手教程「JavaWeb」优雅的SpringMvc+Mybatis整合之路
- 358℃初次使用IntelliJ IDEA新建Maven项目
- 351℃Maven技术方案最全手册(mavena)
- 348℃安利Touch Bar 专属应用,让闲置的Touch Bar活跃起来!
- 346℃InfoQ 2024 年趋势报告:架构篇(infoq+2024+年趋势报告:架构篇分析)
- 345℃IntelliJ IDEA 2018版本和2022版本创建 Maven 项目对比
- 342℃从头搭建 IntelliJ IDEA 环境(intellij idea建包)
- 最近发表
- 标签列表
-
- powershellfor (55)
- messagesource (56)
- aspose.pdf破解版 (56)
- promise.race (63)
- 2019cad序列号和密钥激活码 (62)
- window.performance (66)
- qt删除文件夹 (72)
- mysqlcaching_sha2_password (64)
- ubuntu升级gcc (58)
- nacos启动失败 (64)
- ssh-add (70)
- jwt漏洞 (58)
- macos14下载 (58)
- yarnnode (62)
- abstractqueuedsynchronizer (64)
- source~/.bashrc没有那个文件或目录 (65)
- springboot整合activiti工作流 (70)
- jmeter插件下载 (61)
- 抓包分析 (60)
- idea创建mavenweb项目 (65)
- vue回到顶部 (57)
- qcombobox样式表 (68)
- vue数组concat (56)
- tomcatundertow (58)
- pastemac (61)
本文暂时没有评论,来添加一个吧(●'◡'●)