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

  }

}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107.
  • 108.
  • 109.
  • 110.

然后在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
702浏览 • 1回复 待解决
HarmonyOS 自定义弹窗CustomDialog 问题
695浏览 • 1回复 待解决
HarmonyOS 自定义弹窗CustomDialog问题
1280浏览 • 1回复 待解决
HarmonyOS 自定义弹窗 (CustomDialog)问题
1212浏览 • 1回复 待解决
HarmonyOS 自定义弹窗CustomDialog调用问题
1029浏览 • 1回复 待解决
HarmonyOS 自定义弹窗不能显示问题
785浏览 • 1回复 待解决
HarmonyOS 全局自定义弹窗无法弹出
767浏览 • 1回复 待解决
HarmonyOS 自定义弹窗封装后不显示
1118浏览 • 1回复 待解决