Home » Android » focus – Android EditText AutoFocus to next EditText when at maxlength

focus – Android EditText AutoFocus to next EditText when at maxlength

Posted by: admin May 14, 2020 Leave a comment

Questions:

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.

How to&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) {
   }
});

Answer:

Print out your "s" with log.d() or something, so you can see how "s" looks like.
Maybe its not how you expect.
In this case you can code it dirty.
Make your own TextWatcher that implements TextWatcher and give in the Constructor the "usr".
so you can get the total length with usr.getText()

Answer:

hello i have try right now ,but i success ,our code are same, my xml is simple only two editText my phone is Meizu version is 4.1

Answer:

Try to do this on the afterTextChanged method.
also you can call usr.clearFocus() before requesting the focus on pwd.

Answer:

Remove nextfocusdown from your edit text xml.

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 (usr.getText.toString().length() == 9) (Use usr.getText.toString() instead of s) 
        { 
            pswd.requestFocus(); 
        }
    }
    @Override
    public void afterTextChanged(Editable arg0) {
    }

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