[英].dismiss() is showing an error in AlertDialog
我有以下代码:
AlertDialog.Builder b = new AlertDialog.Builder(getActivity());
View view = LayoutInflater.from(getActivity()).inflate(R.layout.displayfilecontents, null);
EditText text = (EditText) view.findViewById(R.id.etFileContents);
if (text != null) {
text.setFocusable(false);
text.setLongClickable(false);
text.setTextIsSelectable(false);
}
text.setText(builder);
b.setView(view);
b.setTitle("Trip Name: " + FilesInFolder.get(position).toString().substring(0, FilesInFolder.get(position).toString().lastIndexOf(".")));
Button btnCloseIt = (Button) view.findViewById(R.id.btnClose);
btnCloseIt.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
b.dismiss();
}
});
AlertDialog dl = b.create();
dl.show();
我想一旦关闭该对话框btnCloseIt
被按下。 我在这条线上收到错误:
b.dismiss(); //giving an error
错误: The method dismiss() is undefined for the type AlertDialog.Builder
更新 :[ 已解决 ]
// custom dialog
final Dialog dialog = new Dialog(getActivity());
dialog.setContentView(R.layout.displayfilecontents);
dialog.setTitle("Trip Name: " + FilesInFolder.get(position).toString().substring(0, FilesInFolder.get(position).toString().lastIndexOf(".")));
EditText text = (EditText) dialog.findViewById(R.id.etFileContents);
if (text != null) {
text.setFocusable(false);
text.setLongClickable(false);
text.setTextIsSelectable(false);
}
text.setText(builder);
Button btnCloseIt = (Button) dialog.findViewById(R.id.btnClose);
// if button is clicked, close the custom dialog
btnCloseIt.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
正如其他人已经指出的那样, b
是对AlertDialog.Builder
的引用,而不是对Dialog
本身的引用。 AlertDialog.Builder
类没有名为dismiss()
任何方法。 保存对Dialog
的引用,当您从AlertDialog.Builder
类调用create()
或show()
方法时,该Dialog
将返回给您。
还有一件事,因为您要同时调用create()
和show()
方法,您真的要同时调用这两个方法吗? 我相信只调用show()
方法就足够了。 来自开发人员参考的公共AlertDialog show():创建一个AlertDialog,其中提供给此构建器的参数以及show()的对话框。
您需要存储调用b.create()
的结果; 这就是您需要调用dismiss()
的原因。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.