我怎样才能添加多个反向按钮,就像当我按下后退按钮显示我按下按钮后再按一下按钮然后按下按钮然后应用程序退出...我做了这个方法但它只是两次到退出我要实施3次的应用程序
if (doubleBackToExitPressedOnce){
super.onBackPressed();
doubleBackToExitPressedOnce = false;
}
else {
doubleBackToExitPressedOnce = true;
Toast.makeText(this, "Double Press to exit", Toast.LENGTH_SHORT).show();
}
答案 0 :(得分:1)
从UI的角度来看,这似乎是超级hacky,但如果你真的想这样做,一个简单的方法可能是这样的:
Integer backToExitPressedCounter = 0
...
if(backToExitPressedCounter==3){
super.onBackPressed();
backToExitPressedCounter = 0;
} else {
backToExitPressedCounter++;
Toast.makeText(this, "Triple Press to exit", Toast.LENGTH_SHORT).show();
}
答案 1 :(得分:1)
我建议那样:
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();
}