【关键字】
customDialog / cancel
【问题描述】
怎么判断customDialog是否正在显示。
【解决方案】
CustomDialogControllerOptions对象中有一个参数cancel,该属性是一个回调函数且是返回、ESC键和点击遮障层弹窗退出时的回调。
具体参数信息请参考:
https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/ts-methods-custom-dialog-box-V5
Demo如下:
@CustomDialog
struct CustomDialogExample {
cancel?: () => void
confirm?: () => void
controller: CustomDialogController
build() {
Column() {
Text('我是内容').fontSize(20).margin({ top: 10, bottom: 10 })
Flex({ justifyContent: FlexAlign.SpaceAround }) {
Button('cancel')
.onClick(() => {
this.controller.close()
if (this.cancel) {
this.cancel()
}
}).backgroundColor(0xffffff).fontColor(Color.Black)
Button('confirm')
.onClick(() => {
this.controller.close()
if (this.confirm) {
this.confirm()
}
}).backgroundColor(0xffffff).fontColor(Color.Red)
}.margin({ bottom: 10 })
}
}
}
@Entry
@Component
struct CustomDialogUser {
@State isShow : boolean = false;//添加一个变量来表示当前弹窗是否在显示
dialogController: CustomDialogController = new CustomDialogController({
builder: CustomDialogExample({
cancel: ()=> {
this.isShow = false;//开启弹窗时设置为false
this.onCancel()
console.log(this.isShow + "")
},
confirm: ()=> {
this.isShow = false;//开启弹窗时设置为false
this.onAccept()
},
}),
cancel:() => {
this.isShow = false//返回、点击遮障层取消时改变状态变量
console.log("点击了取消")
}
})
onCancel() {
console.info('Callback when the first button is clicked')
}
onAccept() {
console.info('Callback when the second button is clicked')
}
build() {
Column() {
Button('click me')
.onClick(() => {
this.isShow = true;//开启弹窗时设置为true
this.dialogController.open()
})
}.width('100%').margin({ top: 5 })
}
}