如何在android中的字段中检查密码和确认密码?

时间:2016-12-29 10:51:46

标签: android passwords

我正在制作注册表格,其中有一个名为密码的字段并确认密码。我希望在用户输入密码时使它们相等..但我不知道如何通过编码使它们相等..可以任何人救命?? 这是注册页面:

 public class RegistrationForm extends AppCompatActivity {

    EditText fn,ln,mb,em,pw,cpw,dob,gen;
    Switch sw;
    RadioGroup male,feml;
    Switch swth;
    private ProgressDialog pDialog;
    String status="";


    public final Pattern EMAIL_ADDRESS_PATTERN = Pattern.compile(
            "[a-zA-Z0-9+._%-+]{1,256}" +
                    "@" +
                    "[a-zA-Z0-9][a-zA-Z0-9-]{0,64}" +
                    "(" +
                    "." +
                    "[a-zA-Z0-9][a-zA-Z0-9-]{0,25}" +
                    ")+"
    );


    private static String url_create_book = "http://cloud.....com/broccoli/creatinfo.php";

    // JSON Node names
    // JSON Node names
    private static final String TAG_SUCCESS = "success";


String rval;

    JSONParser jsonParser = new JSONParser();
    private int serverResponseCode = 0;
    Context c;
    int i=0;


    Button sub;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_registration_form);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);


        fn=(EditText)findViewById(R.id.fnm) ;
        ln=(EditText)findViewById(R.id.lnm) ;
        mb=(EditText)findViewById(R.id.mobile) ;




        pw=(EditText)findViewById(R.id.pass) ;
        cpw=(EditText)findViewById(R.id.cpass) ;


        cpw.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                String passwrd = pw.getText().toString();
                if (editable.length() > 0 && passwrd.length() > 0) {
                    if(!cpw .equals(passwrd )){
                        // give an error that password and confirm password not match
                    }

                }
            }
        });





























        dob=(EditText)findViewById(R.id.dob);

       dob.setOnClickListener(new View.OnClickListener(){
                                  @Override
                                  public void onClick(View v) {
                                      int mYear, mMonth, mDay;
                                      final Calendar c = Calendar.getInstance();
                                      mYear = c.get(Calendar.YEAR);
                                      mMonth = c.get(Calendar.MONTH);
                                      mDay = c.get(Calendar.DAY_OF_MONTH);
                                      DatePickerDialog datePickerDialog = new DatePickerDialog(RegistrationForm.this,R.style.datepicker, new DatePickerDialog.OnDateSetListener() {
                                          @Override
                                          public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                                              dob.setText(year + "/" + (month + 1) + "/" + dayOfMonth);
                                             // dob.setText(dayOfMonth + "/" + (month + 1) + "/" + );
                                          }
                                      }, mYear, mMonth, mDay);
                                      //forsetting minimum date for selection
                                     // datePickerDialog.getDatePicker().setMinDate(c.getTimeInMillis());
                                      datePickerDialog.show();
                                  }
                              });


       // RadioButton male=(RadioButton)findViewById(R.id.rgm) ;

       // RadioButton feml=(RadioButton)findViewById(R.id.rgf) ;

        Switch swth=(Switch)findViewById(R.id.mySwitch) ;


//////set the switch to ON
        swth.setChecked(false);

//////attach a listener to check for changes in state
        swth.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {

                if(isChecked){
                    status="true"; //edit here
                }else{
                    status="false";
                }

            }
        });


        RadioGroup rgrp=(RadioGroup)findViewById(R.id.rg);

        RadioButton radioButton;





        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        sub=(Button)findViewById(R.id.sub2);




        sub.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                RadioGroup rgrp = (RadioGroup) findViewById(R.id.rg);
                em = (EditText) findViewById(R.id.email);
                RadioButton radioButton;
                int selectedId = rgrp.getCheckedRadioButtonId();
                // find the radiobutton by returned id
                radioButton = (RadioButton) findViewById(selectedId);
                rval = radioButton.getText().toString();
                // Toast.makeText(RegistrationForm.this, rval, Toast.LENGTH_SHORT).show();

                if(validate()){
                    new CreateNewProduct().execute();
                    // startActivity(new Intent(RegistrationForm.this, Home.class));
                }
            }
            private boolean validate() {
                String checkemail = em.getText().toString();
                String pass=pw.getText().toString();
                String cpass=cpw.getText().toString();

                boolean temp=true;
                if(!EMAIL_ADDRESS_PATTERN.matcher(checkemail).matches()){
                    Toast.makeText(RegistrationForm.this,"Invalid Email Address",Toast.LENGTH_SHORT).show();
                    temp=false;
                }
                else if(!pass.equals(cpass)){
                    Toast.makeText(RegistrationForm.this,"Password Not matching",Toast.LENGTH_SHORT).show();
                    temp=false;
                }
                new CreateNewProduct().execute();
return false;
            }
        });

    }







    class CreateNewProduct extends AsyncTask<String, String, String> {
        private  String fname;
        private  String lname;
        private  String email;
        private  String passwrd;
        private  String cpasswrd;
        private String dobr;
        private String mobile;



        /**
         * Before starting background thread Show Progress Dialog
         * */
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(RegistrationForm.this);
            pDialog.setMessage("Creating books..");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(true);
            pDialog.show();
            fname = fn.getText().toString();
            lname = ln.getText().toString();
            email = em.getText().toString();
            passwrd = pw.getText().toString();
            cpasswrd = cpw.getText().toString();
            dobr = dob.getText().toString();
            mobile=mb.getText().toString();
            //Toast.makeText(RegistrationForm.this,
            //dobr, Toast.LENGTH_SHORT).show();
        }




        protected String doInBackground(String... args) {


            // Building Parameters
            List<NameValuePair> params = new ArrayList<NameValuePair>();
            params.add(new BasicNameValuePair("First_Name", fname));
            params.add(new BasicNameValuePair("Last_Name",lname));
            params.add(new BasicNameValuePair("email", email));
            params.add(new BasicNameValuePair("Gender", rval));
            params.add(new BasicNameValuePair("password", passwrd));
            params.add(new BasicNameValuePair("confirmPasw",cpasswrd));
            params.add(new BasicNameValuePair("DOB",dobr));
            params.add(new BasicNameValuePair("sms_subscrb",status));
            params.add(new BasicNameValuePair("Mobile_No",mobile));



            // getting JSON Object
            // Note that create product url accepts POST method
            JSONObject json = jsonParser.makeHttpRequest(url_create_book,
                    "POST", params);

            // check log cat fro response
            Log.d("Create Response", json.toString());

            // check for success tag
            try {
                int success = json.getInt(TAG_SUCCESS);

                if (success == 1) {
                    // successfully created product
                    Intent i = new Intent(getApplicationContext(), Login.class);
                    startActivity(i);

                    // closing this screen
                    finish();
                } else {
                    // failed to create product
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }

            return null;
        }

        /**
         * After completing background task Dismiss the progress dialog
         * **/
        protected void onPostExecute(String file_url) {
            // dismiss the dialog once done
            pDialog.dismiss();
        }

    }



























    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                onBackPressed();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

}

5 个答案:

答案 0 :(得分:4)

在检查电子邮件模式后,您需要匹配两个字段。更改子按钮的代码,如此....

sub.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            RadioGroup rgrp = (RadioGroup) findViewById(R.id.rg);
            em = (EditText) findViewById(R.id.email);
            RadioButton radioButton;
            int selectedId = rgrp.getCheckedRadioButtonId();
            // find the radiobutton by returned id
            radioButton = (RadioButton) findViewById(selectedId);
            rval = radioButton.getText().toString();
           // Toast.makeText(RegistrationForm.this, rval, Toast.LENGTH_SHORT).show();


            if(validate){
                new CreateNewProduct().execute();
                // startActivity(new Intent(RegistrationForm.this, Home.class));
            }  
        }
        private boolean validate() {
            boolean temp=true;
            String checkemail = em.getText().toString();
            String pass=pw.getText().toString();
            String cpass=cpw.getText().toString();
            if(!EMAIL_ADDRESS_PATTERN.matcher(checkemail).matches()){
                 Toast.makeText(Registratiomform.this,"Invalid Email Address",Toast.LENGTH_SHORT).show();
                 temp=false;
            }
            else if(!pass.equals(cpass)){
                 Toast.makeText(Registratiomform.this,"Password Not matching",Toast.LENGTH_SHORT).show();
                 temp=false;
            }
            return temp;
        }
    });

答案 1 :(得分:2)

@ z.al,如果您想在输入确认密码时检查密码并确认密码是否相同,则必须在确认时执行 TextWatcher 密码编辑文本(在您的情况下为 cpw ),如

cpw.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                String passwrd = pw.getText().toString();
                if (editable.length() > 0 && passwrd.length() > 0) { 
                    if(!cpasswrd .equals(passwrd )){
                        // give an error that password and confirm password not match
                    }

                } 
            }
        });

答案 2 :(得分:1)

if (!user_password.getText().toString().equals(user_confirm_password.getText().toString()){

    Toast.makeText(this, "Confirm password is not correct", Toast.LENGTH_SHORT).show();

  }
  • 其中user_password和user_confirm_password是您的编辑文本。

答案 3 :(得分:0)

if(password.getText().toString().equals(confirmpassword.getText().toString())){

用您的编辑文字名称替换密码和确认密码

答案 4 :(得分:0)

| Project | Machine | Category |   Details   |         DateTime         |
------------------------------------------------------------------------
| M00004  |   A09   |    QC    | CENTER OVER |  2020-01-21 19:15:00.000 |
| M00006  |   C03   |    QC    |  PUSHER UP  |  2020-01-21 17:30:00.000 |