Я оставляю это здесь для любого с той же проблемой:
for(i in 1..4) {
val layoutID = this.resources
.getIdentifier("editText$i","id",this.activity!!.packageName)
val currentEditText = view.findViewById<EditText>(layoutID)
currentEditText.addTextChangedListener(object:TextWatcher{
override fun afterTextChanged(p0: Editable?) {
Log.e("p0?.length",p0?.length.toString())
if (p0?.length!! > 0) {
//change focus to the next edittext
if(i in 1..3){
val layoutIDnext = this.resources
.getIdentifier("editText"+(i+1),
"id",
this.activity!!.packageName)
val nextEditText = view.findViewById<EditText>(layoutIDnext)
nextEditText.requestFocus()
}
} else if (p0?.length == 0) {
//change focus to the previous edit text
if(i in 2..4) {
val layoutIdbefore = this.resources.getIdentifier("editText"+(i-1),
"id",
this.activity!!.packageName)
val lastEditText = view.findViewById<EditText>(layoutIdbefore)
lastEditText.requestFocus()
currentEditText.setOnKeyListener { _, _, keyEvent ->
if(keyEvent.keyeditText == KeyEvent.KEYeditText_DEL) {
lastEditText.requestFocus()
}
return@setOnKeyListener false
}
}
}}
override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
}
override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
}
})
}