Created
June 22, 2025 06:46
-
-
Save yongjhih/adfff0aa2e4baeb0f14b1dbe4343e35a to your computer and use it in GitHub Desktop.
This file contains hidden or 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
fun EditText.maxLength(length: Int?): InputFilter.LengthFilter? { | |
if (length == null) { | |
filters = filters.filter { it !is InputFilter.LengthFilter }.toTypedArray() | |
return null | |
} | |
val lengthFilter = InputFilter.LengthFilter(length) | |
filters = filters.filter { it !is InputFilter.LengthFilter } | |
.toMutableList() | |
.apply { add(lengthFilter) }.toTypedArray() | |
return lengthFilter | |
} | |
var EditText.maxLength: Int? | |
set(value) { maxLength(value) } | |
get() = filters.filterIsInstance<InputFilter.LengthFilter>().firstOrNull()?.max |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment