当前位置:网站首页>【HarmonyOS】【ARK UI】HarmonyOS ets语言怎么实现双击返回键退出
【HarmonyOS】【ARK UI】HarmonyOS ets语言怎么实现双击返回键退出
2022-07-30 17:43:00 【华为开发者论坛】
参考资料
代码运行
思路:
定义一个全局变量,判断点击时间与上一次点击时间差,若大于2秒则提示‘再按一次退出程序’字样,小于2秒退出应用,代码如下
import prompt from '@system.prompt';import app from '@system.app'@[email protected] Index { //todo 定义全局变量 @State exitTime: number= 0; private onBackPress() { this.ExitApp(); return true; } public ExitApp() { //todo 判断点击时间与上一次点击时间差,若大于5秒则提示‘再按一次退出程序’字样 if ((new Date().getTime() - this.exitTime) > 5000) { prompt.showToast({ message: '再按一次退出程序', duration: 2000, }); this.exitTime = new Date().getTime(); } else { //todo 小于5秒退出应用 app.terminate(); } } build() { Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { Text('双击返回app退出') .fontSize(40) .height(200) .width("100%") .textAlign(TextAlign.Center) .fontWeight(FontWeight.Bold) } .width('100%') .height('100%') }}
运行效果
欲了解更多更全技术文章,欢迎访问https://developer.huawei.com/consumer/cn/forum/?ha_source=zzh
边栏推荐
猜你喜欢
随机推荐
Leetcode数据库系列题解合集(持续更新)
CMake library search function does not search LD_LIBRARY_PATH
Promise入门到精通(1.5w字详解)
un7.30:linux——如何在docker容器中安装MySQL?
JMeter Notes 3 | JMeter Installation and Environment Instructions
weiit新零售小程序如何探索数字化门店的破局之路
基于亚马逊云科技无服务器服务快速搭建电商平台——性能篇
SQL存储过程详解
Dodging ice cream assassins?Crawling ice cream prices through crawlers
Google earth engine如何实现我们时间列表的排列和选取
FastJson反序列化漏洞(复现)
mysql刷脏的几种场景以及相关参数
leetcode-547:省份数量
LayaBox---TypeScript---基础数据类型
「Redis应用与深度实践笔记」,深得行业人的心,这还不来看看?
BCryptPasswordEncoder的使用及原理
Arranger software FL Studio Chinese version installation tutorial and switching language tutorial
超声波探伤仪是做什么用的?
592. Fraction Addition and Subtraction
[HarekazeCTF2019] Avatar Uploader 1