AlertDialog dismiss not working

2019-06-20 19:44发布

I have the following AlertDialog:

AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(mContext);
dialogBuilder.setTitle(R.string.title);
dialogBuilder.setMessage(mContext.getString(R.string.message));
dialogBuilder.setPositiveButton(R.string.positive, new MyOnClickListener());
dialogBuilder.setNegativeButton(R.string.negative, new MyOnClickListener());
dialogBuilder.show();

with this ClickListener

public static class MyOnClickListener implements DialogInterface.OnClickListener{
    @Override
    public void onClick(DialogInterface dialog, int which) {
      dialog.dismiss();
    }
  }

I would expect the dialog to be closed, when clicking on either of the buttons, but the dialog stays open instead.

I debugged the onClick method and the line

dialog.dismiss() 

is being executed, but nothing happens.

Where am I going wrong or how can I fix this?

6条回答
Juvenile、少年°
2楼-- · 2019-06-20 20:08

Try this

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

    // Setting Dialog Title
    alertDialog.setTitle("Alert Dialog");

    // Setting Dialog Message
    alertDialog.setMessage("Welcome to AndroidHive.info");

    // Setting Icon to Dialog
    alertDialog.setIcon(R.drawable.tick);

    // Setting OK Button
    alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
            // Write your code here to execute after dialog closed
            Toast.makeText(getApplicationContext(), "You clicked on OK", Toast.LENGTH_SHORT).show();
            }
    });

    // Showing Alert Message
    alertDialog.show();
查看更多
Summer. ? 凉城
3楼-- · 2019-06-20 20:09

Declare your AlertDialog at the top like:

private AlertDialog myAlertdialog;

than replace your dialogBuilder.show(); with

myAlertDialog = dialogBuilder.create();
myAlertDialog.show();

than u can call myAlertDialog.dismiss();

查看更多
趁早两清
4楼-- · 2019-06-20 20:13

try this ... it worked for me ... You can use this method for your dialog or you can simply use alert.dismiss(); to dismiss the dialog .... or just put the dialog method in your code and call the method i.e. MyAlertDialogBox(); wherever you want a dialog box .

AlertDialog alert;
// Dialog method .
private void MyAlertDialogBox(){
    AlertDialog.Builder builder = new AlertDialog.Builder(this);

    builder.setMessage("Welcome ! This is a Alert dialog")
            .setCancelable(false)
            //You can name you strings according to your preference here 
            //or the String.xml file
            .setPositiveButton(getResources().getString(R.string.yes), new DialogInterface.OnClickListener()
            {
                public void onClick(final DialogInterface dialog, int id) {
                    Toast.makeText(MyActivityName.this, "Toast message 
 when you click Yes ", Toast.LENGTH_SHORT).show();
                }
            })
            .setNegativeButton( getResources().getString(R.string.no), 
 new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    Toast.makeText(MyActivityName.this, "Toast message 
 when you click No ", Toast.LENGTH_SHORT).show();

   //                        To dismiss your alert dialog box on click of 
     No button (or you can say the negative button)
                    alert.dismiss();
                }
            });

    //Creating dialog box
    alert = builder.create();
    /*   To give dialog box title   
        alert.setTitle(getResources().getString(R.string.folder));*/

     /*    to set icon in alert dialog box
    alert.setIcon(R.drawable.ic_create_new_folder_white_24dp);*/
    alert.show();
}
查看更多
迷人小祖宗
5楼-- · 2019-06-20 20:16

Trying below code while using interface to create your own dialog button click listener.

MyOnClickListener.java

import android.content.DialogInterface;

public interface MyOnClickListener extends DialogInterface.OnClickListener {
    @Override
    public void onClick(DialogInterface dialog, int which);
}

Your dialog Code:

AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(MainActivity.this);
    dialogBuilder.setTitle("You Title");
    dialogBuilder.setMessage("Here Is your message");
    dialogBuilder.setPositiveButton("YES", new MyOnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            Toast.makeText(MainActivity.this, "YES Clicked", Toast.LENGTH_LONG).show();
dialog.dismiss();
        }
    });
    dialogBuilder.setNegativeButton("NO", new MyOnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            Toast.makeText(MainActivity.this, "NO Clicked", Toast.LENGTH_LONG).show();
dialog.dismiss();
        }
    });
    dialogBuilder.show();

Output:

Open your dialog:

Your dialog

Performing OnClick of dialog:

OnClick of dialog

I hope its helps you.

查看更多
狗以群分
6楼-- · 2019-06-20 20:21

When you change the line dialogBuilder.show() to dialogBuilder.create().show() it should be working correctly.

查看更多
地球回转人心会变
7楼-- · 2019-06-20 20:26

Instead of dialog.dismiss() , use dialoginterface.dismiss() .

I've shown an example below

public void onClick(DialogInterface dialogInterface, int i) 
{
    dialogInterface.dismiss();
} 
查看更多
登录 后发表回答