【问题标题】:How to set On back option for Navigation and Current activity如何为导航和当前活动设置 On back 选项
【发布时间】:2020-05-04 12:23:31
【问题描述】:

我创建了一个带有列表视图的应用程序,当我从主页单击返回按钮时,它会显示一条消息并要求退出或不退出。在我实现了导航抽屉并设置打开和关闭之后。但是当我从导航中单击返回按钮时,返回并弹出我设置为锄头页面的吐司。

@Override
public void onBackPressed() {

    if (drawerLayout.isDrawerOpen(GravityCompat.START)){
        drawerLayout.closeDrawer(GravityCompat.START);
    }else{
        super.onBackPressed();
    }

    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setMessage("Are you sure you want to Exit?")
            .setCancelable(false)
            .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    finish();
                    AdminHome.super.onBackPressed();
                }
            })

            .setNegativeButton("No", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.cancel();
                }
            });

    AlertDialog alertDialog = builder.create();
    alertDialog.show();
}

当我从导航栏点击返回并从主页设置 toast 时,如何停止 toast 消息。

【问题讨论】:

    标签: java android android-layout button navigation


    【解决方案1】:

    您好,您可以像这样更改您的代码

    @Override
    public void onBackPressed() {
    
    if (drawerLayout.isDrawerOpen(GravityCompat.START)){
        drawerLayout.closeDrawer(GravityCompat.START);
    }else{
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setMessage("Are you sure you want to Exit?")
            .setCancelable(false)
            .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    finish();
                    AdminHome.super.onBackPressed();
                }
            })
    
            .setNegativeButton("No", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.cancel();
                }
            });
    
    AlertDialog alertDialog = builder.create();
    alertDialog.show();
    }
    

    }

    【讨论】:

      【解决方案2】:

      将 AlertDialog 代码放在 else 块中。

      【讨论】:

        【解决方案3】:

        将您的警报对话框放入您的状况的else

        @Override
        public void onBackPressed() {
        
            if (drawerLayout.isDrawerOpen(GravityCompat.START)){
                drawerLayout.closeDrawer(GravityCompat.START);
            }else{
                    AlertDialog.Builder builder = new AlertDialog.Builder(this);
                    builder.setMessage("Are you sure you want to Exit?")
                    .setCancelable(false)
                    .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            super.onBackPressed();
                        }
                    })
        
                    .setNegativeButton("No", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.cancel();
                        }
                    });
        
                   AlertDialog alertDialog = builder.create();
                   alertDialog.show();
            }
        
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2016-06-14
          • 1970-01-01
          • 1970-01-01
          • 2021-11-19
          • 2013-06-16
          • 2014-04-19
          • 1970-01-01
          相关资源
          最近更新 更多