我怎样才能在后退按钮上添加多次点击

how can i add multiple click on back pressed button

我如何添加多个后退按钮,比如当我按下后退按钮时,当我双击按下按钮然后应用程序退出时,显示消息后按下双击退出...我用了这个方法,但它只是两次退出我要执行的应用程序 3 次

    if (doubleBackToExitPressedOnce){
        super.onBackPressed();
        doubleBackToExitPressedOnce = false;

    }

    else {
        doubleBackToExitPressedOnce = true;
        Toast.makeText(this, "Double Press to exit", Toast.LENGTH_SHORT).show();
    }

好吧,从 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();
}

我会这样建议:​​

    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();
}