美文网首页我爱编程
实现点击两次退出程序(代码片段)

实现点击两次退出程序(代码片段)

作者: 老牛哥儿 | 来源:发表于2018-04-15 12:48 被阅读0次

实现点击两次退出程序
// 第一次按下返回键的事件
private long firstPressedTime = 0;

// System.currentTimeMillis() 当前系统的时间
@Override
public void onBackPressed() {
if (System.currentTimeMillis() - firstPressedTime < 2000) {
super.onBackPressed();
} else {
Toast.makeText(MainActivity.this, "再按一次退出", Toast.LENGTH_SHORT).show();
firstPressedTime = System.currentTimeMillis();
}
}

相关文章

网友评论

    本文标题:实现点击两次退出程序(代码片段)

    本文链接:https://www.haomeiwen.com/subject/aoedkftx.html