当前位置: 首页 > news >正文

全局UI方法-弹窗六-自定义弹窗

1、描述

        通过CustomDialogController类显示自定义弹窗。使用弹窗组件时,可优先考虑自定义弹窗,便于自定义弹窗的样式与内容。

2、接口

CustomDialogController(value:{builder: CustomDialog, cancel?: () => void, autoCancel?: boolean, alignment?: DialogAlignment, offset?: Offset, customStyle?: boolean, gridCount?: number})

3、参数

参数名

参数类型

必填

参数说明

builder

CustomDialog

自定义弹窗内容构造器。

cancel() => void点击遮障层退出时的回调。
autoCancelboolean

是否允许点击遮障层退出。

默认值:true

alignmentDialogAlignment

弹窗在竖直方向上的对齐方式。

默认值:DialogAlignment.Default

offsetOffset弹窗相对alignment所在位置的偏移量。
customStyleboolean

弹窗容器样式是否自定义。

默认值:false,弹窗容器的宽度根据栅格系统自适应,不跟随子节点;高度自适应子节点,最大为窗口高度的90%;圆角为24vp。

gridCountnumber弹窗宽度占栅格宽度的个数。默认为按照窗口大小自适应,异常值按默认值处理,最大栅格数为系统最大栅格数。

4、DialogAlignment枚举说明:

名称

描述

Top

垂直顶部对齐。

Center垂直居中对齐。
Bottom垂直底部对齐。
Default默认对齐。
TopStart左上对齐。
TopEnd右上对齐。
CenterStart左中对齐。
CenterEnd右中对齐。
BottomStart左下对齐。
BottomEnd右下对齐。

5、Offset

名称

类型

必填

说明

dx

Length

水平方向偏移量。

dy

Length

竖直方向偏移量。

6、CustomDialogController

导入对象:

dialogController : CustomDialogController = new CustomDialogController(value:{builder: CustomDialog, cancel?: () => void, autoCancel?: boolean})

说明:

CustomDialogController仅在作为@CustomDialog和@Component struct的成员变量,且在@Component struct内部定义时赋值才有效,具体用法可看下方示例。

open()

open(): void

显示自定义弹窗内容,允许多次使用,但如果弹框为SubWindow模式,则该弹框不允许再弹出SubWindow弹框。

close

close(): void

关闭显示的自定义弹窗,若已关闭,则不生效。

7、示例

@CustomDialog
struct CustomDialogExample {@Link textValue: string@Link inputValue: stringcontroller: CustomDialogController// 若尝试在CustomDialog中传入多个其他的Controller,以实现在CustomDialog中打开另一个或另一些CustomDialog,那么此处需要将指向自己的controller放在最后cancel: () => voidconfirm: () => voidbuild() {Column() {Text('Change text').fontSize(20).margin({ top: 10, bottom: 10 })TextInput({ placeholder: '', text: this.textValue }).height(60).width('90%').onChange((value: string) => {this.textValue = value})Text('Whether to change a text?').fontSize(16).margin({ bottom: 10 })Flex({ justifyContent: FlexAlign.SpaceAround }) {Button('cancel').onClick(() => {this.controller.close()this.cancel()}).backgroundColor(0xffffff).fontColor(Color.Black)Button('confirm').onClick(() => {this.inputValue = this.textValuethis.controller.close()this.confirm()}).backgroundColor(0xffffff).fontColor(Color.Red)}.margin({ bottom: 10 })}// dialog默认的borderRadius为24vp,如果需要使用border属性,请和borderRadius属性一起使用。}
}@Entry
@Component
struct CustomDialogPage {@State message: string = '通过CustomDialogController类显示自定义弹窗。使用弹窗组件时,可优先考虑自定义弹窗,便于自定义弹窗的样式与内容。'@State textValue: string = ''@State inputValue: string = 'click me'dialogController: CustomDialogController = new CustomDialogController({builder: CustomDialogExample({cancel: this.onCancel,confirm: this.onAccept,textValue: $textValue,inputValue: $inputValue}),cancel: this.existApp,autoCancel: true,alignment: DialogAlignment.Bottom,offset: { dx: 0, dy: -20 },gridCount: 4,customStyle: false})// 在自定义组件即将析构销毁时将dialogController置空aboutToDisappear() {this.dialogController = undefined // 将dialogController置空}onCancel() {console.info('Callback when the first button is clicked')}onAccept() {console.info('Callback when the second button is clicked')}existApp() {console.info('Click the callback in the blank area')}build() {Row() {Column() {Text(this.message).fontSize(20).fontWeight(FontWeight.Bold).width("96%").margin({ top: 12 })Button(this.inputValue).onClick(() => {if (this.dialogController != undefined) {this.dialogController.open()}})}.width('100%').height("100%")}.height('100%')}
}

8、效果图

相关文章:

  • Flask 与小程序 的图片数据交互 过程及探讨研究学习
  • 如何在群晖NAS搭建bitwarden密码管理软件并实现无公网IP远程访问
  • Install Docker
  • 【机器学习】代价函数
  • #Linux(make工具和makefile文件以及makefile语法)
  • spark: 从pulsar中读取数据
  • tcpdump 抓包
  • 基于STELLA系统动态模拟技术及在农业、生态及环境科学中的应用教程
  • WINDOWS设置代理链chain
  • 一文整合工厂模式、模板模式、策略模式
  • 什么是通配符SSL证书?
  • Webgl学习系列-认识Webgl
  • 一、TLE9471 - SBC Mode切换 + VCC2 开关
  • 百度谷歌301强引蜘蛛池效果怎么样
  • 项目中配置多个阿里巴巴矢量图库方案
  • 【跃迁之路】【699天】程序员高效学习方法论探索系列(实验阶段456-2019.1.19)...
  • 11111111
  • 4个实用的微服务测试策略
  • Bootstrap JS插件Alert源码分析
  • ComponentOne 2017 V2版本正式发布
  • crontab执行失败的多种原因
  • github指令
  • IDEA 插件开发入门教程
  • JS+CSS实现数字滚动
  • Linux后台研发超实用命令总结
  • MySQL常见的两种存储引擎:MyISAM与InnoDB的爱恨情仇
  • Rancher-k8s加速安装文档
  • vue 个人积累(使用工具,组件)
  • Webpack4 学习笔记 - 01:webpack的安装和简单配置
  • 阿里云应用高可用服务公测发布
  • 测试如何在敏捷团队中工作?
  • 动态规划入门(以爬楼梯为例)
  • 基于MaxCompute打造轻盈的人人车移动端数据平台
  • 计算机常识 - 收藏集 - 掘金
  • 我从编程教室毕业
  • 限制Java线程池运行线程以及等待线程数量的策略
  • 小程序01:wepy框架整合iview webapp UI
  • [Shell 脚本] 备份网站文件至OSS服务(纯shell脚本无sdk) ...
  • Java性能优化之JVM GC(垃圾回收机制)
  • ​queue --- 一个同步的队列类​
  • ​无人机石油管道巡检方案新亮点:灵活准确又高效
  • ​云纳万物 · 数皆有言|2021 七牛云战略发布会启幕,邀您赴约
  • # Panda3d 碰撞检测系统介绍
  • #《AI中文版》V3 第 1 章 概述
  • #NOIP 2014# day.2 T2 寻找道路
  • #stm32驱动外设模块总结w5500模块
  • #每天一道面试题# 什么是MySQL的回表查询
  • #我与Java虚拟机的故事#连载03:面试过的百度,滴滴,快手都问了这些问题
  • %3cli%3e连接html页面,html+canvas实现屏幕截取
  • (42)STM32——LCD显示屏实验笔记
  • (LeetCode C++)盛最多水的容器
  • (附源码)springboot码头作业管理系统 毕业设计 341654
  • (力扣记录)235. 二叉搜索树的最近公共祖先
  • (免费领源码)python+django+mysql线上兼职平台系统83320-计算机毕业设计项目选题推荐
  • (亲测)设​置​m​y​e​c​l​i​p​s​e​打​开​默​认​工​作​空​间...