如何在安卓中关闭AlertDialog?

52

我创建了一个包含4个按钮的AlertDialog

OptionDialog = new AlertDialog.Builder(this);
        OptionDialog.setTitle("Options");
        LayoutInflater li = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View v = li.inflate(R.layout.options, null, false);
        background = (Button) v.findViewById(R.id.bkgSpinnerLabel);
        SoundVib = (Button) v.findViewById(R.id.SoundVibSpinnerLabel);
        
        OptionDialog.setView(v);
        OptionDialog.setCancelable(true);
        OptionDialog.setNeutralButton("Ok",
                new DialogInterface.OnClickListener() {
                
                    public void onClick(DialogInterface arg0, int arg1) {
                    }
                });
        background.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                SetBackground();
             // here I want to dismiss it after SetBackground() method 
            }
        });
        

        SoundVib.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                Intent soundVibIntent = new Intent(SebhaActivity.this, EditPreferences.class);
                startActivity(soundVibIntent);
            }
        });
        
        OptionDialog.show();

在SetBackground()方法之后,我想要把它解雇掉,该怎么做?


4
请使用Java命名规范:方法和变量名应以小写字母开头。 - nhaarman
在 SetBackground() 后使用 dialog.dismiss();。 - saran
1
@DaanGeurts - AlertDialog.Builder类中没有任何名为dismiss()的方法。 - user370305
@user370305 好的,我漏掉了那个,你的答案应该可行。 - Daan Geurts
1
OptionDialog.setView(null); - Vicky
我发现关闭AlertDialog最简单的方法是在按钮的onPressed:属性中添加Navigator.pop(context);当一切都保持简单并行时,这特别有用。 - malikilam
8个回答

152

实际上,AlertDialog.Builder类中没有任何cancel()dismiss()方法。

因此,不要使用AlertDialog.Builder optionDialog,而是使用AlertDialog实例。

例如,

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

现在,只需调用 optionDialog.dismiss();

background.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {
        SetBackground();
        // here I want to dismiss it after SetBackground() method 
        optionDialog.dismiss();
    }
});

非常好,重要的部分是:如果不创建(AlertDialog OptionDialog =new AlertDialog.Builder(this).create();),则不要创建(AlertDialog.Builder OptionDialog = AlertDialog.Builder(getActivity());),之后可以调用.dismiss(); 太棒了,谢谢!!!! - user3402040
这里需要注意的是,在上面的代码片段中,OptionDialog 需要对 onclick 监听器可见。一种实现方法是在父活动类中将 AlertDialog 声明为成员变量。 - Tim Biegeleisen
2
在关闭选项对话框后,我应该执行 optionDialog = null 以避免内存泄漏吗? - Basheer AL-MOMANI

65

我认为有一个更简单的解决方案:只需使用传递给onClick方法的DialogInterface参数。

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

            };  
        });

例如,可以参考http://www.mkyong.com/android/android-alert-dialog-example


7
"d.dismiss();" 对我来说很有效,是的,这种方法更简单且适用于 AlertDialog。 - rupinderjeet
我相信这仍然是正确的答案。 - Panama Jack

11

试试这个:

   AlertDialog.Builder builder = new AlertDialog.Builder(this);
   AlertDialog OptionDialog = builder.create();
  background.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            SetBackground();
       OptionDialog .dismiss();
        }
    });

2
我之前尝试过这个,但直到我使用了被接受的答案中的代码才成功。 - Emy Alsabbagh
好的,我错过了这一行代码:AlertDialog.Builder builder = new AlertDialog.Builder(this); 现在编辑答案... - Android_coder
@Android_coder 在片段中该怎么办?this将为空。 - Slick Slime

6

如何关闭或取消 AlertDialog.Builder

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

你需要在对话框接口上调用 dismiss() 方法。

2
这是我如何关闭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;
            }
        });

1

使用Kotlin:

fun Activity.showDialog() {
    val builder = AlertDialog.Builder(this)
    builder.setMessage(R.string.message_id)
        .setPositiveButton(
            R.string.confirm_action_text_id
        ) { dialogInterface, _ ->
            dialogInterface.dismiss()
            SignUpActivity.start(this)
        }
        .setNegativeButton(R.string.cancel) { _, _ -> }
    builder.create().show()
}

1
有两种关闭警告对话框的方法。
选项1:

AlertDialog#create().dismiss();

选项2:

The DialogInterface#dismiss();

框架默认情况下,在为按钮定义事件监听器时,会调用DialogInterface#dismiss();

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

对于警报对话框。


-2

只需将视图设置为null,即可关闭AlertDialog。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接