HarmonyOS鸿蒙Next中自定义弹窗封装

HarmonyOS鸿蒙Next中自定义弹窗封装

1.自定义弹框定义

// 自定义弹窗层
dialogController: CustomDialogController = new CustomDialogController({
  builder: CustomInputDialog({
    title: '重命名音频',
    message: '支持下划线、数宇、英文字母与中文',
    inputText: this.tempInputText,
    onConfirm: async () => {
      await this.updateRecord()
    },
  }),
  autoCancel: true,
  customStyle: true,
  alignment: DialogAlignment.Center,
})

2.自定义弹框打开方式

aboutToAppear() {
  // 打开弹窗
  this.dialogController.open()
}

3.自定义弹框封装

@CustomDialog
export struct CustomInputDialog {
  controller?: CustomDialogController
  title?: ResourceStr = ''
  message?: ResourceStr = ''
  placeholder?: ResourceStr = ''
  @Link inputText: string
  @State tempInputText: string = ''
  onCancel?: () => void
  onConfirm?: () => void

  aboutToAppear() {
    this.tempInputText = this.inputText
  }

  build() {
    Column({ space: 10 }) {
      Text(this.title)
        .fontSize(20)
        .fontWeight(500)
        .fontColor($r('app.color.font'))
      Text(this.message)
        .fontSize(14)
        .fontColor($r('app.color.font'))
      TextInput({ text: this.tempInputText, placeholder: this.placeholder })
        .selectAll(true)// 自动全选
        .defaultFocus(true)// 自动获取焦点
        .height(40)
        .width('100%')
        .border({ width: 1, color: $r('app.color.border'), radius: 6 })
        .backgroundColor($r('app.color.white'))
        .padding({ left: 10 })
        .cancelButton({
          icon: { src: $r('app.media.ic_public_close'), size: 20 },
          style: CancelButtonStyle.INPUT
        })
        .onChange((value) => {
          this.tempInputText = value
        })
      Row({ space: 10 }) {
        Button('取消')
          .fontSize(16)
          .type(ButtonType.Normal)
          .height(40)
          .borderRadius(40 / 2)
          .backgroundColor(Color.Transparent)
          .fontColor($r('app.color.brand'))
          .layoutWeight(1)
          .onClick(() => {
            // 关闭弹窗
            this.controller?.close()
            // 执行取消
            if (this.onCancel) this.onCancel()
          })
        Button('确定')
          .fontSize(16)
          .type(ButtonType.Normal)
          .height(40)
          .borderRadius(40 / 2)
          .backgroundColor($r('app.color.brand'))
          .fontColor($r('app.color.white'))
          .layoutWeight(1)
          .enabled(this.tempInputText.length > 0)
          .onClick(() => {
            // 关闭弹窗
            this.controller?.close()
            // 更新 @Link 的值
            this.inputText = this.tempInputText
            // 执行确定
            if (this.onConfirm) this.onConfirm()
          })
      }
      .margin({ top: 10 })
    }
    .width(336)
    .borderRadius(24)
    .padding(20)
    .backgroundColor('#f1f1f1')
    .alignItems(HorizontalAlign.Start)
  }
}

更多关于HarmonyOS鸿蒙Next中自定义弹窗封装的实战教程也可以访问 https://www.itying.com/category-93-b0.html

2 回复

在HarmonyOS Next中自定义弹窗封装需使用ArkUI组件系统。通过继承CustomDialogController类实现自定义弹窗逻辑,利用@CustomDialog装饰器定义弹窗UI。需在aboutToAppear中初始化数据,build方法中构建布局。支持绑定自定义事件回调,通过controller控制弹窗显示/隐藏。关键属性包括alignment(位置)、offset(偏移)、customStyle(样式)。弹窗内容可使用所有ArkUI组件,需注意内存管理避免泄漏。

更多关于HarmonyOS鸿蒙Next中自定义弹窗封装的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


在HarmonyOS Next中,自定义弹窗的实现方式很规范。您的代码展示了完整的自定义弹窗封装流程,包括控制器定义、弹窗结构和打开方式。以下是关键点分析:

  1. 控制器配置合理:
  • 使用了CustomDialogController管理弹窗生命周期
  • 通过builder参数传入自定义组件
  • 设置了autoCancel和alignment等常用属性
  1. 弹窗组件结构清晰:
  • 使用@CustomDialog装饰器正确定义了自定义弹窗组件
  • 通过@Link实现了父子组件数据双向绑定
  • 包含了完整的标题、消息、输入框和操作按钮
  1. 交互逻辑完善:
  • 正确处理了输入框变化事件
  • 实现了取消和确认按钮的回调
  • 通过controller.close()管理弹窗关闭

建议可以进一步优化的地方:

  • 弹窗宽度可以考虑使用百分比适配不同屏幕
  • 背景色建议使用资源引用而非硬编码
  • 可以增加动画效果提升用户体验

整体实现符合HarmonyOS的组件开发规范,是一个标准的自定义弹窗实现方案。

回到顶部