自定义弹窗:
@CustomDialog
export struct TwoButtonDialog {
onClickLeft?: () => void
onClickRight?: () => void
title: string = ''
content: string = ''
leftBtnText: string = '取消'
rightBtnText: string = '确定'
controller?: CustomDialogController
build() {
Column() {
if (this.title.length > 0) {
Text(this.title)
.fontColor(0x333333)
.fontSize(18)
.fontWeight(FontWeight.Bold)
}
Text(this.content)
.fontColor(0x333333)
.fontSize(14)
.margin({ top: this.title.length > 0 ? 15 : 0 })
.constraintSize({
minHeight: 44
})
Row() {
Button(this.leftBtnText)
.height(44)
.layoutWeight(1)
.fontColor($r('app.color.app_main_color'))
.fontSize(16)
.borderWidth(1)
.borderColor($r('app.color.app_main_color'))
.backgroundColor(Color.White)
.onClick(() => {
if (this.controller) {
this.controller.close()
}
if (this.onClickLeft) {
this.onClickLeft()
}
})
Divider()
.width(11)
.backgroundColor(Color.Transparent)
.color(Color.Transparent)
Button(this.rightBtnText)
.height(44)
.layoutWeight(1)
.fontColor(Color.White)
.fontSize(16)
.backgroundColor($r('app.color.app_main_color'))
.onClick(() => {
if (this.controller) {
this.controller.close()
}
if (this.onClickRight) {
this.onClickRight()
}
})
}
.width('100%')
.margin({ top: 20 })
}
.width('100%')
.backgroundColor(Color.White)
.borderRadius(12)
.alignItems(HorizontalAlign.Center)
.padding({
left: 20,
right: 20,
top: 30,
bottom: 30
})
}
}
//在封闭一个静态方法:
export class DialogUtil{
static showDialog(){
let dialogController: CustomDialogController = new CustomDialogController({
builder:TwoButtonDialog({
title:'提示',
content:‘你好’,
leftBtnText:'取消',
rightBtnText:'确定',
onClickLeft: ()=> {
},
onClickRight: ()=> {
},
}),
backgroundColor:Color.White,
cornerRadius:12
});
dialogController.open()
}
}
然后在Page的按钮onClick方法中调用:DialogUtil.showDialog()
自定义弹窗无法显示
目前每次用自定义弹窗时都需要在页面里创建CustomDialogController
如果想进一步封装实现调用一个方法,传入弹窗内容即可弹窗,场景更推荐使用promptAction.openCustomDialog
开发者主要需要自定义弹框内容的buillder,使用参考:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-promptaction-V5\#promptactionopencustomdialog11