0

我想创建一个 AlertDialog 来检查两个密码是否匹配。问题是当我从布局中膨胀视图时,对话框没有出现,OK 和 Cancel 按钮也是如此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;
}
4

1 回答 1

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();
于 2013-08-31T14:30:54.420 回答