android – 关闭自定义对话框?

我正在尝试创建一个自定义对话框来显示此对话框中的视图.这是Builder代码
//Getting the layout
LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.custom_dialog_simple,(ViewGroup) findViewById(R.id.rlDialogSimple));

//Change Text and on click
TextView tvDialogSimple = (TextView) layout.findViewById(R.id.tvDialogSimple);
tvDialogSimple.setText(R.string.avisoComprobar);
Button btDialogSimple = (Button) layout.findViewById(R.id.btDialogSimple);
btDialogSimple.setonClickListener(new OnClickListener() {

    public void onClick(View v) {
        //Do some stuff

        //Here i want to close the dialog
    }
});

AlertDialog.Builder builder = new AlertDialog.Builder(AcPanelEditor.this);
builder.setView(layout);
AlertDialog alert = builder.create();
alert.show();

所以,我想在btDialogSimple的onClick中关闭对话框.我该怎么办?我不知道如何从onclicklistener内调用dismiss方法.

我的按钮有一个自定义的布局,所以我不想做一个builder.setPositiveButton.

有任何想法吗?

解决方法

您必须将AlertDialog保存到您的父类属性中,然后使用以下内容
class parentClass ........ {
private AlertDialog alert=null;
........
public void onClick(View v) {
        //Do some stuff

        //Here i want to close the dialog
        if (parentClass.this.alert!=null)            
        parentClass.this.alert.dismiss();
    }
........
this.alert = builder.create();
this.alert.show();

}

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...