前言
10年積累的成都網(wǎng)站建設(shè)、成都網(wǎng)站制作經(jīng)驗(yàn),可以快速應(yīng)對(duì)客戶(hù)對(duì)網(wǎng)站的新想法和需求。提供各種問(wèn)題對(duì)應(yīng)的解決方案。讓選擇我們的客戶(hù)得到更好、更有力的網(wǎng)絡(luò)服務(wù)。我雖然不認(rèn)識(shí)你,你也不認(rèn)識(shí)我。但先做網(wǎng)站后付款的網(wǎng)站建設(shè)流程,更有宜良免費(fèi)網(wǎng)站建設(shè)讓你可以放心的選擇與我們合作。
Android的編輯框控件EditText在平常編程時(shí)會(huì)經(jīng)常用到,有時(shí)候會(huì)對(duì)編輯框增加某些限制,如限制只能輸入數(shù)字,最大輸入的文字個(gè)數(shù),不能輸入一些非法字符等,本文就來(lái)給大家介紹了一種最簡(jiǎn)單的輸入限制方法。
效果圖
Github地址,歡迎點(diǎn)贊,fork
今天帶來(lái)工作中的一個(gè)小安利,產(chǎn)品要求對(duì)用戶(hù)名輸入需要限制,只能是數(shù)字和字母,符號(hào),不能包含空格和鍵盤(pán)上輸入的emoji.開(kāi)始拿到這個(gè)需求,覺(jué)得給 EditText 增加一個(gè) addTextChangedListener ,里面做各種判斷不就OK 啦!
哈哈,又可以愉快的玩???..
但是回調(diào)里面邏輯太多,看著也不爽,不符合我們程序員的氣質(zhì),簡(jiǎn)潔大方,干凈利落!所以我特意去看了 du 了一下, 結(jié)合自己的實(shí)際要求,重寫(xiě)了 EditText 的 onCreateInputConnection() 方法,在那里做文章,請(qǐng)看下面源碼(如果還有不清楚的,可以留言或者看Github地址)
方法如下:
只需要自定義EditText重寫(xiě)其onCreateInputConnection()方法,然后再定義一個(gè)內(nèi)部類(lè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("^[·!#¥(——):;“”‘、,|《。》?、【】\\[\\]]\$") // 對(duì)輸入攔截 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 } }
總計(jì)60行代碼,可以搞定一般需求啦,再來(lái)看看其布局用法(xml文件),平時(shí)怎么在布局寫(xiě)EditText,還是怎么寫(xiě)!
<?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>
最后來(lái)看看在 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)符號(hào) // 延時(shí)主要是更方便觀察 window.decorView.postDelayed({ // 注意,獲得焦點(diǎn)需要自己再處理下,其實(shí)很簡(jiǎn)單,如下: let_main.isFocusable = true let_main.isFocusableInTouchMode = true let_main.requestFocus() },1000) } }
當(dāng)然,這些限制正則也可以在 LimitEditText 中定義方法,大家需要什么加入什么就好了!
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問(wèn)大家可以留言交流,謝謝大家對(duì)創(chuàng)新互聯(lián)的支持。
本文題目:Android最簡(jiǎn)單的限制輸入方法(只包含數(shù)字、字母和符號(hào))
文章位置:http://aaarwkj.com/article30/jjjiso.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站改版、域名注冊(cè)、搜索引擎優(yōu)化、、用戶(hù)體驗(yàn)、做網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)