Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/210.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android Alertdialog无法正确显示视图_Android_Android Layout_Android Edittext_Android View_Android Alertdialog - Fatal编程技术网

Android Alertdialog无法正确显示视图

Android Alertdialog无法正确显示视图,android,android-layout,android-edittext,android-view,android-alertdialog,Android,Android Layout,Android Edittext,Android View,Android Alertdialog,我想创建一个AlertDialog来检查两个密码是否匹配。问题是,当我从布局中放大视图时,对话框不会出现,OK和Cancel按钮也不会出现,只有edittext会显示,Mainactivity中也会显示。这是我目前使用的代码。有人能帮我解决这个问题吗 @Override protected Dialog onCreateDialog(int id) { switch (id) { case MY_PASSWORD_DIALOG_ID: LayoutInflater

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

您正在使用自定义对话框。然后您不使用默认的
AlertDialog.setPositiveButton
。您只能使用customview按钮,请尝试以下操作

 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();

似乎不起作用……请编辑我的代码,这样我就可以知道我是否做得不对了。只需复制并粘贴此代码,使用布局和widgetsit,类型视图中的方法setOnClickListener(View.OnClickListener)不适用于参数(new OnClickListener(){});