如何关闭使用自定义xml文件的android对话框

时间:2013-07-25 00:31:37

标签: android xml dialog alertdialog android-alertdialog

我有一个使用AlertDialog.builder构建的alertDialog。

然后我给它一个自定义的xml布局。

在该布局中,我有onclick方法的图像视图。

当我点击它们时,我想运行一些效果很好的代码,但我也想关闭对话窗口。

我该怎么做?我似乎没有对话框的引用,我不能给对话框一个ID?

现在我将对话框保存到公共字段,然后通过指向该引用来关闭它,但这似乎是一种可怕的方式。

感谢任何帮助:)

编辑:我的代码`AlertDialog.Builder alert = new AlertDialog.Builder(this);

    alert.setTitle("Choose Color");
    LayoutInflater inflater = this.getLayoutInflater();
    View v = inflater.inflate(R.layout.colorpicker, null);
    alert.setView(v);
    Dialog d=alert.create();`

并且colorpicker布局有16个不同的图像视图,在单击时运行changecolor方法(使当前颜色变为现实)

2 个答案:

答案 0 :(得分:0)

为了最好地回答您的问题,我们需要您发布代码示例,但您可以尝试这个

.setNegativeButton(android.R.string.cancel,new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog,int id) {
                    // if this button is clicked, just close
                    // the dialog box and do nothing
                    dialog.cancel();
                }
            });

答案 1 :(得分:0)

好的我会假设您的关闭按钮位于您的布局中,所以您可以做的是更改

Dialog d=alert.create();`

到一个全局变量,然后为你的按钮设置一个onclick监听器

YOUR_BUTTON.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            YOUR_DIALOG.dismiss(); 
        }
    });    
相关问题