网站首页 全球最实用的IT互联网站!

人工智能P2P分享Wind搜索发布信息网站地图标签大全

当前位置:诺佳网 > 软件工程 > 移动端开发 > 鸿蒙 >

【HarmonyOS NEXT】解决自定义弹框遮挡气泡提示的问

时间:2025-03-27 11:03

人气:

作者:admin

标签:

导读:【HarmonyOS NEXT】解决自定义弹框遮挡气泡提示的问题 一、问题背景: 弹框和气泡,dialog和toast。是我们应用开发中常用的两种提示媒介。 在鸿蒙应用中,如果自定义弹框在底部展示时,...

【HarmonyOS NEXT】解决自定义弹框遮挡气泡提示的问题

一、问题背景:

弹框和气泡,dialog和toast。是我们应用开发中常用的两种提示媒介。

在鸿蒙应用中,如果自定义弹框在底部展示时,又弹出气泡进行提示,会发现气泡在弹框层级之下。会被遮挡住,影响气泡的显示。

二、解决方案:

设置气泡的显示模式为置顶即可:

       promptAction.showToast({
            message: "测试",
            duration: 3000,
            showMode: promptAction.ToastShowMode.TOP_MOST, // 添加展示模式,顶部展示,可保证层级最高。
          })

三、源码示例

下方源码,操作点击click me按钮,显示弹框的同时,会显示气泡:
image

@Entry
@Component
struct TextPage {
  @State textValue: string = ''
  @State inputValue: string = 'click me'
  dialogController: CustomDialogController | null = new CustomDialogController({
    builder: CustomDialogExample({
      cancel: ()=> { this.onCancel() },
      confirm: ()=> { this.onAccept() },
      textValue: $textValue,
      inputValue: $inputValue
    }),
    cancel: this.exitApp,
    autoCancel: true,
    onWillDismiss:(dismissDialogAction: DismissDialogAction)=> {
      console.info("reason=" + JSON.stringify(dismissDialogAction.reason))
      console.log("dialog onWillDismiss")
      if (dismissDialogAction.reason == DismissReason.PRESS_BACK) {
        dismissDialogAction.dismiss()
      }
      if (dismissDialogAction.reason == DismissReason.TOUCH_OUTSIDE) {
        dismissDialogAction.dismiss()
      }
    },
    keyboardAvoidMode: KeyboardAvoidMode.DEFAULT,
    alignment: DialogAlignment.Bottom,
    // offset: { dx: 0, dy: -20 },
    gridCount: 4,
    customStyle: false,
    cornerRadius: 10,
    backgroundColor: Color.Black
  })

  // 在自定义组件即将析构销毁时将dialogController置空
  aboutToDisappear() {
    this.dialogController = null // 将dialogController置空
  }

  onCancel() {
    console.info('Callback when the first button is clicked')
  }

  onAccept() {
    console.info('Callback when the second button is clicked')
  }

  exitApp() {
    console.info('Click the callback in the blank area')
  }
  build() {
    Column() {
      Button(this.inputValue)
        .onClick(() => {
          promptAction.showToast({
            message: "测试",
            duration: 3000,
            showMode: promptAction.ToastShowMode.TOP_MOST, // 添加展示模式,顶部展示,可保证层级最高。
          })
          if (this.dialogController != null) {
            this.dialogController.open()
          }
        }).backgroundColor(0x317aff)
    }.width('100%').margin({ top: 5 })
    .margin({
      bottom: -16
    })
  }
}


@CustomDialog
@Component
struct CustomDialogExample {
  @Link textValue: string
  @Link inputValue: string

  controller?: CustomDialogController
  cancel: () => void = () => {
  }
  confirm: () => void = () => {
  }

  build() {
    Column() {
      TextInput({ placeholder: '', text: this.textValue }).height(60).width('90%')
        .onChange((value: string) => {
          this.textValue = value
        })
      Text('Whether to change a text?').fontSize(16).margin({ bottom: 10 })
      Flex({ justifyContent: FlexAlign.SpaceAround }) {
        Button('cancel')
          .onClick(() => {
            if (this.controller != undefined) {
              this.controller.close()
              this.cancel()
            }
          }).backgroundColor(0xffffff).fontColor(Color.Black)
        Button('confirm')
          .onClick(() => {
            if (this.controller != undefined) {
              this.inputValue = this.textValue
              this.controller.close()
              this.confirm()
            }
          }).backgroundColor(0xffffff).fontColor(Color.Red)
      }.margin({ bottom: 10 })

    }.width("90%")
    .offset({ x: 0, y: 16}) 
  }
}
温馨提示:以上内容整理于网络,仅供参考,如果对您有帮助,留下您的阅读感言吧!
相关阅读
本类排行
相关标签
本类推荐

CPU | 内存 | 硬盘 | 显卡 | 显示器 | 主板 | 电源 | 键鼠 | 网站地图

Copyright © 2025-2035 诺佳网 版权所有 备案号:赣ICP备2025066733号
本站资料均来源互联网收集整理,作品版权归作者所有,如果侵犯了您的版权,请跟我们联系。

关注微信