如何通过显示退出的小吃店对话框在双后按中退出?

时间:2019-02-27 10:08:38

标签: android android-intent onbackpressed android-snackbar

我想双击双击创建一个快餐栏对话框以退出...(java)

已请求这些

  1. 第一次按下显示对话“再次按下以退出” 2秒钟

  2. 再次按回车键时,显示“您要退出吗?” 确认按钮2秒钟

如下所示-

enter image description here

2 个答案:

答案 0 :(得分:0)

在activity_main中为您的布局创建ID

CoordinatorLayout coordinatorLayout;

@Override
public void onBackPressed() {     
    coordinatorLayout= (CoordinatorLayout) findViewById(R.id.coordinatorLayout);
if (!doubleBackToExitPressedOnce) {
            this.doubleBackToExitPressedOnce = true;
            Snackbar.make(coordinatorLayout, "Do you really want to exit?", Snackbar.LENGTH_LONG)
                .setAction("YES", new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    //button action here
                    System.exit(0);
                }
            }).setActionTextColor(Color.YELLOW)
              .show();
            new Handler().postDelayed(new Runnable() {

                @Override
                public void run() {
                    doubleBackToExitPressedOnce = false;
                }
            }, 2000);
        } 

希望这会有所帮助

答案 1 :(得分:0)

这是为您提供的解决方案-

private final long DOUBLE_PRESS_BACK_TO_EXIT_TIME = 2000;
boolean doubleBackPressed = false;

    @Override
    public void onBackPressed() {
        // todo: show the snackbar here. 
        this.doubleBackPressed = true;
        utils.showToastLong(getString(R.string.press_again_to_exit));
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                doubleBackPressed = false;
                // todo: hide the snackbar here. 
            }
        }, DOUBLE_PRESS_BACK_TO_EXIT_TIME);

    }

要自定义小吃店,请遵循以下link