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

本文介绍了如何在HarmonyOS中使用CustomDialog创建自定义弹窗组件,与前端框架VUE3的elementplus库相比,它提供了更多的灵活性。开发者可以通过组合基础组件定制布局,并在父组件中实例化CustomDialogController来触发弹窗显示。
摘要由CSDN通过智能技术生成

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

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

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

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

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

@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​

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

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

Button().onClick((e) => {
          if (this.diaglogController != undefined) {
            this.diaglogController.open()

          }
        })

最终效果:

​​​​​​​

  • 9
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

尘烟生活家

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值