如何解除android中的AlertDialog

Emy*_*agh 44 android dismiss android-alertdialog

我创建了包含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();
Run Code Online (Sandbox Code Playgroud)

我想在SetBackground()方法之后解雇它,我该怎么做呢?提前致谢.

use*_*305 127

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

所以不要AlertDialog.Builder optionDialog使用AlertDialog实例.

喜欢,

AlertDialog optionDialog = new AlertDialog.Builder(this).create();
Run Code Online (Sandbox Code Playgroud)

现在,请致电 optionDialog.dismiss();

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

  • 我应该在解除后执行`optionDialog = null` 以避免内存泄漏吗? (2认同)

Joh*_*mer 61

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

AlertDialog.Builder db = new AlertDialog.Builder(context);
        db.setNegativeButton("cancel", new DialogInterface.OnClickListener(){
            @Override
            public void onClick(DialogInterface d, int arg1) {
                d.cancel();
            };  
        });
Run Code Online (Sandbox Code Playgroud)

例如,请参见http://www.mkyong.com/android/android-alert-dialog-example.

  • `d.dismiss();`为我工作,是的,这个更简单,也是AlertDialog原生的 (2认同)

And*_*der 10

试试这个:

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

  • 我以前试过这个,直到我使用接受的答案中的代码才对我有用。 (2认同)

小智 5

关闭或取消 AlertDialog.Builder

dialog.setNegativeButton("?????", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        dialogInterface.dismiss();
                    }
                });
Run Code Online (Sandbox Code Playgroud)

你在对话框界面上调用dismiss()