【HarmonyOS NEXT】解决自定义弹框遮挡气泡提示的问题
2025-06-30 23:03:22
102次阅读
0个评论
【HarmonyOS NEXT】解决自定义弹框遮挡气泡提示的问题
##鸿蒙开发能力 ##HarmonyOS SDK应用服务##鸿蒙金融类应用 (金融理财#
一、问题背景:
弹框和气泡,dialog和toast。是我们应用开发中常用的两种提示媒介。
在鸿蒙应用中,如果自定义弹框在底部展示时,又弹出气泡进行提示,会发现气泡在弹框层级之下。会被遮挡住,影响气泡的显示。
二、解决方案:
设置气泡的显示模式为置顶即可:
promptAction.showToast({
message: "测试",
duration: 3000,
showMode: promptAction.ToastShowMode.TOP_MOST, // 添加展示模式,顶部展示,可保证层级最高。
})
三、源码示例
下方源码,操作点击click me按钮,显示弹框的同时,会显示气泡:
@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})
}
}
00
- 1回答
- 0粉丝
- 0关注
相关话题
- 【HarmonyOS Next】鸿蒙应用弹框和提示气泡详解(一)
- 【HarmonyOS Next】鸿蒙中自定义弹框OpenCustomDialog、CustomDialog与DialogHub的区别详解
- 【拥抱鸿蒙】HarmonyOS之构建一个自定义弹框
- 自定义组件之<四>自定义对话框(Dialog)
- 自定义组件之<五>自定义对话框(PromptAction)
- 鸿蒙Next气泡提示(Popup)的使用
- 【HarmonyOS Next】鸿蒙应用弹框和提示气泡详解(二)之浮层(OverlayManager),半模态页面(bindSheet),全模态页面(bindContentCove
- 【HarmonyOS NEXT】 自定义弹窗页面级层级控制解决方案
- 鸿蒙开发:自定义一个动态输入框
- 自定义组件之<二>自定义圆环(Ring)
- 封装自定义组件,快速实现HarmonyOS Next系统下的统一弹窗解决方案
- 自定义组件之<八>自定义下拉刷新(RefreshList)
- 鸿蒙开发:实现Popup气泡提示
- 自定义组件之<三>自定义标题栏(TitleBar)
- 自定义组件之<六>自定义饼状图(PieChart)