How do I show only one Dialog at a time?

最后都变了- 提交于 2019-12-18 19:21:45

问题


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?

Here is my code:

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

回答1:


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.

So something like:

boolean dialogShown;

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



回答2:


you need to check if dialog isShowing or not

Dialog has an isShowing() method that should return if the dialog is currently visible.

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();
  }



回答3:


Create a positive or negative button for it and just call it as OK and use it to dismiss. Something like :

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();



回答4:


For every push on the button you call the method. So this is why it is shown multile times.

The easiest way is to just define an instance variable in your class of your code like:

boolean alertIsBeingShown = false;

Then set it to true when the alert is being shown like this

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

    }
 });

and set the variable to false in the code where you press the OK to make it disappear.




回答5:


When I came across this problem, I was not able to use Flags. I had to show a dialog for a clicked list item in a RecyclerView.

In the onclick method I created a variable for the dialog and then when building the dialog I enclosed it with an if statement that checks if the AlertDialog variable is null. When the user clicks on a list item the first time the dialog appears, because the variable is null, even if the user clicks on a item twice only one dialog will appear, because after the second click the AlertDialog variable is no longer null. When the user dismissed the AlertDialog the variable is set to null again.

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();
        }



回答6:


Create the dialog in a try-catch-block like this:

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

The first time you execute this, the NullPointerException is thrown and the dialog is created. The following times nothing visible will really happen.



来源:https://stackoverflow.com/questions/12560931/how-do-i-show-only-one-dialog-at-a-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!