I want to implement a 4-digit PIN input. For each digit, I made a separate EditText. Accordingly, only one character can be written in one EditText. I need the focus to automatically move to the next EditText when entering a character, similarly for deletion. I tried to implement this with TextWatcher:
inner class EnterCodeTextWatcher(private var prevFocus: EditText?, private var nextFocus: EditText?) : TextWatcher { override fun afterTextChanged(s: Editable?) { } override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) { } override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) { if (s.isNullOrEmpty() && prevFocus != null){ prevFocus?.requestFocus() prevFocus?.isCursorVisible = true } else if (!s.isNullOrEmpty() && nextFocus != null){ nextFocus?.requestFocus() nextFocus?.isCursorVisible = true } } } This works well if you type all 4 characters and delete all 4 characters. But if you enter only 2 characters and try to delete one character, it will not work, because the focus will be on the next EditText, which is empty. How to solve this problem? Or maybe you need to do it in another way?