我使用此代码来创建“确定/取消”对话框:
AlertDialog.Builder b = new AlertDialog.Builder(view.getContext());
b.setTitle("Login Unsuccessful");
b.setMessage("Login has failed! Try again!");
b.setCancelable(false);
b.setPositiveButton("OK", new AlertDialog.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
nameTextView.setText("");
passTextView.setText("");
}
});
b.create().show();
这是对的吗?我觉得好像没有高效输入5条额外的行只是为了让这个该死的对话框显示出来。有什么想法吗?
答案 0 :(得分:1)
好吧,我用:
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
alertDialogBuilder.setTitle("Authentication Error!");
alertDialogBuilder
.setMessage("User Name/Password is invalid.")
.setCancelable(false)
.setNeutralButton("Try Again",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
dialog.cancel();
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
请参阅,如果这会缩短您的代码。