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)

相关推荐
是稻香啊14 小时前
HarmonyOS6 ArkUI 触摸拦截(onTouchIntercept)全面解析与实战演示
ubuntu·华为·harmonyos·harmonyos6
是稻香啊15 小时前
HarmonyOS6 ArkUI 子组件触摸测试控制(onChildTouchTest)全面解析与实战演示
华为·harmonyos·harmonyos6
fei_sun17 小时前
【鸿蒙智能硬件】(一)环境配置
华为·harmonyos
大雷神18 小时前
HarmonyOS APP<玩转React>开源教程一:开发环境搭建与项目创建
harmonyos
HarmonyOS_SDK18 小时前
高德地图携手HamonyOS SDK,首发鸿蒙AR实景步导
harmonyos
●VON1 天前
【鸿蒙PC】在 HarmonyOS 上跑 Electron?手把手教你搞定桌面欢迎页!(Mac版)
开发语言·macos·华为·electron·电脑·harmonyos
前端不太难1 天前
一个真实鸿蒙 App 的工程目录结构
华为·状态模式·harmonyos
枫叶丹41 天前
【HarmonyOS 6.0】聚合链接(App Linking)实战:从创建配置到应用跳转
开发语言·华为·harmonyos
alice--小文子1 天前
鸿蒙(HarmonyOS)-怎么在电脑端给鸿蒙真机安装.hap包
华为·harmonyos
Swift社区1 天前
鸿蒙 ArkUI 项目为什么容易变成“巨型页面文件”?
华为·harmonyos