<EditText
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:imeOptions="actionNext"
android:inputType="textNoSuggestions|textCapCharacters"
android:maxLength="3"
android:hint="ABC"
/>
Also have another EditText
that should be focused when all 3 symbols are filled in the name
.
name.addTextChangedListener(object: TextWatcher {
override fun afterTextChanged(s: Editable?) {
// Move to surname when all three symbols are entered.
if (name.text.toString().length == 3) {
surname.requestFocus()
}
}
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) { }
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) { }
})
It works right when we enter 1, 2 and 3 symbols. But when we select last position in name
, press a new letter in a keyboard, nothing happens. I tried to catch a key press with
name.setOnKeyListener { v, keyCode, event ->
if (name.text.getText().length == 3) {
surname.requestFocus()
}
false
}
but an event doesn't arise. How to move focus?
Change maxLength
to 4
android:maxLength="4"
In afterTextChanged
:
override fun afterTextChanged(s: Editable?) {
// Move to surname when all three symbols are entered.
if (name.isFocused() && name.text.toString().length > 3) {
surname.requestFocus();
name.setText(s.toString().substring(0,3));
}
}