Skip to content

Instantly share code, notes, and snippets.

@chowaikong
Created June 10, 2017 09:29
Show Gist options
  • Save chowaikong/be83b22cc36a722fd0d5eb3b87b3d700 to your computer and use it in GitHub Desktop.
Save chowaikong/be83b22cc36a722fd0d5eb3b87b3d700 to your computer and use it in GitHub Desktop.
class EmojiExcludedEditText : EditText {
constructor(context: Context) : super(context) {
init()
}
constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {
init()
}
constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super(context, attrs,
defStyleAttr) {
init()
}
private fun init() {
filters = arrayOf<InputFilter>(EmojiExcludeFilter())
}
private inner class EmojiExcludeFilter : InputFilter {
override fun filter(source: CharSequence, start: Int, end: Int, dest: Spanned, dstart: Int,
dend: Int): CharSequence? {
return if ((start..end - 1)
.map { Character.getType(source[it]) }
.any { it == Character.SURROGATE.toInt() || it == Character.OTHER_SYMBOL.toInt() }) "" else null
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment