HarmonyOS 自定义弹窗(CustomDialog)不直接在组件内new CustomDialogController({}) ,弹窗无法显示

自定义弹窗:

@CustomDialog
export struct TwoButtonDialog {
  onClickLeft?: () => void
  onClickRight?: () => void
  title: string = ''
  content: string = ''
  leftBtnText: string = '取消'
  rightBtnText: string = '确定'

  controller?: CustomDialogController

  build() {
    Column() {
      if (this.title.length > 0) {
        Text(this.title)
          .fontColor(0x333333)
          .fontSize(18)
          .fontWeight(FontWeight.Bold)
      }

      Text(this.content)
        .fontColor(0x333333)
        .fontSize(14)
        .margin({ top: this.title.length > 0 ? 15 : 0 })
        .constraintSize({
          minHeight: 44
        })


      Row() {

        Button(this.leftBtnText)
          .height(44)
          .layoutWeight(1)
          .fontColor($r('app.color.app_main_color'))
          .fontSize(16)
          .borderWidth(1)
          .borderColor($r('app.color.app_main_color'))
          .backgroundColor(Color.White)
          .onClick(() => {
            if (this.controller) {
              this.controller.close()
            }
            if (this.onClickLeft) {
              this.onClickLeft()
            }
          })

        Divider()
          .width(11)
          .backgroundColor(Color.Transparent)
          .color(Color.Transparent)

        Button(this.rightBtnText)
          .height(44)
          .layoutWeight(1)
          .fontColor(Color.White)
          .fontSize(16)
          .backgroundColor($r('app.color.app_main_color'))
          .onClick(() => {
            if (this.controller) {
              this.controller.close()
            }
            if (this.onClickRight) {
              this.onClickRight()
            }
          })

      }
      .width('100%')
      .margin({ top: 20 })
    }
    .width('100%')
    .backgroundColor(Color.White)
    .borderRadius(12)
    .alignItems(HorizontalAlign.Center)
    .padding({
      left: 20,
      right: 20,
      top: 30,
      bottom: 30
    })
  }
}

//在封闭一个静态方法:
export class DialogUtil{

  static showDialog(){

    let dialogController: CustomDialogController = new CustomDialogController({
      builder:TwoButtonDialog({
        title:'提示',
        content:‘你好’,
        leftBtnText:'取消',
        rightBtnText:'确定',
        onClickLeft: ()=> {
        },
        onClickRight: ()=> {

        },
      }),
      backgroundColor:Color.White,
      cornerRadius:12
    });
    dialogController.open()

  }

}

然后在Page的按钮onClick方法中调用:DialogUtil.showDialog()

自定义弹窗无法显示

HarmonyOS
2024-12-25 07:35:50
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
FengTianYa

目前每次用自定义弹窗时都需要在页面里创建CustomDialogController

如果想进一步封装实现调用一个方法,传入弹窗内容即可弹窗,场景更推荐使用promptAction.openCustomDialog

开发者主要需要自定义弹框内容的buillder,使用参考:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-promptaction-V5#promptactionopencustomdialog11

分享
微博
QQ
微信
回复
2024-12-25 10:38:34
相关问题
HarmonyOS 自定义弹窗CustomDialog
206浏览 • 1回复 待解决
HarmonyOS 自定义弹窗CustomDialog问题
818浏览 • 1回复 待解决
HarmonyOS 自定义弹窗CustomDialog 问题
226浏览 • 1回复 待解决
HarmonyOS 自定义弹窗 (CustomDialog)问题
708浏览 • 1回复 待解决
HarmonyOS 自定义弹窗不能显示问题
288浏览 • 1回复 待解决
HarmonyOS 全局自定义弹窗无法弹出
317浏览 • 1回复 待解决
HarmonyOS 自定义弹窗封装后不显示
610浏览 • 1回复 待解决