Reputation: 131
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.
Upvotes: 13
Views: 8750
Reputation: 1
usr .addTextChangedListener(new TextWatcher() {
public void onTextChanged(CharSequence s, int start, int before, int count)
{
String tusr = usr.getText().toString();
if (tusr.length()==2) //max lenght 2bit
{
pswd.requestFocus();
}
}
@Override
public void afterTextChanged(Editable arg0) {
}
@Override
public void beforeTextChanged(CharSequence s, int start,
int count, int after) {
}
});
Upvotes: 0
Reputation: 101
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) {
}
});
Upvotes: 1
Reputation: 259
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) {
}
});
Upvotes: 0
Reputation: 1093
Try to do this on the afterTextChanged method. also you can call usr.clearFocus() before requesting the focus on pwd.
Upvotes: 0
Reputation: 31
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
Upvotes: 0
Reputation: 1
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()
Upvotes: 0