HarmonyOS 鸿蒙Next 弹框不允许自动取消
HarmonyOS 鸿蒙Next 弹框不允许自动取消
CustomDialog如何限制无法自动关闭, 必须用户点击按钮关闭, autoCancel 只能限制用户点击背景不自动关闭, 但是用户物理返回、系统侧滑返回还是会关闭弹框
HUAWEI Mate 60 Pro ALN-AL80 next0.0.65(SP71C00E65R4P9log)
DevEco Studio版本:(5.0.3.700)
代码示例
```javascript
// xxx.ets
[@CustomDialog](/user/CustomDialog)
struct CustomDialogExample {
controller?: CustomDialogController
cancel: () => void = () => {
}
confirm: () => void = () => {
}
build() {
Column() {
Text('可展示在主窗口外的弹窗')
.fontSize(30)
.height(100)
Button('点我关闭弹窗')
.onClick(() => {
if (this.controller != undefined) {
this.controller.close()
}
})
.margin(20)
}
}
}
[@Entry](/user/Entry)
[@Component](/user/Component)
struct CustomDialogUser {
dialogController: CustomDialogController | null = new CustomDialogController({
builder: CustomDialogExample({
cancel: () => { this.onCancel() },
confirm: () => { this.onAccept() }
}),
cancel: this.existApp,
autoCancel: false,
alignment: DialogAlignment.Center,
offset: { dx: 0, dy: -20 },
gridCount: 4,
showInSubWindow: false,
isModal: true,
customStyle: false,
cornerRadius: 10,
})
// 在自定义组件即将析构销毁时将dialogControlle置空
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')
}
existApp() {
console.info('Click the callback in the blank area')
}
build() {
Column() {
Button('click me')
.onClick(() => {
if (this.dialogController != null) {
this.dialogController.open()
}
}).backgroundColor(0x317aff)
}.width('100%').margin({ top: 5 })
}
}
更多关于HarmonyOS 鸿蒙Next 弹框不允许自动取消的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
2 回复
这个可以加上onWillDismiss方法,判断下退出方式就行了
builder: CustomDialogExample({
...
onWillDismiss: (dismissDialogAction: DismissDialogAction) => {
console.info("reason=" + JSON.stringify(dismissDialogAction.reason))
console.log("dialog onWillDismiss")
if (dismissDialogAction.reason == DismissReason.CLOSE_BUTTON) {
dismissDialogAction.dismiss()
}
}
})
更多关于HarmonyOS 鸿蒙Next 弹框不允许自动取消的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html