HarmonyOS自定义弹出对话框CustomDialog并传递变量

HarmonyOS定义了一系列弹窗反馈类的组件​

和前端开发框架VUE3配套生态库element plus中的提供各种组件相比,还是要少一些。可能是手机端操作和PC端操作的差异导致的​

如果内置的弹窗不满足要求,可以基于CustomDialog自定义出各种个性化的反馈组件。

首先新建一个ets文件,使用CustomDialog装饰器定义一个自定义的dialog。其内部的语法和普通的Component是完全相同的,通过组合基础组件可以实现不同的布局。

注意需要传递的参数使用@Link进行修饰,在"实例化"的时候引用

javascript 复制代码
@CustomDialog
export default struct CustomActionSheet {
  @Link actionList: string[]
  controller: CustomDialogController
  cancel: () => void


  build() {
    Column() {
      ForEach(this.actionList, (item: string) => {
        Text(item)
          .width('100%')
          .height(24)
          .fontSize(16)
          .margin({ top: 2, bottom: 2 })
          .textAlign(TextAlign.Center)
          .onClick((e)=>{

          })
      }, item => item)


      Button('取消', { type: ButtonType.Normal }).borderRadius(4).width(80).onClick((e) => {
        this.controller.close()
        this.cancel()
      })
    }
    .justifyContent(FlexAlign.SpaceAround)
    .alignItems(HorizontalAlign.Center)
    .height(200)
    .padding({ left: 10, top: 20 })
    .borderRadius(8)
  }
}

​然后在父组件中实例化CustomDialogController,并在builder属性中引用这个CustomDialog​

javascript 复制代码
diaglogController: CustomDialogController = new CustomDialogController({
    builder: CustomActionSheet({
      cancel: this.onCancel,
      actionList: $actionList
    }),
    cancel: this.existApp,
    autoCancel: true,
    alignment: DialogAlignment.Bottom,
    offset: { dx: 0, dy: -20 },
    gridCount: 4,
    customStyle: false
  })

在需要触发的地方调用即可​

javascript 复制代码
Button().onClick((e) => {
          if (this.diaglogController != undefined) {
            this.diaglogController.open()

          }
        })

最终效果:

​​​​​​​

相关推荐
不爱吃糖的程序媛15 小时前
OpenHarmony Linux 环境 SDK 使用说明(进阶--依赖库的解决方法)
linux·运维·harmonyos
狮子也疯狂15 小时前
【生态互联】| 鸿蒙三方库的选择与适配策略
华为·harmonyos
不爱吃糖的程序媛15 小时前
鸿蒙Lycium 交叉编译框架完全指南
华为·harmonyos
人间打气筒(Ada)15 小时前
[鸿蒙2025领航者闯关]星盾护航支付安全:鸿蒙6.0在金融APP中的实战闯关记
安全·金融·harmonyos·#鸿蒙2025领航者闯关#·#鸿蒙6实战#·#开发者年度总结#
hh.h.15 小时前
灰度发布与A/B测试:Flutter+鸿蒙的分布式全量发布方案
分布式·flutter·harmonyos
路人与大师15 小时前
PaddleOCR VL 华为NPU 910B 环境配置完成报告
华为
讯方洋哥15 小时前
HarmonyOS实战开发调试
harmonyos
yenggd15 小时前
华为sr-mpls TE配置案例
网络·华为
yenggd15 小时前
华为+sr-mpls BE简单配置案例
运维·网络·华为
柒儿吖1 天前
m4宏处理器在鸿蒙PC上的应用指南
华为·harmonyos