我有以下代码:
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();