Skip to content

Instantly share code, notes, and snippets.

@thenishchalraj
Created August 15, 2022 10:24
Show Gist options
  • Save thenishchalraj/2949ad4a9909798bf3f4bc5d08145e43 to your computer and use it in GitHub Desktop.
Save thenishchalraj/2949ad4a9909798bf3f4bc5d08145e43 to your computer and use it in GitHub Desktop.
Text watcher that will take care of input fields in CreateInputFieldsInInputBox.kt
private fun setTextWatcher(editText: EditText?) {
editText?.addTextChangedListener(object : TextWatcher {
override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {}
override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
if (s.toString() != "") {
mTextMap[editText.id] = TextMapModel(s.toString(), editText)
val indexOfNext = mTextIdArray.indexOf(editText.id) + 1
if (indexOfNext < mTextIdArray.size) {
(mTextMap[mTextIdArray[i]]?.view as EditText).requestFocus()
}
}
}
override fun afterTextChanged(s: Editable) {
if (s.toString() == "") {
mTextMap[editText.id] = TextMapModel("", editText)
val indexOfNext = mTextIdArray.indexOf(editText.id) - 1
if (indexOfNext >= 0) {
(mTextMap[mTextIdArray[i]]?.view as EditText).requestFocus()
}
}
}
})
editText?.setOnKeyListener { v, keyCode, event ->
if (keyCode == KeyEvent.KEYCODE_DEL && editText.selectionStart == 0) { // i.e. 67
val indexOfPrev = mTextIdArray.indexOf(editText.id) - 1
if (indexOfPrev >= 0) {
(mTextMap[mTextIdArray[i]]?.view as EditText).requestFocus()
}
}
true
}
}
@thenishchalraj
Copy link
Author

Blog related to the gist: Click here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment