1

如何将后退按钮配置为在应用退出之前按下两次?我想触发

@Override
public void onBackPressed() {
    //custom actions
    //display toast "press again to quit app"
    super.onBackPressed();
}
4

4 回答 4

8

试试这个:

private boolean doubleBackToExitPressedOnce = false;

@Override
protected void onResume() {
    super.onResume();
    // .... other stuff in my onResume ....
    this.doubleBackToExitPressedOnce = false;
}

@Override
public void onBackPressed() {
    if (doubleBackToExitPressedOnce) {
        super.onBackPressed();
        return;
    }
    this.doubleBackToExitPressedOnce = true;
    Toast.makeText(this, "Press twice to exit", Toast.LENGTH_SHORT).show();

}

此代码段还处理活动恢复时的重置状态

于 2012-06-06T20:14:45.330 回答
2

我看到这个问题有点老了,但我虽然这可能会帮助一些人寻找已经给出的答案的替代方案。

这就是我处理退出应用程序的方式。如果有人有更好的 - 或谷歌建议的 - 完成此任务的方法,我想知道。

编辑——忘了提到这是针对 Android 2.0 及更高版本的。对于以前的版本,覆盖onKeyDown(int keyCode, KeyEvent event)并检查keyCode == KeyEvent.KEYCODE_BACK. 是一个很好的检查链接。

private boolean mIsBackEligible = false;

@Override
public void onBackPressed() {

    if (mIsBackEligible) {

        super.onBackPressed();

    } else {

        mIsBackEligible = true;
        new Runnable() {
            // Spin up new runnable to reset the mIsBackEnabled var after 3 seconds
            @Override
            public void run() {
                CountDownTimer cdt = new CountDownTimer(3000, 3000) {
                    @Override
                    public void onTick(long millisUntilFinished) { 
                        // I don't want to do anything onTick
                    }

                    @Override
                    public void onFinish() {
                        mIsBackEligible = false;
                    }
                }.start();
            }
        }.run(); // End Runnable()

        Toast.makeText(this.getApplicationContext(),
                "Press back once more to exit", Toast.LENGTH_SHORT).show();

    }

}
于 2014-09-20T18:23:28.690 回答
0

你可以用一个全局整数来做你所要求的事情,然后计算它,如果 > 2,退出。

但是您可以采取更好的(IMO)方法,询问用户是否愿意退出:

private void questionQuit(){
    final CharSequence[] items = {"Yes, quit now", "No, cancel and go back"};

    builder = new AlertDialog.Builder(mContext);
    builder.setCancelable(false);
    builder.setTitle("Are you sure you want to quit?");
    builder.setItems(items, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int item) {
            switch(item){
            case 0:
                quit();
                break;
            case 1:
            default:
                break;
            }
        }
    }).show();
    AlertDialog alert = builder.create();

}
于 2012-06-06T20:14:52.333 回答
0
@Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (event.getAction() == KeyEvent.ACTION_DOWN) {
            switch (keyCode) {
            case KeyEvent.KEYCODE_BACK :
                int i = 0 ;
                   if(i == 1 )
                      {
                       finish();
                      }
                     i++;

                return true;
            }
        }
        return super.onKeyDown(keyCode, event);
    }
于 2012-06-06T20:31:51.007 回答