0
  • 聊天消息
  • 系统消息
  • 评论与回复
登录后你可以
  • 下载海量资料
  • 学习在线课程
  • 观看技术视频
  • 写文章/发帖/加入社区
会员中心
创作中心

完善资料让更多小伙伴认识你,还能领取20积分哦,立即完善>

3天内不再提示

在OpenHarmony上如何使用不同的弹窗

OpenHarmony技术社区 ? 来源:OST开源开发者 ? 2023-06-18 15:10 ? 次阅读
加入交流群
微信小助手二维码

扫码添加小助手

加入工程师交流群

应用中经常用到弹窗,比如警告弹窗、日期选择弹窗、文本选择弹窗以及其他自定义弹窗等等。本例将为大家介绍如何使用不同的弹窗。

效果呈现

本例最终效果如下:

83f5bbfe-0da6-11ee-962d-dac502259ad0.gif

示例中共涉及四类弹窗:

警告弹窗:提示信息尚未保存。

日期滑动选择器弹窗:选择出生日期。

文本滑动选择器弹窗:选择性别。

自定义弹窗:填写兴趣爱好。

说明:自定义弹窗可以根据业务需要自行定义弹窗的形式和内容,比如文本输入、单选、多选等等,本例以文本输入为例进行介绍。

运行环境

本例基于以下环境开发,开发者也可以基于其他适配的版本进行开发:

IDE:DevEco Studio 3.1 Release

SDK:Ohos_sdk_public 3.2.12.5(API Version 9 Release)

实现思路

本例中涉及的 4 类弹窗及实现方案如下:

警告弹窗:使用 AlertDialog 实现。

日期滑动选择器弹窗:使用 DatePickerDialog 实现。

文本滑动选择器弹窗:使用 TextPickerDialog 实现。

自定义弹窗:使用 CustomDialogController 实现。

开发步骤

由于本例重点讲解对话框的使用,所以开发步骤会着重讲解相关实现,不相关的内容不做介绍,全量代码可参考完整代码章节。

①首先,使用 AlertDialog 实现警告弹窗

通过 message 参数设置告警信息,alignment 设置弹窗在界面中垂直方向的对齐方式;通过 primaryButton 和 secondaryButton 添加按钮。

具体代码如下:

alertDialog(context:Context.UIAbilityContext){
AlertDialog.show({
//通过message设置告警信息
message:'当前数据未保存,是否确认离开?',
//通过alignment设置弹窗在界面垂直方向的对齐方式,此处设置为底部对齐
alignment:DialogAlignment.Bottom,
//通过offset设置基于对齐位置的便宜量
offset:{
dx:0,
dy:-20
},
//弹窗中左起第一个按钮
primaryButton:{
value:'取消',
action:()=>{
console.info('Callbackcancelbuttonisclicked');
}
},
//弹窗中左起第二个按钮
secondaryButton:{
value:'确定',
action:()=>{
//Exitingtheapp.
context.terminateSelf();
console.info('Callbackdefinitebuttonisclicked');
}
}
});
}
②使用 DatePickerDialog 实现日期滑动选择器弹窗

通过 start 和 end 分别设置日期区间的起始时间和末尾时间;通过 lunar 设置使用农历还是阳历;使用 onAccept 监听选择的日期,本例中通过变量 selectedDate 将选中的日期设置给参数 selected,这样弹窗弹出时的日期就默认为上次选中的日期。

具体代码如下:

datePickerDialog(dateCallback){
DatePickerDialog.show({
start:newDate('1900-1-1'),
end:newDate('2100-1-1'),
//通过变量selectedDate将选中的日期设置给参数selected
selected:this.selectedDate,
lunar:false,
//使用onAccept监听选择的日期
onAccept:(value:DatePickerResult)=>{
letyear=value.year;
letmonth=value.month+1;
letday=value.day;
letbirthdate:string=this.getBirthDateValue(year,month,day);
//通过setFullYear将选中的日期传递给变量selectedDate
this.selectedDate.setFullYear(value.year,value.month,value.day)
//返回选中的日期
dateCallback(birthdate);
}
});
}
③使用 TextPickerDialog 实现文本滑动选择器弹窗

通过 range 设置文本选择项,使用 onAccept 监听选择的文本项,本例中通过变量 selectedGender 将选中的性别的索引设置给参数 selected,这样弹窗弹出时的性别就默认为上次选中的性别。

具体代码如下:

textPickerDialog(sexArray:Resource,sexCallback){
//判断文本项的列表是否为空
if(this.isEmptyArr(sexArray)){
console.error('sexisnull');
return;
}
TextPickerDialog.show({
//通过range设置文本选择项
range:sexArray,
//通过变量selectedGender将选中的性别的索引设置给参数selected
selected:this.selectedGender,
//使用onAccept监听选择的文本项
onAccept:(result:TextPickerResult)=>{
sexCallback(result.value);
//获取选中项的索引
this.selectedGender=result.index
},
onCancel:()=>{
console.info('TextPickerDialogonCancel');
}
});
}

④使用 CustomDialogController 实现自定义弹窗

当现有弹窗不能满足业务诉求时,开发者可以自行设计弹窗的样式。在实现自定义弹窗时,需要将弹窗的 UI 放在被 @CustomDialog 修饰的自定义组件中,然后使用 CustomDialogController 的实例来控制弹窗的弹出和关闭。

具体代码如下:

//使用@CustomDialog修饰自定义弹窗
@CustomDialog
structCustomDialogFrame{
...
//定义CustomDialogController
controller:CustomDialogController

build(){
Column(){
Text('兴趣爱好').fontSize(20).margin({top:10,bottom:10})
TextInput({placeholder:'',text:this.textValue}).height(60).width('90%')
.onChange((value:string)=>{
this.textValue=value
})
Flex({justifyContent:FlexAlign.SpaceAround}){
Button('取消')
.onClick(()=>{
//点击‘取消’,弹窗关闭
this.controller.close()
})
.backgroundColor('')
.fontColor('#007DFF')
Button('保存')
.onClick(()=>{
this.inputValue=this.textValue
//点击‘保存’,弹窗关闭
this.controller.close()
})
.backgroundColor(0xffffff)
.fontColor('#007DFF')
}.margin({bottom:10})
}.justifyContent(FlexAlign.Start)
}
}
...
//实例化自定义弹窗
customDialogController:CustomDialogController=newCustomDialogController({
//使用上文创建的自定义弹窗进行实例化
builder:CustomDialogFrame({
textValue:$textValue,
inputValue:$inputValue
}),
alignment:DialogAlignment.Bottom,
offset:{
dx:0,
dy:-20
}
});
...


完整代码

本例完整代码如下:

importContextfrom'@ohos.app.ability.common';
importhilogfrom'@ohos.hilog';

@Component
structTextFrame{
@Linkcontent:string;
privatetextImage:Resource;
privatetext:string;
onTextClick:()=>void;

build(){
Row(){
Image(this.textImage)
.width(24)
.height(24)
.margin({left:12})
Text(this.text)
.fontSize(16)
.margin({left:12})
.height(24)
Text(this.content)
.fontSize(16)
.textAlign(TextAlign.End)
.textOverflow({overflow:TextOverflow.Ellipsis})
.maxLines(1)
.margin({
left:16,
right:7
})
.layoutWeight(1)
.width('100%')
Image($r('app.media.ic_arrow'))
.width(12)
.height(24)
.margin({right:14})
}
.margin({top:24})
.borderRadius(24)
.backgroundColor(Color.White)
.width('93.3%')
.height(64)
.onClick(this.onTextClick)
}
}

@Component
structInputFrame{
privateinputImage:Resource;
privatehintText:string;

build(){
Row(){
Image(this.inputImage)
.width(24)
.height(24)
.margin({left:12})
TextInput({placeholder:this.hintText})
.fontSize(16)
.padding({left:12})
.placeholderColor('#99000000')
.backgroundColor(Color.White)
.fontWeight(FontWeight.Normal)
.fontStyle(FontStyle.Normal)
.fontColor(Color.Black)
.margin({right:32})
.layoutWeight(1)
.height(48)
}
.margin({top:24})
.borderRadius(24)
.backgroundColor(Color.White)
.width('93.3%')
.height(64)
}
}

@CustomDialog
structCustomDialogFrame{
@LinktextValue:string
@LinkinputValue:string
controller:CustomDialogController

build(){
Column(){
Text('兴趣爱好').fontSize(20).margin({top:10,bottom:10})
TextInput({placeholder:'',text:this.textValue}).height(60).width('90%')
.onChange((value:string)=>{
this.textValue=value
})
Flex({justifyContent:FlexAlign.SpaceAround}){
Button('取消')
.onClick(()=>{
this.controller.close()
}).backgroundColor('').fontColor('#007DFF')
Button('保存')
.onClick(()=>{
this.inputValue=this.textValue
this.controller.close()
}).backgroundColor(0xffffff).fontColor('#007DFF')
}.margin({bottom:10})
}.justifyContent(FlexAlign.Start)
}
}

@Entry
@Component
structIndex{
@Statebirthdate:string='';
@Statesex:string='';
@StatetextValue:string='';
@StateinputValue:string='';
selectedDate:Date=newDate("2010-1-1")
selectedGender:number=0
privatesexArray:Resource=$r('app.strarray.sex_array');
customDialogController:CustomDialogController=newCustomDialogController({
builder:CustomDialogFrame({
textValue:$textValue,
inputValue:$inputValue
}),
alignment:DialogAlignment.Bottom,
offset:{
dx:0,
dy:-20
}
});

alertDialog(context:Context.UIAbilityContext){
AlertDialog.show({
message:'当前数据未保存,是否确认离开?',
alignment:DialogAlignment.Bottom,
offset:{
dx:0,
dy:-20
},
primaryButton:{
value:'取消',
action:()=>{
console.info('Callbackcancelbuttonisclicked');
}
},
secondaryButton:{
value:'确定',
action:()=>{
//Exitingtheapp.
context.terminateSelf();
console.info('Callbackdefinitebuttonisclicked');
}
}
});
}

datePickerDialog(dateCallback){
DatePickerDialog.show({
start:newDate('1900-1-1'),
end:newDate('2100-1-1'),
selected:this.selectedDate,
lunar:false,
onAccept:(value:DatePickerResult)=>{
letyear=value.year;
letmonth=value.month+1;
letday=value.day;
letbirthdate:string=this.getBirthDateValue(year,month,day);
this.selectedDate.setFullYear(value.year,value.month,value.day)
dateCallback(birthdate);
}
});
}

textPickerDialog(sexArray:Resource,sexCallback){
if(this.isEmptyArr(sexArray)){
console.error('sexisnull');
return;
}
TextPickerDialog.show({
range:sexArray,
selected:this.selectedGender,
onAccept:(result:TextPickerResult)=>{
sexCallback(result.value);
this.selectedGender=result.index
},
onCancel:()=>{
console.info('TextPickerDialogonCancel');
}
});
}

getBirthDateValue(year:number,month:number,day:number):string{
letbirthdate:string=`${year}${'年'}${month}`+
`${'月'}${day}${'日'}`;
returnbirthdate;
}

isEmpty(obj):boolean{
returnobj===undefined||obj===null||obj==='';
}

isEmptyArr(array):boolean{
returnthis.isEmpty(array)||array.length===0;
}

build(){
Row(){
Column(){
Row(){
Image($r('app.media.ic_back'))
.width(26)
.height(26)
.alignSelf(ItemAlign.Start)
.margin({
left:'7.2%',
top:19
})
.onClick(()=>{
letcontext=getContext(this)asContext.UIAbilityContext;
this.alertDialog(context);
})
Text('个人信息')
.fontColor(Color.Black)
.fontSize(20)
.margin({top:20,left:20})
.alignSelf(ItemAlign.Center)
}.width('100%')
Image($r('app.media.ic_avatar'))
.width(56)
.height(56)
.alignSelf(ItemAlign.Center)
.margin({top:'5.5%'})
Text('头像')
.fontColor(Color.Black)
.fontSize(16)
.margin({top:'2.1%'})
.alignSelf(ItemAlign.Center)
InputFrame({
inputImage:$r('app.media.ic_nickname'),
hintText:'昵称'
})
TextFrame({
textImage:$r('app.media.ic_birthdate'),
text:'出生日期',
content:$birthdate,
onTextClick:()=>{
this.datePickerDialog((birthValue:string)=>{
this.birthdate=birthValue;
});
}
})
TextFrame({
textImage:$r('app.media.ic_sex'),
text:'性别',
content:$sex,
onTextClick:()=>{
this.textPickerDialog(this.sexArray,(sexValue:string)=>{
this.sex=sexValue;
});
}
})
InputFrame({
inputImage:$r('app.media.ic_signature'),
hintText:'个性签名'
})
TextFrame({
textImage:$r('app.media.ic_hobbies'),
text:'兴趣爱好',
content:$textValue,
onTextClick:()=>{
this.customDialogController.open();
}
})
}
.backgroundColor('#F5F5F5')
.height('100%')
.width('100%')
}
.height('100%')
}
}



审核编辑:刘清

声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉
  • OpenHarmony
    +关注

    关注

    30

    文章

    3865

    浏览量

    18959

原文标题:OpenHarmony上使用弹窗

文章出处:【微信号:gh_834c4b3d87fe,微信公众号:OpenHarmony技术社区】欢迎添加关注!文章转载请注明出处。

收藏 人收藏
加入交流群
微信小助手二维码

扫码添加小助手

加入工程师交流群

    评论

    相关推荐
    热点推荐

    鸿蒙非侵入式弹窗新解法,企查查正式开源“QuickDialog”弹窗组件库

    近日,企查查将其自研的鸿蒙弹窗组件库“QuickDialog”开源,并上线至?OpenHarmony 三方库中心仓。这是鸿蒙生态首个支持“弹窗堆栈暂存能力”的非侵入式弹窗解决方案,凭借
    的头像 发表于 07-31 10:40 ?217次阅读
    鸿蒙非侵入式<b class='flag-5'>弹窗</b>新解法,企查查正式开源“QuickDialog”<b class='flag-5'>弹窗</b>组件库

    《仿盒马》app开发技术分享-- 分类模块顶部导航列表弹窗(16)

    技术栈 Appgallery connect 开发准备 一节我们实现了分类页面的顶部导航栏列表,并且实现了首页金刚区跟首页导航栏的联动,这一节我们实现导航栏列表的弹窗功能,需要学习的知识点有自定义
    发表于 06-30 10:34

    《仿盒马》app开发技术分享-- 商品规格弹窗(11)

    技术栈 Appgallery connect 开发准备 一节我们实现了商品详情页面,并且成功页面上展示了商品的图片、商品规格、活动详情等信息,要知道同一种商品大多数都是有多种型号跟规格的,所以
    发表于 06-30 09:15

    HarmonyOS实战:首页多弹窗顺序弹出终极解决方案

    背景 随着应用软件功能的不断增加,应用程序软件首页成为弹窗的重灾区,不仅有升级弹窗,还有积分弹窗,签到,引导等各种弹窗。为了彻底解弹窗问题,
    的头像 发表于 06-09 16:47 ?196次阅读
    HarmonyOS实战:首页多<b class='flag-5'>弹窗</b>顺序弹出终极解决方案

    如何在KaihongOS操作系统写一个弹窗组件

    写一个弹窗组件 KaihongOS框架提供了弹窗的API接口,开发者可直接使用,详情请参考@ohos.promptAction (弹窗)。但在开发过程中当提供的弹窗接口无法满足需求时
    发表于 04-30 06:44

    请问下,openharmony支持哪一款龙芯的开发板?有没有开源的龙芯的openharmony源码?

    想买个2k0300的开发板学习龙芯和openharmony,愣是没有看到提供openharmony源码的,也没与看到开源的代码。giteeopenharmony的龙芯sig仓库也是
    发表于 04-26 13:06

    DialogHub上线OpenHarmony开源社区,高效开发鸿蒙应用弹窗

    作为鸿蒙应用开发者,使用ArkUI现有能力进行弹窗开发时,总会遇到一些让人纠结的交互问题:应用内进行消息提示时,既要求消息内容支持图文混排,又要求弹窗本身不能打断用户交互(页面滑动、页面
    发表于 04-03 17:30

    STM32H5使用fatfs写函数时用不了DMA的写方式,应该怎么使用呢?

    请问STM32H5使用fatfs写函数时,用不了DMA的写方式,应该怎么使用呢,有人遇到过类似的问题嘛
    发表于 03-12 07:10

    OpenHarmony应用与游戏开发领域的前沿成果

    日前,由开放原子开源基金会主办的第二届OpenHarmony创新应用挑战赛决赛路演北京圆满结束,作为第二届开放原子大赛的重要赛项之一,本届赛事汇聚全球418支团队,产出超过110个创新作品,集中
    的头像 发表于 03-03 15:04 ?665次阅读

    蜂鸟板Openharmony系统跑QT程序

    将QT程序放到Openharmony系统跑,可以运行,但是会被覆盖掉。(用的网盘里面的install,支持QT组件的版本)。 运行情况是,终端运行QT程序,可以正常运行出来,但是触摸屏幕后,会被
    发表于 02-26 13:04

    鸿蒙原生开源库ViewPoolOpenHarmony社区正式上线

    近日,由伙伴参与共建的鸿蒙原生开源库“ViewPool”OpenHarmony社区正式上线。这个开发库是基于OpenHarmony技术孵化的成果,充分发挥了平台的技术特性,同时融入了伙伴
    的头像 发表于 12-20 14:44 ?681次阅读

    OpenHarmony人才生态大会南向生态社区发展论坛武汉圆满举办

    11月27日,OpenHarmony人才生态大会2024武汉隆重举行。当日下午的 OpenHarmony南向生态社区发展论坛(以下简称“论坛”),众多社区伙伴、企业代表、技术专家与
    的头像 发表于 11-29 10:06 ?670次阅读
    <b class='flag-5'>OpenHarmony</b>人才生态大会南向生态社区发展论坛<b class='flag-5'>在</b>武汉圆满举办

    OpenHarmony人才生态大会南向生态社区发展论坛武汉圆满举办

    11月27日,OpenHarmony人才生态大会2024武汉隆重举行。当日下午的 OpenHarmony南向生态社区发展论坛(以下简称“论坛”),众多社区伙伴、企业代表、技术专家与
    发表于 11-29 09:54

    基于ArkTS语言的OpenHarmony APP应用开发:HelloOpenharmony

    1、程序简介该程序是基于OpenHarmony标准系统编写的UI应用类:HelloOpenHarmony。本案例是基于API9接口开发。本案例已在OpenHarmony凌蒙派-RK3568开发
    的头像 发表于 09-15 08:09 ?927次阅读
    基于ArkTS语言的<b class='flag-5'>OpenHarmony</b> APP应用开发:Hello<b class='flag-5'>Openharmony</b>

    第二届大会回顾第25期 | OpenHarmony的Python设备应用开发

    Python以其简单、易学和功能强大而闻名,有着广泛的用户群体。采用Python开发有助于降低OpenHarmony的学习门槛。如何在OpenHarmony用Python开发设备应用,有哪些关键技术?电
    的头像 发表于 08-27 11:53 ?1315次阅读
    第二届大会回顾第25期 | <b class='flag-5'>OpenHarmony</b><b class='flag-5'>上</b>的Python设备应用开发