【问题标题】:How to dismiss AlertDialog in android如何在android中关闭AlertDialog
【发布时间】:2013-01-28 23:39:25
【问题描述】:

我创建了包含 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() 方法之后关闭它,我该怎么做? 提前致谢。

【问题讨论】:

  • 请使用java命名约定:方法和变量名应该以小写字母开头。
  • 使用 dialog.dismiss();在 SetBackground(); 之后
  • @DaanGeurts - AlertDialog.Builder 类中没有任何 dismiss() 方法。
  • @user370305 对,我错过了那个,你的答案应该有效
  • OptionDialog.setView(null);

标签: android android-alertdialog dismiss


【解决方案1】:

实际上 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();
    }
});

【讨论】:

  • 真的很好,重要的部分是:“NOT CREATE A (AlertDialog.Builder OptionDialog = AlertDialog.Builder(getActivity()); ) 如果不创建 (AlertDialog OptionDialog =new AlertDialog.Builder(this) .create(); ) 之后你可以调用 .dismiss(); 太好了,谢谢!!!!!
  • 这里需要注意的是,OptionDialog 需要对上面 sn-p 中的 onclick 侦听器可见。一种方法是将AlertDialog 声明为父活动类中的成员变量。
  • 我应该在关闭它之后使用optionDialog = null 以避免内存泄漏吗?
【解决方案2】:

我认为有一个更简单的解决方案:只需使用传递给 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

【讨论】:

  • d.dismiss(); 为我工作,是的,这个更简单,更原生于 AlertDialog
【解决方案3】:

试试这个:

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

【讨论】:

  • 我之前试过这个,但直到我使用了接受的答案中的代码后才对我有用。
  • 好的,我错过了这条线 AlertDialog.Builder builder = new AlertDialog.Builder(this);现在编辑答案...
  • @Android_coder 在片段中时该怎么办? this 将为空。
【解决方案4】:

关闭或取消 AlertDialog.Builder

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

你在对话界面调用dismiss()

【讨论】:

    【解决方案5】:

    这是我关闭警报对话框的方法

    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;
                }
            });
    

    【讨论】:

      【解决方案6】:

      有两种方法可以关闭警报对话框。

      选项 1:

      AlertDialog#create().dismiss();

      选项 2:

      The DialogInterface#dismiss();

      当您为按钮定义事件侦听器时,框架会立即调用DialogInterface#dismiss();

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

      用于警报对话框。

      【讨论】:

        【解决方案7】:

        只需将视图设置为 null 即可简单地关闭 AlertDialog。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2012-05-04
          • 1970-01-01
          • 1970-01-01
          • 2018-10-23
          • 1970-01-01
          • 1970-01-01
          • 2017-10-24
          相关资源
          最近更新 更多