如何在android中关闭AlertDialog [英] How to dismiss AlertDialog in android

查看:32
本文介绍了如何在android中关闭AlertDialog的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I created AlertDialog that contains 4 buttons

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() 方法后关闭它,我该怎么做?提前致谢.

I want to dismiss it after SetBackground() method, how can I do this? thanks in advance.

推荐答案

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

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

所以代替 AlertDialog.Builder optionDialog 使用 AlertDialog 实例.

So Instead of AlertDialog.Builder optionDialog use AlertDialog instance.

喜欢,

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

这篇关于如何在android中关闭AlertDialog的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆