Android 关闭自定义对话框?

Android 关闭自定义对话框?,android,dialog,Android,Dialog,我正在尝试创建一个自定义对话框,以在此对话框中显示视图。这是生成器代码: //Getting the layout LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE); View layout = inflater.inflate(R.layout.custom_dialog_simple, (ViewGroup) f

我正在尝试创建一个自定义对话框,以在此对话框中显示视图。这是生成器代码:

//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时取消对话框。我怎么能做到?我不知道如何从onclicklistener内部调用Disclose方法

我的按钮具有自定义布局,因此我不想创建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();

}

我想最好的办法是打电话

dismissDialog(DIALOG_ID);
将AlertDialog设置为类属性是否会违背从onCreateDialog()返回对话框的目的

public  Dialog dialog;

buton_sil.setOnClickListener(new View.OnClickListener() {

            @ Override
            public void onClick(View v) {

                   DialogClose();
                   }
}):


public void DialogClose() {
        dialog.dismiss();
    }

使用
AlertDialog
实例,例如
mAlertDialog
作为全局变量。并调用
mAlertDialog.discouse()内部
onClick()

好问题!这正是它似乎应该起作用的方式。但是,当然,这不——那很容易!(不是安卓的方式)。对我也很有效。但是,由于我有一个自定义对话框构建为xml文件,所以我不需要构建它,只需使用setContentView(自定义对话框)。@pentium10。。。我也遇到同样的问题,但对我来说不起作用