微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

}

原文地址:https://www.jb51.cc/android/310940.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐