在 Kotlin 中,您可以像 .. 一样使用 bellow
txtOTP_1.addTextChangedListener(object : TextWatcher {
override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
if (txtOTP_1.text.toString().length == 1) {
txtOTP_1.clearFocus()
txtOTP_2.requestFocus()
txtOTP_2.setCursorVisible(true)
}
}
override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {
}
override fun afterTextChanged(s: Editable) {
if (txtOTP_1.text.toString().length == 0) {
txtOTP_1.requestFocus()
}
}
})
txtOTP_2.addTextChangedListener(object : TextWatcher {
override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
if (txtOTP_2.text.toString().length == 1) {
txtOTP_2.clearFocus()
txtOTP_3.requestFocus()
txtOTP_3.setCursorVisible(true)
}
}
override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {
}
override fun afterTextChanged(s: Editable) {
if (txtOTP_2.text.toString().length == 0) {
txtOTP_2.requestFocus()
}
}
})
txtOTP_3.addTextChangedListener(object : TextWatcher {
override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
if (txtOTP_3.text.toString().length == 1) {
txtOTP_3.clearFocus()
txtOTP_4.requestFocus()
txtOTP_4.setCursorVisible(true)
}
}
override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {
}
override fun afterTextChanged(s: Editable) {
if (txtOTP_3.text.toString().length == 0) {
txtOTP_3.requestFocus()
}
}
})
txtOTP_4.addTextChangedListener(object : TextWatcher {
override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
if (txtOTP_4.text.toString().length == 1) {
txtOTP_4.clearFocus()
txtOTP_5.requestFocus()
txtOTP_5.setCursorVisible(true)
}
}
override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {
}
override fun afterTextChanged(s: Editable) {
if (txtOTP_4.text.toString().length == 0) {
txtOTP_4.requestFocus()
}
}
})
txtOTP_5.addTextChangedListener(object : TextWatcher {
override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
if (txtOTP_5.text.toString().length == 1) {
txtOTP_5.requestFocus()
txtOTP_5.setCursorVisible(true)
}
}
})