我想创建一个 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;
}