我如何添加多个后按按钮,例如当我按向后按钮以向我显示吐司消息后按两次双击退出,当我单击两次按按钮然后退出应用程序时...我做了这种方法,但是两次退出该应用程序我想实施3次
if (doubleBackToExitPressedOnce){
super.onBackPressed();
doubleBackToExitPressedOnce = false;
}
else {
doubleBackToExitPressedOnce = true;
Toast.makeText(this, "Double Press to exit", Toast.LENGTH_SHORT).show();
}
最佳答案
我建议这样:
int counter = 0;
....
public void onBackPressed() {
counter++;
if(counter > 2){
System.exit(0);
}else{
Toast.makeText(this, "TRIPLE CLICK TO EXIT!", Toast.LENGTH_SHORT).show();
}
final long DELAY_TIME = 3000L;
new Thread(new Runnable() {
public void run(){
try {
Thread.sleep(DELAY_TIME);
counter = 0;
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
}