1

I am trying to do this, users are signing up, and for the password field I don't want to have two password fields (like traditionally password and the other is for confirm password) I want to have only one edittext and when after entering the password for first time and loose focus, well the below code is self-explanatory

passwordSgnup_et.setOnFocusChangeListener(new View.OnFocusChangeListener() {
                    @Override
                    public void onFocusChange(View v, boolean hasFocus) {
                        if(!hasFocus ) {
                            if (i <= 0) {
                                passwordSgnup_et.requestFocus();
                                cfmPasswordSgnup = Password;
                                passwordSgnup_et.getText().clear();
                                passwordSgnup_et.setHint("Confirm Password");
                                if (passwordSgnup_et.getText().toString() .equals( cfmPasswordSgnup)) {
                                    ParseUser userSignup = new ParseUser();
                                    userSignup.setUsername(Name);
                                    userSignup.setEmail(Email);
                                    userSignup.setPassword(Password);
                                    userSignup.put("Nickname",nickName);

Problem that I am facing is this. How can I know if the confirm password is entered and matches with the one entered the first time. I am confused there. Also to exit after the confirm password matches with the first password I am using the good old int i <= 0 logic anyway to improve this?

EDIT: This question was closed as a duplicate simply referring to another equals to question, MY QUESTION being is that, in the logic you can see that,

passwordSgnup_et.setHint("Confirm Password");
                                    if (passwordSgnup_et.getText().toString() .equals( cfmPasswordSgnup)) {

in between these two lines i need to take in the userinput (confirmpassword) to actually compare with the first password, that is what I am not able to get it.

JackyBoi
  • 2,695
  • 12
  • 42
  • 74
  • 4
    Use `equals` instead of `==` for comparing String values – ρяσѕρєя K Apr 21 '15 at 12:11
  • `.equals()` to compare objects. `==` for primitive data types – Razgriz Apr 21 '15 at 12:13
  • @ρяσѕρєяK this question is much more than a SIMPLE equals... did u get the question?? – JackyBoi Apr 21 '15 at 12:21
  • @JackyBoi: ok and please update your code using `equals` method – ρяσѕρєя K Apr 21 '15 at 12:23
  • your question is not very clear. You want to clear the password field and ask user to retype it (confirm the password) and then check if the two passwords match, right? – Sufian Apr 21 '15 at 12:35
  • Yes that is as simple as that. – JackyBoi Apr 21 '15 at 12:36
  • That is a pretty simple question. You'd use a `private String mPassword` into your `Fragment`/`Activity` and save the p/w in that to compare with the next time he types in. Can't believe what you just asked :P – Sufian Apr 21 '15 at 12:40
  • only if it was that easy to resolve.. the question being, how to know if the user finishes typing in the confirmpassword (the second time), I think the below answers will stimulate your mind much further on the complexity that I am facing. – JackyBoi Apr 21 '15 at 15:11

2 Answers2

1

One workaround taking a example with just one edittext

String password = null;
edittext.setOnFocusChangeListener(new View.OnFocusChangeListener() {
    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        //First time edittext will be with default initialization so no tag will be set so u can check 
        if (!hasFocus){
            if (v.getTag() == null){
                //you require to put validations you want
                password = edittext.getText().toString();
                v.setTag("c");
                v.setText("");
                v.setHint("Confirm Password");
                v.requestFocus();
            }else if (((String)v.getTag()).equals("c"){
                //It is now confirm password field lost focus
            }
        }
    }
});

apart from this you can also implement the textwatcher and in afterTextChanged you can check

if (((String)v.getTag()).equals("c") {
   // So user has completed entering confirm password so you can put a check. 
}

I have not tested this code so it might have some code level issues/warning which you require to fix in actual implementation.

Rajen Raiyarela
  • 5,526
  • 4
  • 21
  • 41
  • This is somewhat close, but I am facing another issue as when I loose the focus I am unable to get focus back to the password. – JackyBoi Apr 22 '15 at 09:50
0

I think you want to use a TextWatcher.

TextView.addTextWatcher(...)

There are callbacks to get the info about when input is done. And check out OnFocusChangeListener interface as well!

Bondax
  • 3,143
  • 28
  • 35