辞退一个自定义对话框?自定义、对话框

2023-09-13 01:31:25 作者:落败的唯美丶

我试图使自定义对话框,显示在此对话框中查看。这是生成器code:

I'm trying to make a custom dialog to show a view in this dialog. This is the Builder code:

//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内的解雇方式。

So, i want to dismiss the dialog in the onClick of btDialogSimple. How i can do it? I don't know how to call the dismiss method from inside a onclicklistener.

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

My buttons have a custom layout, so i don't want to make a builder.setPositiveButton.

任何想法?

推荐答案

您必须将AlertDialog保存到你的父类的属性,然后使用是这样的:

You have to save the AlertDialog to your parent class property, then use something like this:

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

}