我正在尝试创建一个自定义对话框来显示此对话框中的视图.这是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();
}