Yes sure you can use TextWatcher
Java version
et1.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (s != null && s.endsWith("Your_special_char")){
yourDesiredEditText.requestFocus();
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
@Override
public void afterTextChanged(Editable s) {}
});
Kotlin version
et1.addTextChangedListener(object : TextWatcher {
override fun afterTextChanged(p0: Editable?) {
}
override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
}
override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
if (p0?.endsWith("Your_special_char") == true){//it will eliminate nullability (trick)
yourDesiredEditText.requestFocus()
}
}
})
EDIT
the only issue is I have with this is you end up with a ton of code in each event.
Ok I will provide a clean and neat solution using Kotlin extension functions
fun EditText.forwardFocusOnSpecialCharEntered(targetEditText: EditText) {
this.addTextChangedListener(object : TextWatcher {
override fun afterTextChanged(p0: Editable?) {}
override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {}
override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
if (p0?.endsWith(" ") == true) targetEditText.requestFocus()
}
})
}
Usage
firstEditText.forwardFocusOnSpecialCharEntered(secondEditText)
secondEditText.forwardFocusOnSpecialCharEntered(thirdEditText)
thirdEditText.forwardFocusOnSpecialCharEntered(firstEditText)
Now using one line of code you can achieve your goal as well as your code stays clean and readable