如何一次只显示一个对话框?

12

我的Android应用程序在按钮点击时显示一个AlertDialog。当我点击按钮超过一次时,就会创建多个对话框。如何解决这个问题?

以下是我的代码:

button.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        dialog =  new AlertDialog.Builder(context);             
        dialog.show();
    }
});

我使用一个自定义类继承AlertDialog,而不是AlertDialog.Builder。该类显示一张图片,没有按钮。 - Asha Soman
1
你可以仍然使用相同的概念,只需创建一种标志类型 :) - Araw
7个回答

22

你需要检查 dialog 是否正在显示。

Dialog 有一个 isShowing() 方法,该方法应返回对话框当前是否可见。

public AlertDialog myDialog;

public void showDialog(Context context) {
    if( myDialog != null && myDialog.isShowing() ) return;

    AlertDialog.Builder builder = new AlertDialog.Builder(context);
    builder.setTitle("Title");
    builder.setMessage("Message");
    builder.setPositiveButton("ok", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int arg1) {
                dialog.dismiss();
            }});
    builder.setCancelable(false);
    myDialog = builder.create();
    myDialog.show();
  }

错误示例。 错误场景(Android 9):
  1. 显示对话框。
  2. 切换到另一个应用程序。
  3. 在15秒内,操作系统将应用程序切换到后台模式。
  4. 返回应用程序 - 对话框将被移除,但代码认为对话框已显示: if (myDialog!= null && myDialog.isShowing()) return;
- Gregory

7
你可以创建一个全局标志(布尔值),如果显示对话框,则将其设置为true。 如果用户单击“确定”,“是”,“否”或任何内容,对话框将关闭并将标志设置为false。
因此,类似于以下内容:
boolean dialogShown;

If(dialogShown)
{
  return;
}
else
{
  dialogShown = true;
  dialog =  new AlertDialog.Builder(context);              
  dialog.show();
}

2
始终将代码块放在括号中,即使只有一行。这样可以避免很多麻烦(特别是在使用预处理器和宏的语言中)。 - Marcin Orlowski

1

每次点击按钮时,都会调用该方法。这就是为什么它会显示多次的原因。

最简单的方法就是在代码类中定义一个实例变量,例如:

boolean alertIsBeingShown = false;

当警报被显示时,将其设置为 true

button.setOnClickListener(new OnClickListener() {
           @Override
        public void onClick(View v) {
               if (alertIsBeingShown) return;
               alertIsBeingShown = true;
               dialog =  new AlertDialog.Builder(context);              
               dialog.show();

    }
 });

在按下“确定”使其消失的代码中,将变量设置为false。

你为什么要使用YES和NO? - Roman Holzner
2
我猜可能是太多的Objective-C了。 - Alberto M

0
创建一个正面或负面按钮,将其命名为“确定”并使用它来关闭。类似于:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Look at this dialog!")
       .setCancelable(false)
       .setPositiveButton("OK", new DialogInterface.OnClickListener() {
           public void onClick(DialogInterface dialog, int id) {
                //do things
           }
       });
AlertDialog alert = builder.create();
alert.show();

0

当我遇到这个问题时,我无法使用Flags。我必须在RecyclerView中的已点击列表项上显示对话框。

onclick方法中,我为对话框创建了一个变量,然后在构建对话框时,我将其包含在if语句中,该语句检查AlertDialog变量是否为空。当用户第一次单击列表项时,对话框会出现,因为变量为空,即使用户两次单击项目,也只会出现一个对话框,因为在第二次单击后,AlertDialog变量不再为空。当用户关闭AlertDialog时,变量再次设置为null。

AlertDialog alertDialog;

if(alertDialog == null) {

            alertDialog = new AlertDialog.Builder(MyActivity.this)
                    .setTitle("Title for Dialog")
                    .setMessage("Dialog Message")
                    .setPositiveButton("Okay", null)
                    .setNegativeButton("No", null)
                    .setOnDismissListener(new DialogInterface.OnDismissListener() {
                        @Override
                        public void onDismiss(DialogInterface dialogInterface) {

                            alertDialog = null;

                        }
                    })
                    .show();
        }

0
boolean dialogShown;

if (!dialogShown) {

  dialogShown = true;
  dialog =  new AlertDialog.Builder(context);  
  dialog .setNegativeButton(...)
  dialog .setCancelable(false);
  dialog.show();
}

在负面点击内部:

dialogShown=false;

-1

请像这样在try-catch块中创建对话框:

    try {
        dialog.setVisible(true);
    } catch (NullPointerException e) {
        dialog =  new AlertDialog.Builder(context);              
        dialog.show();
    }

第一次执行时,会抛出NullPointerException并创建对话框。接下来的几次执行不会有任何可见的变化。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接