Skip to content

Instantly share code, notes, and snippets.

@lukelorusso
Last active August 27, 2019 11:14
Show Gist options
  • Save lukelorusso/41051c7c4c5a30454e1b5eb8ea9b809b to your computer and use it in GitHub Desktop.
Save lukelorusso/41051c7c4c5a30454e1b5eb8ea9b809b to your computer and use it in GitHub Desktop.
onAttachedToWindow()
var text: Editable = "".toEditable()
set(value) {
field = value
renderCode() // let's keep this for later
}
...
override fun onAttachedToWindow() {
super.onAttachedToWindow()
...
if (!isInEditMode) {
llCodeWrapper.removeAllViews()
for (i in 0 until maxLength) {
View.inflate(
context,
R.layout.item_code_edit_text,
findViewById(R.id.llCodeWrapper)
)
}
if (text.isNotEmpty()) editCodeReal.text = text
editCodeReal.filters = arrayOf<InputFilter>(InputFilter.LengthFilter(maxLength))
editCodeReal.removeTextChangedListener(textChangedListener)
editCodeReal.addTextChangedListener(textChangedListener)
llCodeWrapper.setOnClickListener {
editCodeReal.apply {
showKeyboard()
focusOnLastLetter()
}
}
}
...
}
private val textChangedListener = object : TextWatcher {
override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {}
override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {}
override fun afterTextChanged(s: Editable) {
text = s
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment