Android最簡單的限制輸入方法(只包含數(shù)字、字母和符號)
前言
Android的編輯框控件EditText在平常編程時會經(jīng)常用到,有時候會對編輯框增加某些限制,如限制只能輸入數(shù)字,最大輸入的文字個數(shù),不能輸入一些非法字符等,本文就來給大家介紹了一種最簡單的輸入限制方法。
效果圖
Github地址,歡迎點(diǎn)贊,fork
今天帶來工作中的一個小安利,產(chǎn)品要求對用戶名輸入需要限制,只能是數(shù)字和字母,符號,不能包含空格和鍵盤上輸入的emoji.開始拿到這個需求,覺得給 EditText 增加一個 addTextChangedListener ,里面做各種判斷不就OK 啦!
哈哈,又可以愉快的玩???..
但是回調(diào)里面邏輯太多,看著也不爽,不符合我們程序員的氣質(zhì),簡潔大方,干凈利落!所以我特意去看了 du 了一下, 結(jié)合自己的實(shí)際要求,重寫了 EditText 的 onCreateInputConnection() 方法,在那里做文章,請看下面源碼(如果還有不清楚的,可以留言或者看Github地址)
方法如下:
只需要自定義EditText重寫其onCreateInputConnection()方法,然后再定義一個內(nèi)部類就好,下面代碼即拷即用
首先,看看 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) { // 數(shù)字,字母 private val pattern = Pattern.compile("^[0-9A-Za-z_]\$") // 標(biāo)點(diǎn) 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) // 英文標(biāo)點(diǎn) private val patternEn = Pattern.compile("^[`~!@#\$%^&*()_\\-+=<>?:\"{},.\\\\/;'\\[\\]]\$") // 中文標(biāo)點(diǎn) 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 中用法,其實(shí)和一般普通 EditText 用法一致啦!
class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) // demo 中默認(rèn) LimitEditText 只能輸入字母數(shù)字和標(biāo)點(diǎn)符號 // 延時主要是更方便觀察 window.decorView.postDelayed({ // 注意,獲得焦點(diǎn)需要自己再處理下,其實(shí)很簡單,如下: let_main.isFocusable = true let_main.isFocusableInTouchMode = true let_main.requestFocus() },1000) } }
當(dāng)然,這些限制正則也可以在 LimitEditText 中定義方法,大家需要什么加入什么就好了!
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。
- Android EditText限制輸入字?jǐn)?shù)的方法
- Android編程中EditText限制文字輸入的方法
- Android開發(fā)中給EditText控件添加TextWatcher監(jiān)聽實(shí)現(xiàn)對輸入字?jǐn)?shù)的限制(推薦)
- Android EditText限制輸入字符的方法總結(jié)
- Android EditText限制輸入字符類型的方法總結(jié)
- Android TextWatcher監(jiān)控EditText中的輸入內(nèi)容并限制其個數(shù)
- Android EditText限制輸入整數(shù)和小數(shù)的位數(shù)的方法示例
相關(guān)文章
Android中Android Virtual Device(AVD)使用教程
這篇文章主要介紹了Android中Android Virtual Device(AVD)使用教程,本文還對使用過程中發(fā)生的一些錯誤給出了處理方法,需要的朋友可以參考下2015-01-01Android同步屏障機(jī)制sync barrier實(shí)例應(yīng)用詳解
這篇文章主要介紹了Android同步屏障機(jī)制sync barrier實(shí)例應(yīng)用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2023-02-02Android CountDownTimer實(shí)現(xiàn)倒計時器
這篇文章主要為大家詳細(xì)介紹了Android CountDownTimer實(shí)現(xiàn)倒計時效果的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-02-02Android實(shí)現(xiàn)城市選擇三級聯(lián)動
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)城市選擇三級聯(lián)動,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2020-12-12Android之自定義實(shí)現(xiàn)BaseAdapter(通用適配器一)
這篇文章主要為大家詳細(xì)介紹了Android之自定義實(shí)現(xiàn)BaseAdapter通用適配器第一篇,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-08-08Flutter?DateTime獲取本月的開始時間與結(jié)束時間方法
這篇文章主要為大家介紹了Flutter?DateTime獲取本月的開始時間與結(jié)束時間方法示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步早日升職加薪2023-05-05Flutter使用JsBridge方式處理Webview與H5通信的方法
這篇文章主要介紹了Flutter使用JsBridge方式處理Webview與H5通信的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04Android 利用 APT 技術(shù)在編譯期生成代碼
本文主要講解Android 利用 APT 技術(shù)在編譯期生成代碼,這里提供詳細(xì)的資料,并講解如何實(shí)現(xiàn),有興趣的小伙伴可以參考下2016-08-08