2014-11-25 172 views
11

这里是我的代码 -如何检查AlertDialog.builder是否显示并取消显示?

View layout = LayoutInflater.from(this).inflate(R.layout.dialog_loc_info, null); 
final Button mButton_Mobile = (Button) layout.findViewById(R.id.button); 
AlertDialog.Builder builder = new AlertDialog.Builder(this); 
builder.setView(layout); 
mButton_Mobile.setOnClickListener(new OnClickListener() { 
    @Override 
    public void onClick(View arg0) { 
     if(builder.) 
      showDialog(); // this is another dialog, nothing to do with this code 
     } 
    }); 
builder.setNeutralButton(getString(android.R.string.ok), 
         new DialogInterface.OnClickListener() { 
    @Override 
    public void onClick(DialogInterface dialog, int which) { 
     dialog.dismiss(); 
    } 
}); 
builder.show(); 
+3

注意添加评论为什么downvoted这个问题?这将有助于提升我的质量。 – Darpan 2014-11-25 14:05:55

回答

31

您可以使用AlertDialog方法。

AlertDialog alert = new AlertDialog.Builder(context).create(); 

if (alert.isShowing()) { 
    alert.dismiss(); 
} 

希望它有帮助。

0

你可以用这个检查:

if(alert != null && alert.isShowing()){ 
    alert.show();// or alert.dismiss() it 
} 
+0

@Darpan tnx您提到的,但我从'builder'名称使用,因为与问题所有者对象AlertDialog.Builder相等。 – 2014-11-25 13:55:59

+1

这里是文档http://developer.android.com/reference/android/app/AlertDialog.Builder.html。 isShowing()函数无处可用。尝试运行你的代码可能是。 – Darpan 2014-11-25 13:59:20

+0

isShowing在构建器上不存在 – 2015-01-29 19:26:39

0

AlertDialog延伸Dialog具有isShowing()

提示:AlertDialog.Builder创建一个AlertDialog实例。 :)

+0

已经得到/接受答案 – user2450263 2014-11-25 14:02:42

2

另一种方法是使用一种方法与生成器生成AlertDialog,然后在将AlertDialog设置为类变量时创建AlertDialog而不显示它。

AlertDialog mAlertDialog; 

public showMyAlertDialog(View layout){ 

    AlertDialog.Builder builder = new AlertDialog.Builder(getContext()); 

    builder.setView(layout); 

    builder.setNeutralButton(getString(android.R.string.ok),new DialogInterface.OnClickListener() { 

     @Override 
     public void onClick(DialogInterface dialog, int which) { 
      dialog.dismiss(); 
      mAlertDialog = null; //setting to null is not required persay 
     } 

    }); 

    mAlertDialog = builder.create() 
    mAlertDialog.show(); 
} 

public boolean isAlertDialogShowing(AlertDialog thisAlertDialog){ 
    if(thisAlertDialog != null){ 
     return thisAlertDialog.isShowing(); 
    } 
} 

希望应当理解如何使用此源:

然后用.isShowing();方法

实施例检查。 欢呼声

相关问题