【问题标题】:AlertDialog OnBackPressed() Not Working ProperlyAlertDialog OnBackPressed() 无法正常工作
【发布时间】:2019-04-23 08:54:34
【问题描述】:

我为简单的警报对话框编写了以下代码,但没有显示警报对话框。 API:26

 @Override
    public void onBackPressed() {
        super.onBackPressed();

    AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
    builder.setMessage("Are You sure to Exit?")
            .setTitle("Exit")
            .setCancelable(false)
            .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    MainActivity.super.onBackPressed();
                }
            })
            .setNegativeButton("No", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {

                }
            });

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

我对 android 和测试它的功能非常陌生。 我在这个小代码上工作了 30 分钟来调试它,并在模拟器和外部电话中使用了不同的电话,但失败了。 然后我决定把这段代码放到stackoverflow上。

【问题讨论】:

  • 删除该方法顶部的super.onBackPressed(); 行。
  • 在是点击更好写finishAffinity()和在没有点击写dialogInterface.cancel()

标签: android android-alertdialog


【解决方案1】:

希望此代码可以帮助您!

 @Override
    public void onBackPressed()
    {
         AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
        builder.setMessage("Are You sure to Exit?")
                .setTitle("Exit")
                .setCancelable(false)
                .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        this.finish(); //OR super.onBackPressed(); or whatever u want

                    }
                })
                .setNegativeButton("No", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {

                    }
                });

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

【讨论】:

    【解决方案2】:

    使用此代码以编程方式退出或关闭应用程序

    @Override
        public void onBackPressed() {
            AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
            alertDialogBuilder.setTitle("Exit Application?");
            alertDialogBuilder
                    .setMessage("Click yes to exit!")
                    .setCancelable(false)
                    .setPositiveButton("Yes",
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int id) {
                                    moveTaskToBack(true);
                                    android.os.Process.killProcess(android.os.Process.myPid());
                                    System.exit(1);
                                }
                            })
    
                    .setNegativeButton("No", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
    
                            dialog.cancel();
                        }
                    });
    
            AlertDialog alertDialog = alertDialogBuilder.create();
            alertDialog.show();
        }
    

    【讨论】:

    【解决方案3】:

    删除super.onBackPressed();

    @Override
    public void onBackPressed() {
        AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
        builder.setMessage("Are You sure to Exit?")
                .setTitle("Exit")
                .setCancelable(false)
                .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        MainActivity.super.onBackPressed();
                    }
                })
                .setNegativeButton("No", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
    
                    }
                });
    
        AlertDialog alert = builder.create();
        alert.show();
    }
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-19
      • 2020-06-13
      • 2016-12-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多