简体   繁体   English

如何检查警报是可见的android

[英]how to check alert is visible android

how to check if my alert is already showing on screen ? 如何检查我的警报是否已经在屏幕上显示?

 AlertDialog.Builder alert = new AlertDialog.Builder(this);

 alert.show();

I can maintain state by adding a flag in my code to set and reset, but if there is already a method that I can re use ? 我可以通过在我的代码中添加一个标志来设置和重置来维护状态,但是如果已经有一个我可以重用的方法呢?

There is no isShowing() method on the AlertDialog.Builder class. AlertDialog.Builder类上没有isShowing()方法。 There is one on the Dialog class though. 但是Dialog类上有一个。

AlertDialog.Builder AlertDialog.Builder

Dialog 对话

An AlertDialog.Builder is used to create an AlertDialog. AlertDialog.Builder用于创建AlertDialog。 Once you have an instance of an AlertDialog, you can determine whether or not it is still showing by then calling isShowing() on it. 一旦你有一个AlertDialog的实例,你就可以通过在它上面调用isShowing()来确定它是否仍在显示。

AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
AlertDialog alertDialog = alertDialogBuilder.create();

if(!alertDialog.isShowing()){   
  //if its visibility is not showing then show here 
   alertDialog.show();       
 }else{
  //do something here... if already showing       
  }

Yes you can check it with isShowing(); 是的你可以用isShowing(); method it's documented in the Android Documentation too 方法也在Android文档中记录

But in your case you need to catch the AlertDialog that build by the AlertDialog.Builder first. 但在您的情况下,您需要首先捕获由AlertDialog.Builder构建的AlertDialog。
so your code should be like this 所以你的代码应该是这样的

AlertDialog alertDialog;

function showDialog() {
    if(alertDialog == null) { 
        //Initial Creation will always show 
        //or you can just use create() if you don't want to show it at initial creation
        AlertDialog.Builder alert = new AlertDialog.Builder(this);
        AlertDialog alertDialog = alert.show();
    else {
        if(alertDialog.isShowing()) {
             alertDialog.hide();
        } else {
             alertDialog.show();
        }
    }
}

Use this: 用这个:

AlertDialog alertDialog = alert.create();

//to check if its being shown
if(!alertDialog.isShowing()){
    //do something
    alertDialog.show();
}

It will return true if currently that alert dialog is being shown. 如果当前显示警报对话框,它将返回true So in your case, check if it returns false , and then show it. 因此,在您的情况下,检查它是否返回false ,然后显示它。

Hope it helps. 希望能帮助到你。

您可以使用对话框的isShowing方法,或者您可以在创建警告对话框标志为0时显示标志,并在显示后将其设置为1。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM