如何比较密码和确认密码?

时间:2017-06-04 20:01:23

标签: java android

我在

中遇到错误
final EditText etregpassword = (EditText)findViewById(R.id.etregpassword);

我想帮助比较密码和确认密码,并在密码不匹配时显示错误。

public class RegisterActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_register);

    final EditText etregname = (EditText)findViewById(R.id.etregname);
    final EditText etregemailid = (EditText)findViewById(R.id.etregemailid);
    final EditText etregpassword = (EditText)findViewById(R.id.etregpassword);
    final EditText etconfirmregpassword = (EditText)findViewById(R.id.etconfirmregpassword);
    final Button regbutton = (Button) findViewById(R.id.regbutton);

    regbutton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            final String User_name = etregname.getText().toString();
            final String Email_id = etregemailid.getText().toString();
            final String Password = etregpassword.getText().toString();
            final String Confirm_password = etconfirmregpassword.getText().toString();

            Response.Listener<String> responseListner = new Response.Listener<String>(){

                @Override
                public void onResponse(String response) {
                    try {
                        JSONObject jsonObject = new JSONObject(response);
                        boolean SUCCESS = jsonObject.getBoolean("success");

                        if(SUCCESS){
                            Intent intent = new Intent(RegisterActivity.this,LoginActivity.class);
                            RegisterActivity.this.startActivity(intent);
                        }

                        else{
                            AlertDialog.Builder builder = new AlertDialog.Builder(RegisterActivity.this);
                            builder.setMessage("Register Failed")
                                .setNegativeButton("Retry",null)
                                    .create()
                                     .show();

                        }

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                }
            };

            RegisterRequest registerRequest = new RegisterRequest(User_name,Email_id,Password,Confirm_password,responseListner);
            RequestQueue queue= Volley.newRequestQueue(RegisterActivity.this);
            queue.add(registerRequest);
        }

    });


}

}

1 个答案:

答案 0 :(得分:0)

只需比较这些值:

final String password = etregpassword.getText().toString();
final String confirmPassword = etconfirmregpassword.getText().toString();
if (!TextUtils.isEmpty(password) && !TextUtils.isEmpty(confirmPassword))
    {
        if(password.equals(confirmPassword))
        {
            //are equal
        }
        else {
            //are different
        }
    }