如何一次只显示一个对话框? [英] How do I show only one Dialog at a time?

查看:127
本文介绍了如何一次只显示一个对话框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Android应用程序在单击按钮时显示AlertDialog.当我多次单击按钮时,将创建一个以上的对话框.我该如何解决?

My Android application shows an AlertDialog on a button click. When I click on the button more than once more than one Dialog is created. How can I fix this?

这是我的代码:

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

推荐答案

您可以创建一个全局标志(布尔值),如果显示对话框,该标志设置为true吗?如果用户单击确定",是",否"或其他任何内容,则关闭对话框,并将标记设置为false.

You can create a global flag (boolean) that is set to true if a dialog is shown? If the user click ok, yes, no or anything the dialog is closed and you set the flag to false.

所以像这样:

boolean dialogShown;

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

这篇关于如何一次只显示一个对话框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆