我有以下代码:
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
错误: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
的引用当您调用 create()
时会返回给您或show()
方法来自AlertDialog.Builder
类(class)。
还有一件事,既然你调用 create()
和show()
同时调用这两个方法,你真的想同时调用这两个方法吗?我相信只调用 show()
方法对你来说就足够了。来自开发人员引用 public AlertDialog show () : Creates a AlertDialog with the arguments supplied to this builder and show()'s the dialog.
关于java - .dismiss() 在 AlertDialog 中显示错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18061608/