在我们日常开发过程中,为了避免用户由于误触返回键导致的误退出app,我们常常要对用户的退出手势进行第一次拦截,第二次通过的操作,现在,向大家介绍一种退出app手势拦截及通过判断方法
文档地址:https://qcnda4r17bnp.feishu.cn/wiki/He97wqZt1iP4BLkQcn1cgrsrnYb
import { promptAction } from '@kit.ArkUI';
@Entry
@Component
struct Index {
@State message: string = 'Hello World';
lastBackPressTime: number = 0
isWaitingSecond: boolean = false
onBackPress(): boolean | void {
const nowTime: number = Date.now()
if (!this.isWaitingSecond || nowTime - this.lastBackPressTime > 2000) {
this.lastBackPressTime = nowTime
this.getUIContext().getPromptAction().showToast({
message: '再按一次退出app'
})
this.isWaitingSecond = true
// 进行阻止退出
return true
}
// 如果不是以上两种状况,应当给予通行
this.isWaitingSecond = false
// 予以通行
return false
}
build() {
RelativeContainer() {
Text(this.message)
.id('HelloWorld')
.fontSize($r('app.float.page_text_font_size'))
.fontWeight(FontWeight.Bold)
.alignRules({
center: { anchor: '__container__', align: VerticalAlign.Center },
middle: { anchor: '__container__', align: HorizontalAlign.Center }
})
.onClick(() => {
this.message = 'Welcome';
})
}
.height('100%')
.width('100%')
}
}
实现效果:
第一次返回时

517



被折叠的 条评论
为什么被折叠?



