简体   繁体   English

如何在android中关闭AlertDialog

[英]How to dismiss AlertDialog in android

I created AlertDialog that contains 4 buttons我创建了包含 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();

I want to dismiss it after SetBackground() method, how can I do this?我想在 SetBackground() 方法之后关闭它,我该怎么做? thanks in advance.提前致谢。

Actually there is no any cancel() or dismiss() method from AlertDialog.Builder Class.实际上 AlertDialog.Builder 类中没有任何cancel()dismiss()方法。

So Instead of AlertDialog.Builder optionDialog use AlertDialog instance.所以代替AlertDialog.Builder optionDialog使用AlertDialog实例。

Like,喜欢,

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

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

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

I think there's a simpler solution: Just use the DialogInterface argument that is passed to the onClick method.我认为有一个更简单的解决方案:只需使用传递给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

            };  
        });

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

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

To dismiss or cancel AlertDialog.Builder关闭或取消 AlertDialog.Builder

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

you call dismiss() on the dialog interface你在对话框界面上调用dismiss()

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

There are two ways of closing an alert dialog.有两种方法可以关闭警报对话框。

Option 1:选项1:

AlertDialog#create().dismiss();

Option 2:选项 2:

The DialogInterface#dismiss();

Out of the box, the framework calls DialogInterface#dismiss();开箱即用,框架调用DialogInterface#dismiss(); when you define event listeners for the buttons:当您为按钮定义事件侦听器时:

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

for the Alert dialog.用于警报对话框。

With Kotlin:使用 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()
}

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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