How to dismiss AlertDialog in android

后端 未结 7 1919
忘掉有多难
忘掉有多难 2020-11-29 05:43

I created AlertDialog that contains 4 buttons

OptionDialog = new AlertDialog.Builder(this);
        OptionDialog.setTitle(\"Options\");
        LayoutInflat         


        
相关标签:
7条回答
  • 2020-11-29 06:06

    Try this:

       AlertDialog.Builder builder = new AlertDialog.Builder(this);
       AlertDialog OptionDialog = builder.create();
      background.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                SetBackground();
           OptionDialog .dismiss();
            }
        });
    
    0 讨论(0)
  • 2020-11-29 06:16

    To dismiss or cancel AlertDialog.Builder

    dialog.setNegativeButton("إلغاء", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            dialogInterface.dismiss();
                        }
                    });
    

    you call dismiss() on the dialog interface

    0 讨论(0)
  • 2020-11-29 06:17

    Actually there is no any cancel() or dismiss() method from AlertDialog.Builder Class.

    So Instead of AlertDialog.Builder optionDialog use AlertDialog instance.

    Like,

    AlertDialog optionDialog = new AlertDialog.Builder(this).create();
    

    Now, Just call optionDialog.dismiss();

    background.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            SetBackground();
            // here I want to dismiss it after SetBackground() method 
            optionDialog.dismiss();
        }
    });
    
    0 讨论(0)
  • 2020-11-29 06:20

    I think there's a simpler solution: Just use the DialogInterface argument that is passed to the onClick method.

    AlertDialog.Builder db = new AlertDialog.Builder(context);
            db.setNegativeButton("cancel", new DialogInterface.OnClickListener(){
                @Override
                public void onClick(DialogInterface d, int arg1) {
                    db.cancel();
                    //here db.cancel will dismiss the builder
    
                };  
            });
    

    See, for example, http://www.mkyong.com/android/android-alert-dialog-example.

    0 讨论(0)
  • 2020-11-29 06:21

    Here is How I close my alertDialog

    lv_three.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
                @Override
                public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                    GetTalebeDataUser clickedObj = (GetTalebeDataUser) parent.getItemAtPosition(position);
                    alertDialog.setTitle(clickedObj.getAd());
                    alertDialog.setMessage("Öğrenci Bilgileri Güncelle?");
                    alertDialog.setIcon(R.drawable.ic_info);
                    // Setting Positive "Yes" Button
                    alertDialog.setPositiveButton("Tamam", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            // User pressed YES button. Write Logic Here
                        }
                    });
                    alertDialog.setNegativeButton("İptal", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            //alertDialog.
                            alertDialog.setCancelable(true); // HERE
    
                        }
                    });
                    alertDialog.show();
                    return true;
                }
            });
    
    0 讨论(0)
  • 2020-11-29 06:22

    There are two ways of closing an alert dialog.

    Option 1:

    AlertDialog#create().dismiss();

    Option 2:

    The DialogInterface#dismiss();

    Out of the box, the framework calls DialogInterface#dismiss(); when you define event listeners for the buttons:

    1. AlertDialog#setNegativeButton();
    2. AlertDialog#setPositiveButton();
    3. AlertDialog#setNeutralButton();

    for the Alert dialog.

    0 讨论(0)
提交回复
热议问题