Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android EditText AutoFocus to next EditText when at maxlength

I have a username and a password XML segment

<EditText 
    android:id="@+id/username"
    android:hint="@string/username_hint"
    android:layout_width="match_parent"
    android:maxLength="9"
    android:nextFocusDown="@+id/pswd"
    android:layout_height="wrap_content">
</EditText>
<EditText 
    android:inputType="textPassword" 
    android:hint="@string/password_hint"
    android:id="@+id/pswd" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content">
</EditText>

I want when the username field reaches the 9th character for the cursor focus to automatically jump to the password field

I was looking at Focus next textview automatically and was trying:

final EditText usr = (EditText) findViewById (R.id.username);
final EditText pswd = (EditText) findViewById (R.id.pswd);
usr.addTextChangedListener(new TextWatcher() {
    public void onTextChanged(CharSequence s, int start, int before, int count) { 
        if (s.length() == 9) { 
            pswd.requestFocus(); 
        }
    }
    @Override
    public void afterTextChanged(Editable arg0) {
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start,
    int count, int after) {
    }
});

To not much success. I am using this in my oncreate method in which I am processing my login information. Any ideas or suggestions would be appreciated.

like image 635
Dladu Avatar asked Nov 29 '11 22:11

Dladu


1 Answers

try like this may help you.

/** addTextChangedListener is useful when first username box is filled with reaches the 9th character * the request will automatically focus to next box*/

usr.addTextChangedListener(new TextWatcher() {
    public void onTextChanged(CharSequence s, int start, int before, int count) { 
         if (count == 9) { 
             pwd.requestFocus(); 
             usr.setTransformationMethod(PasswordTransformationMethod.getInstance());
         }
    }
   @Override
   public void afterTextChanged(Editable arg0) {
   }
   @Override
   public void beforeTextChanged(CharSequence s, int start,
           int count, int after) {
   }
});
like image 50
user2496783 Avatar answered Nov 19 '22 03:11

user2496783