如何添加多个背面按钮,例如按下向后按钮显示我在吐司消息后单击double单击时,当我单击double on Perspers on Perspert按钮,然后应用程序退出...我做过这种方法,但它只是两次退出我想实现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();
}
从UI的角度看,这似乎超级骇客,但是如果您真的想这样做,那么一种简单的方法可能是:
Integer backToExitPressedCounter = 0
...
if(backToExitPressedCounter==3){
super.onBackPressed();
backToExitPressedCounter = 0;
} else {
backToExitPressedCounter++;
Toast.makeText(this, "Triple Press to exit", Toast.LENGTH_SHORT).show();
}