简体   繁体   中英

How can dismiss this alert dialog?

I have been working on this for an hour, I can't figure out why I can't dismiss the dialog even after click either of the buttons. If I put b.dismiss(), after the buttons the dialog doesn't even show.

   private AlertDialog b;

   // custom dialog
    AlertDialog.Builder dialogBuilder = new 
    AlertDialog.Builder(MapsActivity.this);
    LayoutInflater inflater = MapsActivity.this.getLayoutInflater();
    final View dialogView = inflater.inflate(R.layout.custom_dialog, null);
    dialogBuilder.setView(dialogView);

  Button reject = (Button) dialogView.findViewById(R.id.reject_btn);
  Button accept = (Button) dialogView.findViewById(R.id.accept_btn);

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


    accept.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            b.dismiss();



        }
    });

    reject.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            b.dismiss();


        }
    });

I have tried dismissing it in

   dialogBuilder.setPositiveButton("Done", new 
     DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int whichButton) {
        //do something with edt.getText().toString();
     }
});
dialogBuilder.setNegativeButton("Cancel", new 
  DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int whichButton) {
        //pass
    }
});

That didn't work

If you can pick up something that I missed your assistance would be most appreciated!

I have noticed that if I dismiss it about 60 times it dismisses does this mean that, my code is creating a dialog 60 times, what could be the cause of that?

edit

try this

     dialogBuilder.setPositiveButton("Done", new 
     DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int whichButton) {
        //do something with edt.getText().toString();
        dialogBuilder.dismiss(); 
        //  or
         dialogBuilder.cancel();
     }
});
dialogBuilder.setNegativeButton("Cancel", new 
  DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int whichButton) {
    dialogBuilder.dismiss(); 
      // or
     dialogBuilder.cancel();
    }
});

try this to dismiss the dialog

dialogBuilder.setPositiveButton("Done", new
     DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
            //do something with edt.getText().toString();
            dialog.dismiss();
        }
});

dialogBuilder.setNegativeButton("Cancel", new
     DialogInterface.OnClickListener() {
       public void onClick(DialogInterface dialog, int whichButton) {
            dialog.dismiss();
       }
});

try this

AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage("message");
        builder.setPositiveButton("Yes", new
 DialogInterface.OnClickListener() {
   public void onClick(DialogInterface dialog, int whichButton) {
        dialog.dismiss();

        });

        builder.setNegativeButton("NO",new
 DialogInterface.OnClickListener() {
   public void onClick(DialogInterface dialog, int whichButton) {
        dialog.dismiss();
        });
        builder.show();

I don't know where you have written your code to launch the dialog. I have replicated your code. And this works as expected.

public class MainActivity extends AppCompatActivity {

    private AlertDialog b;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // custom dialog
        AlertDialog.Builder dialogBuilder = new
                AlertDialog.Builder(this);
        LayoutInflater inflater = getLayoutInflater();
        final View dialogView = inflater.inflate(R.layout.custom_dialog, null);
        dialogBuilder.setView(dialogView);

        Button reject = (Button) dialogView.findViewById(R.id.reject_btn);
        Button accept = (Button) dialogView.findViewById(R.id.accept_btn);

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


        accept.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                b.dismiss();
            }
        });

        reject.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                b.dismiss();
            }
        });
    }
}

And below is the XML. Again a very basic one to match your code

custom_dialog.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <Button
        android:layout_width="wrap_content"
        android:id="@+id/reject_btn"
        android:text="Reject"
        android:layout_height="wrap_content" />
    <Button
        android:layout_width="wrap_content"
        android:id="@+id/accept_btn"
        android:text="Accept"
        android:layout_height="wrap_content" />
</LinearLayout>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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