【问题标题】:Dismiss all dialogs in one click一键关闭所有对话框
【发布时间】:2014-11-07 11:51:47
【问题描述】:

我想在点击此按钮时隐藏所有对话框:

new AlertDialog.Builder(FullscreenActivity.this)
                                .setTitle("Error!")
                                .setMessage(".......")
                                .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface dialog, int which) {
//hide all dialogs
                                        }
                                    }).setIcon(R.drawable.icon_error).show();

我该怎么做?我想用这个关闭所有以前打开的对话框。

【问题讨论】:

  • 声明全局,为所欲为……
  • 我与@AM 声明全局并隐藏
  • 制作所有对话框的全局对象并调用 dialog1.cancel();dialog2.cancel();等等,如果您需要更多帮助显示所有对话框的代码
  • 我想用这个关闭所有以前打开的对话框。他们仍然开放。

标签: android dialog


【解决方案1】:
May this help you.Create common method for calling dialog :

// Declare this as global variables 
    public static AlertDialog.Builder builder;
    public AlertDialog dialog;
    public  Vector<AlertDialog> dialogs = new Vector<AlertDialog>();

public void alertDialog(String message) {
        builder = new AlertDialog.Builder(this);
        builder.setMessage(message).setPositiveButton("OK",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        dialogs.remove(dialog);
                        dialog.cancel();
                    }
                });
        dialog=builder.create();
        dialogs.add(dialog);
        dialog.show();

    }

【讨论】:

  • 与 dialog.show();对话框显示两次并且打开的对话框没有取消,我仍然需要单击所有其他按钮。
  • 所以,您要做的是创建另一个显示对话框并在其中写入 dialogs.add(dialog) 和删除所有对话框 dialogs.remove(dialog) 并相应调用的另一种方法
猜你喜欢
  • 2018-11-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-11
  • 1970-01-01
  • 2014-05-19
  • 2020-05-05
相关资源
最近更新 更多