鸿蒙通用组件弹窗简介
创始人
2024-11-10 05:09:54
0次

鸿蒙通用组件弹窗简介

  • 弹窗----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()     })   } } 

相关内容

热门资讯

正版辅助器技巧!智星菠萝德州多... 正版辅助器技巧!智星菠萝德州多功能有开挂-其实有挂秘籍1、智星菠萝德州透视辅助软件激活码多个强度级别...
正版作弊器插件!AAPoKer... 正版作弊器插件!AAPoKer多功能有开挂-好像有挂秘诀1、点击下载安装,AAPoKer脚本分类一目...
正版作弊器脚本!AAPoKer... 正版作弊器脚本!AAPoKer多功能存在有开挂-果然有挂方式1、完成AAPoKer辅助器v3.3的残...
正版透视挂app!wpk多功能... 正版透视挂app!wpk多功能有开挂-其实有挂助手1、wpk免费脚本咨询教程、wpk免费脚本弱密码等...
正版作弊器工具!wpk多功能有... 正版作弊器工具!wpk多功能有开挂-总是有人有挂1)wpk免费钻石:进一步探索wpk免费脚本大陆,与...
正版作弊器app!aapoke... 正版作弊器app!aapoker多功能真的是有开挂-真是有挂分享aapoker辅助器是一种具有地方特...
正版透视挂方法!aapoker... 正版透视挂方法!aapoker多功能有开挂-本来有挂细节1、下载好aapoker脚本下载之后点击打开...
正版外挂技巧!WePoKer多... 正版外挂技巧!WePoKer多功能真的是有开挂-其实有挂神器1、游戏颠覆性的策略玩法,独创攻略技巧玩...
正版辅助方法!wpk多功能存在... 正版辅助方法!wpk多功能存在有开挂-竟然有挂秘笈wpk透视方法中分为三种模型:wpk靠谱、wpk辅...
正版辅助器器!智星德州扑克多功... 正版辅助器器!智星德州扑克多功能有开挂-一贯有挂方式1)智星德州扑克免费钻石:进一步探索智星德州扑克...