Created
August 15, 2022 10:24
-
-
Save thenishchalraj/2949ad4a9909798bf3f4bc5d08145e43 to your computer and use it in GitHub Desktop.
Text watcher that will take care of input fields in CreateInputFieldsInInputBox.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Blog related to the gist: Click here