Android 最簡單的限制輸入方式之一

今天帶來工做中的一個小安利,產品要求對用戶名輸入須要限制,只能是數字和字母,符號,不能包含空格和鍵盤上輸入的emoji.開始拿到這個需求,以爲給 EditText 增長一個 addTextChangedListener ,裏面作各類判斷不就OK 啦!程序員

哈哈,又能夠愉快的玩耍咯…app

可是回調裏面邏輯太多,看着也不爽,不符合咱們程序員的氣質,簡潔大方,乾淨利落!因此我特地去看了 du 了一下, 結合本身的實際要求,重寫了 EditText  的 onCreateInputConnection() 方法,在那裏作文章,請看下面源碼(若是還有不清楚的,能夠留言或者看Github地址)ide

只須要自定義EditText重寫其onCreateInputConnection()方法,而後再定義一個內部類就好,下面代碼即拷即用佈局

首先,看看 LimitEditTextthis

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

    : EditText(context, attrs, defStyleAttr) {


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


    /**

     *  輸入法

     */

    override fun onCreateInputConnection(outAttrs: EditorInfo?): InputConnection {

        return InnerInputConnection(super.onCreateInputConnection(outAttrs), false)

    }


}

class InnerInputConnection(target: InputConnection, mutable: Boolean)

    : InputConnectionWrapper(target, mutable) {

    // 數字,字母

    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)

    // 英文標點

    private val patternEn = Pattern.compile("^[`~!@#\$%^&*()_\\-+=<>?:\"{},.\\\\/;'\\[\\]]\$")

    // 中文標點

    private val patternCn = Pattern.compile("^[·!#¥(——):;「」‘、,|《。》?、【】\\[\\]]\$")



    // 對輸入攔截

    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

    }


}

總計60行代碼,能夠搞定通常需求啦,再來看看其佈局用法(xml文件),平時怎麼在佈局寫EditText,仍是怎麼寫!3d

相關文章
相關標籤/搜索