美文网首页
鸿蒙~自定义弹窗(CustomDialog)

鸿蒙~自定义弹窗(CustomDialog)

作者: 胡修波 | 来源:发表于2023-12-27 07:54 被阅读0次
    • demo
    // xxx.ets
    @CustomDialog
    struct CustomDialogExample {
      controller: CustomDialogController
      cancel: () => void
      confirm: () => void
      build() {
        Column() {
          Text('我是内容').fontSize(20).margin({ top: 10, bottom: 10 })
          Flex({ justifyContent: FlexAlign.SpaceAround }) {
            Button('cancel')
              .onClick(() => {
                this.controller.close()
                this.cancel()
              }).backgroundColor(0xffffff).fontColor(Color.Black)
            Button('confirm')
              .onClick(() => {
                this.controller.close()
                this.confirm()
              }).backgroundColor(0xffffff).fontColor(Color.Red)
          }.margin({ bottom: 10 })
        }
      }
    }
    
    @Entry
    @Component
    struct DialogExample {
      dialogController: CustomDialogController = new CustomDialogController({
        builder: CustomDialogExample({
          cancel: this.onCancel,
          confirm: this.onAccept,
        }),
        alignment: DialogAlignment.Default,  // 可设置dialog的对齐方式,设定显示在底部或中间等,默认为底部显示
      })
      onCancel() {
        console.info('Callback when the first button is clicked')
      }
      onAccept() {
        console.info('Callback when the second button is clicked')
      }
    
      build() {
        Flex({ justifyContent: FlexAlign.Center }) {
          Button('click me')
            .onClick(() => {
              this.dialogController.open()
            })
        }.width('100%')
      }
    }
    

    相关文章

      网友评论

          本文标题:鸿蒙~自定义弹窗(CustomDialog)

          本文链接:https://www.haomeiwen.com/subject/ojekndtx.html