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

ofollow,noindex">Github地址 ,歡迎點贊,fork
今天帶來工作中的一個小安利,產品要求對使用者名稱輸入需要限制,只能是數字和字母,符號, 不能包含空格和鍵盤上輸入的emoji .開始拿到這個需求,覺得給 EditText 增加一個 addTextChangedListener ,裡面做各種判斷不就OK 啦!
哈哈,又可以愉快的玩耍咯...
但是回撥裡面邏輯太多,看著也不爽,不符合我們程式員的氣質,簡潔大方,乾淨利落!所以我特意去看了 du 了一下, 結合自己的實際要求,重寫了 EditText 的 onCreateInputConnection() 方法,在那裡做文章,請看下面原始碼(如果還有不清楚的,可以留言或者看 Github地址 )
只需要自定義EditText重寫其onCreateInputConnection()方法,然後再定義一個內部類就好,下面程式碼即拷即用
首先,看看 LimitEditText
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,還是怎麼寫!
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="cn.molue.jooyer.limitedittext.MainActivity"> <cn.molue.jooyer.limitedittext.LimitEditText android:id="@+id/let_main" android:layout_width="match_parent" android:layout_height="50dp" android:layout_margin="10dp" android:text="Hello World!" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent"/> </android.support.constraint.ConstraintLayout> 複製程式碼
最後來看看在 Activity 中用法,其實和一般普通 EditText 用法一致啦!
class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) // demo 中預設 LimitEditText 只能輸入字母數字和標點符號 // 延時主要是更方便觀察 window.decorView.postDelayed({ // 注意,獲得焦點需要自己再處理下,其實很簡單,如下: let_main.isFocusable = true let_main.isFocusableInTouchMode = true let_main.requestFocus() },1000) } } 複製程式碼
當然,這些限制正則也可以在 LimitEditText 中定義方法,大家需要什麼加入什麼就好了!