我想创建一个AlertDialog来检查两个密码是否匹配。问题是当我从布局中膨胀视图时,对话框不会出现,OK和Cancel按钮也会出现,只显示edittext和在Mainactivity中也是如此。这是我到目前为止使用的代码。有人可以帮我解决这个问题
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case MY_PASSWORD_DIALOG_ID:
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View layout = inflater.inflate(R.layout.activity_main, (ViewGroup) findViewById(R.id.root));
final EditText password1 = (EditText) layout.findViewById(R.id.EditText_Pwd1);
final EditText password2 = (EditText) layout.findViewById(R.id.EditText_Pwd2);
final TextView error = (TextView) layout.findViewById(R.id.TextView_PwdProblem);
AlertDialog.Builder builder=new AlertDialog.Builder(this);
builder.setTitle("Enter Password");
builder.setView(layout);
builder.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
finish();
}
});
builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
String strPassword1 = password1.getText().toString();
String strPassword2 = password2.getText().toString();
if (strPassword1.equals(strPassword2)) {
Toast.makeText(MainActivity.this,
"Matching passwords="+strPassword2, Toast.LENGTH_SHORT).show();
}
}
});
AlertDialog passwordDialog = builder.create();
return passwordDialog;
}
return null;
}
答案 0 :(得分:0)
试试这个,
您正在使用自定义对话框。然后,您不使用默认的AlertDialog.setPositiveButton
。您只能使用自定义视图按钮尝试此操作
private Dialog mForgetPasswordDialog;
mForgetPasswordDialog = new Dialog(Signin.this);
mForgetPasswordDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
mForgetPasswordDialog.setContentView(R.layout.forget_password);
mForgetPasswordDialog.setCancelable(true);
edtGetmailId = (EditText) mForgetPasswordDialog
.findViewById(R.id.edtGetEmailId);
TextView btnOk = (TextView) mForgetPasswordDialog
.findViewById(R.id.btnOk);
btnOk.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// do your stuff
}
});
TextView btnCancel = (TextView) mForgetPasswordDialog
.findViewById(R.id.btnCancel);
btnCancel.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
mForgetPasswordDialog.dismiss();
}
});
mForgetPasswordDialog.show();