I am trying to make a lock screen, the lock screen basically has four edit text fields, these fields basically take numerical input, no problem in providing the numerical part.
Now the problem I am having is that, the text fields should take only one numerical value,and the edit text fields should change focus, right after one field is provided with a value, how can achieve this?
Use the following property to enter only numbers
<EditText android:inputType="number" ... />
or
<EditText
android:inputType="phone"
android:digits="1234567890"
...
/>
For only allowing numerical input (and also showing the "number"-keyboard), use the inputType
-attribute of the EditText class. To only allow entering one digit into the field, you can use the android:maxLength
-attibute .
Use a TextWatcher
to check if the one digit has been entered into the text field and change the focus to the next field.
Add a listener to each EditText for when the text changes (make sure the EditText enforces a max length of 1). The logic of this listener will check that the text is a digit and of length 1, then it'll shift focus. Here's the code:
EditText et1 = ...;
EditText et2 = ...;
EditText et3 = ...;
EditText et4 = ...;
et1.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if( s.length() == 1 && Character.isDigit(s.charAt(0)) ) {
et2.requestFocus();
}
}
@Override
public void afterTextChanged(Editable e) { }
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) { }
});
et2.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if( s.length() == 1 && Character.isDigit(s.charAt(0)) ) {
et3.requestFocus();
}
}
@Override
public void afterTextChanged(Editable e) { }
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) { }
});
et3.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if( s.length() == 1 && Character.isDigit(s.charAt(0)) ) {
et4.requestFocus();
}
}
@Override
public void afterTextChanged(Editable e) { }
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) { }
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.