One of the easiest ways to restrict input for Android

Today brings a small Amway in the work, product requirements for user name input need to be limited, can only be digital and alphabetic, symbols, can not contain spaces and keyboard input emoji. Start to get this demand, I think to add an addTextChangedListener to EditText, which makes a variety of judgments on OK!

Haha, and can play happily.

But there is too much logic in the callback, which is not in line with our programmer's temperament, concise and generous, clean and neat! So I went to Du du and rewrote EditText's onCreateInputConnection() method according to my actual requirements, where to do the article, please see the following source code (if not clear, you can leave a message or see Github address)

Just customize EditText to override its onCreateInputConnection() method, and then define an internal class. The following code is ready to be copied

First, look at LimitEditText

class LimitEditText(context: Context, attrs: AttributeSet, defStyleAttr: Int)
    : EditText(context, attrs, defStyleAttr) {

    constructor(context: Context, attrs: AttributeSet) : this(context, attrs, 0)

     *  Typewriting

    override fun onCreateInputConnection(outAttrs: EditorInfo?): InputConnection {
        return InnerInputConnection(super.onCreateInputConnection(outAttrs), false)


class InnerInputConnection(target: InputConnection, mutable: Boolean)
    : InputConnectionWrapper(target, mutable) {
    //Numbers, letters
    private val pattern = Pattern.compile("^[0-9A-Za-z_]\$")
    private val patternChar = Pattern.compile("[^\\w\\s]+")
    // EmoJi
    private val patternEmoJi = Pattern.compile("[\ud83c\udc00-\ud83c\udfff]|[\ud83d\udc00-\ud83d\udfff]|[\u2600-\u27ff]", Pattern.UNICODE_CASE or Pattern.CASE_INSENSITIVE)
    //English punctuation
    private val patternEn = Pattern.compile("^[`~!@#\$%^&*()_\\-+=<>?:\"{},.\\\\/;'\\[\\]]\$")
    //Chinese punctuation
    private val patternCn = Pattern.compile("^[·!#¥(——): ;""',,|<. >?,[]\\[\\]]\$")

    //Interception of input
    override fun commitText(text: CharSequence?, newCursorPosition: Int)Boolean {
        if (patternEmoJi.matcher(text).find()){
            return false

        if (pattern.matcher(text).matches() || patternChar.matcher(text).matches()) {
            return super.commitText(text, newCursorPosition)
        return false


A total of 60 lines of code, you can work out the general needs, and then look at its layout usage (xml file), usually how to write EditText in the layout, or how to write!

<?xml version="1.0" encoding="utf-8"?>

        android:text="Hello World!"


Finally, let's look at the usage in Activity, which is in line with the general use of EditText.

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {

        //By default in demo, LimitEditText can only enter alphanumeric and punctuation symbols

       //Delay is mainly easier to observe
            //Note that getting the focus needs to be dealt with by oneself, which is very simple, as follows:
            let_main.isFocusable = true
            let_main.isFocusableInTouchMode = true


Of course, these restriction rules can also be defined in LimitEditText, just add what you need!

The original release date is 2018-11-20.

This article is from Yunqi Community Partners“ Android Develops Chinese Station To learn about relevant information, you can pay attention to it.“ Android Develops Chinese Station".

Keywords: Android emoji xml github

Added by horizontal on Tue, 14 May 2019 16:39:19 +0300