Android后退事件处理
程序员文章站
2022-03-09 21:05:09
...
public long lastClickTime = 0;
public void onBackPressed(){
if(lastClickTime <= 0){
Toast.makeText(this, "再按一次退出程序", 1).show();
lastClickTime = System.currentTimeMillis();//记录当前时间
}else{
long currentClickTime = System.currentTimeMillis();//记录当前时间
if(currentClickTime-lastClickTime <= 1000){ //时间差小于1000毫秒
finish(); //退出
}else{
Toast.makeText(this, "再按一次退出程序", 1).show();
lastClickTime = currentClickTime; //刷新时间
}
}
}