2013-07-25 21 views
0

我有一个alertDialog,我使用AlertDialog.builder构建。如何关闭使用自定义xml文件的android对话框

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

在该布局中我使用onclick方法进行图像浏览。

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

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

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

感谢任何帮助:)

编辑:我的代码`AlertDialog.Builder警报=新AlertDialog.Builder(本);

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

和颜色拾取布局有点击时运行changecolor方法16个不同的imageviews(即chenges当前颜色)

回答

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

好吧,我会认为你对关闭按钮在你的布局,你可以做的是改变

Dialog d=alert.create();` 

到一个全局变量然后设置一个onclick侦听您的按钮

YOUR_BUTTON.setOnClickListener(new OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      YOUR_DIALOG.dismiss(); 
     } 
    });  
+0

我没有一个按钮,用于关闭:(只需要16个imageviews – Folke

+0

好,然后看到我以前的答案,做alert.setNegativeButton ......这将添加一个按钮,请参考http://developer.android.com/guide/topics/ui/dialogs.html#AlertDialog – user1634451