中国优质的IT技术网站
专业IT技术创作平台
IT职业在线教育平台
实现二次侧滑退出应用
微信扫码分享
import common from "@ohos.app.ability.common"; import { promptAction } from "@kit.ArkUI"; @Entry @Component struct IndexComponent { @State textColor: Color = Color.Black; onPageShow() { this.textColor = Color.Blue; console.info("IndexComponent onPageShow"); } onPageHide() { this.textColor = Color.Transparent; console.info("IndexComponent onPageHide"); } firstBcakTimestamp: number = 0; onBackPress() { console.info("IndexComponent onBackPress"); this.textColor = Color.Red; let now = Date.now(); if (now - this.firstBcakTimestamp < 1000) { const context = getContext(this) as common.UIAbilityContext context.terminateSelf(); // 终止程序 } else { this.firstBcakTimestamp = now; let option: promptAction.ShowToastOptions = { message: "是否继续退出"// 可以是$r图片 } promptAction.showToast(option); } return true } build() { Column() { Text("Hello World") .fontColor(this.textColor) .fontSize(30) .margin(30) }.width("100%") } }