这可能看起来像是一个简单的问题需要解决,但我是Android新手所以请耐心等待。我有以下代码片段,显示一个警告框:
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();
一切正常。问题是,我无法访问“pwDialog”变量,那么如何关闭对话框呢?
答案 0 :(得分:1)
看起来你应该有权访问你的pwDialog变量。您可能需要将其声明为最终版。
final AlertDialog pwDialog = pwBox.create();
答案 1 :(得分:0)
你想要的东西:
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();
}