最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android最簡單的限制輸入方法(只包含數(shù)字、字母和符號(hào))

 更新時(shí)間:2018年11月28日 09:09:51   作者:天星技術(shù)團(tuán)隊(duì)  
這篇文章主要給大家介紹了關(guān)于Android最簡單的限制輸入的實(shí)現(xiàn)方法,限制輸入框只能輸入數(shù)字、字母和符號(hào),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起看看 吧

前言

Android的編輯框控件EditText在平常編程時(shí)會(huì)經(jīng)常用到,有時(shí)候會(huì)對(duì)編輯框增加某些限制,如限制只能輸入數(shù)字,最大輸入的文字個(gè)數(shù),不能輸入一些非法字符等,本文就來給大家介紹了一種最簡單的輸入限制方法。

效果圖

Github地址,歡迎點(diǎn)贊,fork

今天帶來工作中的一個(gè)小安利,產(chǎn)品要求對(duì)用戶名輸入需要限制,只能是數(shù)字和字母,符號(hào),不能包含空格和鍵盤上輸入的emoji.開始拿到這個(gè)需求,覺得給 EditText 增加一個(gè) addTextChangedListener ,里面做各種判斷不就OK 啦!

哈哈,又可以愉快的玩???..

但是回調(diào)里面邏輯太多,看著也不爽,不符合我們程序員的氣質(zhì),簡潔大方,干凈利落!所以我特意去看了 du 了一下, 結(jié)合自己的實(shí)際要求,重寫了 EditText  的 onCreateInputConnection() 方法,在那里做文章,請(qǐng)看下面源碼(如果還有不清楚的,可以留言或者看Github地址)

方法如下:

只需要自定義EditText重寫其onCreateInputConnection()方法,然后再定義一個(gè)內(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("^[·!#¥(——):;“”‘、,|《。》?、【】\\[\\]]\$")


 // 對(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行代碼,可以搞定一般需求啦,再來看看其布局用法(xml文件),平時(shí)怎么在布局寫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)符號(hào)

  // 延時(shí)主要是更方便觀察
  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)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問大家可以留言交流,謝謝大家對(duì)腳本之家的支持。

相關(guān)文章

最新評(píng)論

阳泉市| 镇雄县| 林州市| 手游| 兴安县| 东港市| 定州市| 沙坪坝区| 子洲县| 五华县| 新化县| 贵定县| 濉溪县| 通江县| 乌拉特前旗| 湄潭县| 阳山县| 西峡县| 大英县| 江北区| 馆陶县| 长宁区| 延吉市| 山阴县| 康平县| 西华县| 万全县| 额济纳旗| 福清市| 泰州市| 马鞍山市| 辽阳市| 府谷县| 武平县| 哈密市| 绥阳县| 兴文县| 兰坪| 醴陵市| 定南县| 汉中市|