【HarmonyOS NEXT】怎么判断customDialog是否正在显示

 【关键字】

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 })
  }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值