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

鸿蒙通用组件弹窗简介

鸿蒙通用组件弹窗简介

  • 弹窗----Toast
    • 引入@ohos.promptAction模块
    • 通过点击按钮,模拟弹窗
  • 警告对话框----AlertDialog
  • 列表弹窗----ActionSheet
  • 选择器弹窗
  • 自定义弹窗
    • 使用@CustomDialog声明一个自定义弹窗
    • 在需要使用的地方声明
    • 自定义弹窗,完整代码

弹窗----Toast

在这里插入图片描述

引入@ohos.promptAction模块

import promptAction from '@ohos.promptAction'

通过点击按钮,模拟弹窗

Button('点击').width('60%').height(50).onClick(()=>{promptAction.showToast({message:'点击了按钮', // 展示的文字duration:2000, // 停留时长bottom: 100 // 距离底部多远})})

警告对话框----AlertDialog

AlertDialog用于向用户提出告警或者确认的对话框。

在这里插入图片描述

AlertDialog.show({title:'提示信息',message:'此信息比较重要,请确认!!!',autoCancel: true, //点击遮障层时,是否关闭弹窗alignment: DialogAlignment.Bottom, //弹窗位置offset: { dx: 0, dy: -30 }, //相对于弹窗位置的偏移量primaryButton: { //主要按钮value: '确认', //按钮内容fontColor: Color.Red, //字体颜色action: () => { //点击回调console.log('点击了确认按钮')}},secondaryButton: { //次要按钮value: '取消',action: () => {console.log('点击了取消按钮')}},cancel: () => { //点击遮罩层取消时的回调console.info('点击遮罩层取消时的回调')}
})

列表弹窗----ActionSheet

ActionSheet用于给用户一组列表弹窗,等用户选择后再作处理。

在这里插入图片描述

ActionSheet.show({
title:'提示信息',
message: '此信息比较重要,请确认!!!',
autoCancel: true, //点击遮障层时,是否关闭弹窗
alignment: DialogAlignment.Center, //弹窗位置
offset: { dx: 0, dy: -20 }, //弹窗相对alignment位置的偏移量
confirm: { //底部按钮value: '取消', //按钮文本内容action: () => { //按钮回调函数console.log('点击按钮取消')}
},
cancel: () => { //点击遮障层关闭弹窗时的回调console.log('点击遮障层取消')
},
sheets:[{title:'操作1',action: () => {console.log('操作1')}},{title:'操作2',action: () => {console.log('操作2')}},{title:'操作3',action: () => {console.log('操作3')}},
]
})

alignment的几种位置:

DialogAlignment.Top 上部
DialogAlignment.Center 中间
DialogAlignment.Bottom 底部
DialogAlignment.TopStart 左上部
DialogAlignment.TopEnd 右上部
DialogAlignment.CenterStart 中间左边
DialogAlignment.CenterEnd 中间右边
DialogAlignment.BottomStart 左下部
DialogAlignment.BottomEnd 右下部

选择器弹窗

鸿蒙API中有很多选择器弹窗,比如时间、日期、文本等,这里就不做介绍。

自定义弹窗

在实际使用过程中,一定会遇到官方提供的弹窗不能满足需要的情况,这时候就需要尽心自定义。

这里举一个自定义弹窗的简单例子

使用@CustomDialog声明一个自定义弹窗

@CustomDialog
struct CustomDialogText{controller: CustomDialogController = new CustomDialogController({ builder: CustomDialogText() })build(){Column({space: 20}){Text('内容1')Text('内容2')Text('内容3')}.margin(20).onClick(()=>{this.controller.close()})}
}

在build里面定义UI样式,这里只做为展示,随便定义了几个展示的文本内容

在需要使用的地方声明

在使用过程中,需要需要初始化CustomDialogController。

controller: CustomDialogController = new CustomDialogController({builder: CustomDialogText(),// builder就是自定义的弹窗alignment: DialogAlignment.Center, // 弹窗弹出位置offset:{dx: 0, dy: -10} // 弹窗弹出后的偏移量,按需要进行设置
})

定义好controller之后,在使用的时候,直接调用this.controller.open()。

在这里插入图片描述

自定义弹窗,完整代码

@Entry
@Component
struct CommentTest{controller: CustomDialogController = new CustomDialogController({builder: CustomDialogText(),alignment: DialogAlignment.Center,offset:{dx: 0, dy: -10}})build(){Column({space: 20}){Button('点击弹出自定义弹窗').width('60%').height(50).onClick(()=>{this.controller.open()})}.width('100%').height('100%').justifyContent(FlexAlign.Center)}
}@CustomDialog
struct CustomDialogText{controller: CustomDialogController = new CustomDialogController({ builder: CustomDialogText() })build(){Column({space: 20}){Text('内容1')Text('内容2')Text('内容3')}.margin(20).onClick(()=>{this.controller.close()})}
}
http://www.lryc.cn/news/343080.html

相关文章:

  • [译文] 恶意代码分析:1.您记事本中的内容是什么?受感染的文本编辑器notepad++
  • Spring Boot3.x集成Disruptor4.0
  • GoEdge自建CDN工具
  • 牛客储物点的距离
  • 【C++历练之路】红黑树——map与set的封装实现
  • RDB快照是怎么实现的?
  • 智能体可靠性的革命性提升,揭秘知识工程领域的参考架构新篇章
  • Shell 初始化配置指北 | Ubuntu
  • [嵌入式系统-69]:RT-Thread-组件:网络组件“组”,RT-Thread系统通向外部网络世界的入口
  • Linux学习笔记1---Windows上运行Linux
  • Java算法-力扣leetcode-135. 分发糖果
  • 企业为什么需要主数据管理工具?十大热门主数据管理工具盘点
  • 免费思维13招之一:体验型思维
  • 面试C++(基础篇)-NULL与nullptr的区别?
  • 「AIGC」深度学习
  • mysql5.7数据库安装及性能测试
  • 聪明与诚实:社会信任的桥梁
  • 基于单片机的无线数据传输系统设计
  • 【IP:Internet Protocol,子网(Subnets),IPv6:动机,层次编址:路由聚集(rout aggregation)】
  • 智启算力平台基本操作
  • 微信小程序 【关键部分】
  • JavaEE技术之MySql高级(索引、索引优化、sql实战、View视图、Mysql日志和锁、多版本并发控制)
  • OCR文本识别模型CRNN
  • 【数据结构】闲谈A股实时交易的数据结构-队列
  • 深入探索van Emde Boas树:原理、操作与C语言实现
  • 正点原子[第二期]Linux之ARM(MX6U)裸机篇学习笔记-14-主频和时钟配置
  • tomcat打开乱码修改端口
  • 03 JavaSE-- 访问控制权限、接口、抽象类、内部类、Object类、异常
  • free5gc+ueransim操作
  • 麦肯锡精英高效阅读法笔记