gpt4 book ai didi

android - 关闭自定义对话框?

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:20:55 27 4
gpt4 key购买 nike

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

//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 - 关闭自定义对话框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2825272/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com