HarmonyOS CustomDialog里面使用router.push,dialog被关闭

@Preview
@CustomDialog
export struct UserAgreeConfirmDialog {
  controller?: CustomDialogController
  onExitListener?: () => void
  onAgreeListener?: () => void

  build() {
    Column() {
      Text("用户协议及隐私保护")
        .fontSize(18)
        .fontColor($r("app.color.color_333333"))
        .fontWeight(FontWeight.Medium)
        .margin({ top: 15 })
      Text() {
        Span("为保障您的合法权益,请阅读并同意").fontSize(15).fontColor($r('app.color.color_333333'))
        Span("《用户协议》").fontSize(15).fontColor($r('app.color.color_2abddd')).onClick(() => {
          let options: router.RouterOptions = {
            url: 'pages/WebPage',
            params: new toWebViewPageRouterParams(ServiceUrls.SERVICE_URL, "用户协议")
          }
          router.pushUrl(options)
        })
        Span("以及").fontSize(15).fontColor($r('app.color.color_333333'))
        Span("《隐私政策》").fontSize(15).fontColor($r('app.color.color_2abddd')).onClick(() => {
          let options: router.RouterOptions = {
            url: 'pages/WebPage',
            params: new toWebViewPageRouterParams(ServiceUrls.PROTOCOL_URL, "隐私政策")
          }
          router.pushUrl(options)
        })

      }.margin({ top: 25 })
      .width("80%")

      Text("严格保护您的个人信息安全")
        .fontSize(15)
        .margin({ top: 25 })
        .width("80%")
      Button('同意并登录', { type: ButtonType.Capsule, stateEffect: false })
        .opacity(1)
        .borderRadius(4)
        .backgroundColor($r('app.color.color_E0433B'))
        .onClick(() => {
          console.log('ButtonType.Normal')
        })
        .height(46)
        .width("90%")
        .flexShrink(1)
        .fontSize(15)
        .fontColor($r('app.color.white'))
        .margin({ top: 20 })
        .onClick(() => {
          if (this.onAgreeListener) {
            this.onAgreeListener()
          }
        })
      Text("不同意")
        .fontColor($r('app.color.color_8F8F8F'))
        .fontSize(15)
        .margin({ top: 15, bottom: 15 })
        .onClick(() => {
          // if (this.onExitListener) {
          //   this.onExitListener()
          // }
        })
    }.width("80%")
    .backgroundColor($r('app.color.white'))
    .borderRadius({ topLeft: 6, topRight: 6, bottomLeft: 6, bottomRight: 6 })
  }
}
  • 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.
HarmonyOS
2024-12-25 08:49:10
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
shlp

目前有两种方案实现:

方案1:通过Stack容器结合其它组件,使用UI组件模拟Dialog的效果,简单示例:

Stack() { // 原页面内容 Column(){...} // 模拟遮罩层 
  Text('').width('100%').height('100%').opacity(0.16) // 透明度可以自己调节一下 
    .backgroundColor(0x000000).visibility(this.visible) // 此处是原Dialog中的内容,使用UI组件模拟Dialog 
  Column(){...} } 
  • 1.
  • 2.
  • 3.
  • 4.

方案2:在页面的onPageShow()这个生命周期方法中调用open()方法打开,简单示例:

@Entry
@Component
struct Demo {
  @State flag: boolean = false
  dialogController: CustomDialogController | null = new CustomDialogController({
    builder: CustomDia({  //CustomDia是的自定义弹窗组件
      cancel: this.onCancel,
      confirm: this.onConfirm,
      jumpPage: this.jumpPage
    }),
    alignment: DialogAlignment.Center
  })
  onPageShow(): void {
    if (this.dialogController != null && this.flag) {
      this.dialogController.open()
    }
  }
  onPageHide(): void {
    if (this.dialogController != null) {
      this.dialogController.close()
    }
  }
  build() {
    Column() {
      Button('第一个页面').onClick(() => {
        if (this.dialogController != null) {
          this.flag = true
          this.dialogController.open()
        }
      })
    }
    .width('100%')
    .height('100%')

  }
  jumpPage() {
    router.pushUrl({
      url: 'pages/Index'
    })
  }
  onConfirm() {
    console.info('------>onConfirm is clicked')
  }
  onCancel() {
    console.info('------>onCancel is clicked')
  }
  aboutToDisappear(): void {
    this.dialogController = null
  }
}
  • 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.
分享
微博
QQ
微信
回复
2024-12-25 10:04:13
相关问题
Navigator和router.push有啥区别?
5558浏览 • 1回复 待解决
HarmonyOSCustomDialog自定义Dialog
1140浏览 • 1回复 待解决
dialog跳转新页面返回后dialog关闭
1023浏览 • 1回复 待解决
如何获取router push的所有page?
1030浏览 • 0回复 待解决
HarmonyOS dialog如何禁止侧滑返回关闭
661浏览 • 1回复 待解决
如何关闭router中的指定页面?
945浏览 • 1回复 待解决
HarmonyOS 关于CustomDialog使用
1083浏览 • 1回复 待解决