3.7、HarmonyOS Next 自定义弹窗(CustomDialog)

自定义弹窗(CustomDialog)可用于广告、中奖、警告、软件更新等与用户交互响应操作。开发者可以通过CustomDialogController类显示自定义弹窗。具体用法请参考

创建自定义弹窗

  1. 使用@CustomDialog装饰器装饰自定义弹窗。
  2. @CustomDialog装饰器用于装饰自定义弹框,此装饰器内进行自定义内容(也就是弹框内容)。
ts 复制代码
@CustomDialog
struct CustomDialogExample {
  controller: CustomDialogController
  build() {
    Column() {
      Text('我是内容')
      .fontSize(20)
      .margin({ top: 10, bottom: 10 })
    }
  }
}

3.创建构造器,与装饰器呼应相连。

ts 复制代码
dialogController: CustomDialogController = new CustomDialogController({
    builder: CustomDialogExample({}),
})

4.点击与onClick事件绑定的组件使弹窗弹出

ts 复制代码
Flex({justifyContent:FlexAlign.Center}){
  Button('click me')
    .onClick(() => {
      this.dialogController.open()
    })
}.width('100%')

弹窗的交互

弹窗可用于数据交互,完成用户一系列响应操作。

1、在@CustomDialog装饰器内添加按钮操作,同时添加数据函数的创建。

ts 复制代码
@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 })
    }
  }
}

2、页面内需要在构造器内进行接收,同时创建相应的函数操作。

ts 复制代码
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')
  }

上一篇 3.6、文本输入(TextInput/TextArea) 下一篇 3.8、气泡提示(Popup)

相关推荐
魔术师ID1 小时前
HarmonyOS开发组件基础
华为·harmonyos
周胡杰5 小时前
组件导航 (Navigation)+flutter项目搭建-混合开发+分栏
数码相机·flutter·华为·电脑·harmonyos·鸿蒙
特立独行的猫a10 小时前
HarmonyOS 影视应用APP开发--配套的后台服务go-imovie项目介绍及使用
华为·golang·harmonyos·影视app
梁下轻语的秋缘12 小时前
HarmonyOS:重构万物互联时代的操作系统范式
华为·重构·harmonyos
lpfasd12312 小时前
Flutter与Kotlin Multiplatform(KMP)深度对比及鸿蒙生态适配解析
flutter·kotlin·harmonyos
交叉编译之王 hahaha12 小时前
OpenHarmony 5.1.0 Release目录结构详细解析(3级目录)
arm开发·华为·harmonyos
交叉编译之王 hahaha13 小时前
RK3568-鸿蒙5.1与原生固件-扇区对比分析
华为·harmonyos
HarmonyOS_SDK13 小时前
【FAQ】HarmonyOS SDK 闭源开放能力 —Vision Kit (3)
harmonyos
梁下轻语的秋缘15 小时前
鸿蒙系统电脑:开启智能办公新时代
华为·电脑·harmonyos
梁下轻语的秋缘15 小时前
HarmonyOS 与 OpenHarmony:同根而不同途
华为·harmonyos