关闭自定义对话框?

YaW*_*YaW 9 android dialog

我正在尝试创建一个自定义对话框以在此对话框中显示视图.这是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();
Run Code Online (Sandbox Code Playgroud)

所以,我想解雇btDialogSimple的onClick中的对话框.我怎么能这样做?我不知道如何从onclicklistener中调用dismiss方法.

我的按钮有自定义布局,所以我不想制作builder.setPositiveButton.

有任何想法吗?

Pen*_*m10 17

您必须将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();
Run Code Online (Sandbox Code Playgroud)

}


sou*_*rar 7

我认为更好的方式可能是打电话

dismissDialog(DIALOG_ID);
Run Code Online (Sandbox Code Playgroud)

不会使AlertDialog成为类属性会失败从onCreateDialog()返回Dialog的目的吗?

  • 如何使用**自定义**对话框? (2认同)