Closing an AlertDialog box
This may seem like a simple problem to solve, but I'm new to Android so please bear with me. I have the following code fragment that displays an alert box:
Builder pwBox = new AlertDialog.Builder(this);
AlertDialog pwDialog;
LayoutInflater mInflater = (LayoutInflater) this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View pwView = mInflater.inflate(R.layout.passworddialog, null);
Button btnSetPassword = (Button) pwView
.findViewById(R.id.btnSetPassword);
pwBox.setView(pwView);
pwBox.setCancelable(false);
pwBox.setTitle("New Password");
开发者_开发问答pwDialog = pwBox.create();
btnSetPassword.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//pwDialog.dismiss(); <------ Problem Line
}
});
pwDialog.show();
Everything works. The problem is, I don't have access to the "pwDialog" variable, so how do I close my dialog?
It looks as though you should have access to your pwDialog variable. You may need to declare it as final though.
final AlertDialog pwDialog = pwBox.create();
You'd want something of the sort:
private static final CommandWrapper DISMISS = new CommandWrapper(Command.NO_OP);
public static AlertDialog createDeletionDialog(final Context context,
final String message, final String positiveLabel, final Command positiveCommand) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setCancelable(true);
builder.setMessage(message);
builder.setInverseBackgroundForced(true);
builder.setPositiveButton(positiveLabel, new CommandWrapper(positiveCommand));
builder.setNeutralButton("Cancel", DISMISS);
return builder.create();
}
精彩评论