欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

前言

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í)價值,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。

相關(guān)文章

  • Android中Android Virtual Device(AVD)使用教程

    Android中Android Virtual Device(AVD)使用教程

    這篇文章主要介紹了Android中Android Virtual Device(AVD)使用教程,本文還對使用過程中發(fā)生的一些錯誤給出了處理方法,需要的朋友可以參考下
    2015-01-01
  • Android實(shí)現(xiàn)串口通信

    Android實(shí)現(xiàn)串口通信

    這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)串口通信,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-08-08
  • Android同步屏障機(jī)制sync barrier實(shí)例應(yīng)用詳解

    Android同步屏障機(jī)制sync barrier實(shí)例應(yīng)用詳解

    這篇文章主要介紹了Android同步屏障機(jī)制sync barrier實(shí)例應(yīng)用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧
    2023-02-02
  • Android支付寶和微信支付集成

    Android支付寶和微信支付集成

    這篇文章主要為大家詳細(xì)介紹了Android支付寶和微信支付集成的相關(guān)資料,需要的朋友可以參考下
    2016-07-07
  • Android CountDownTimer實(shí)現(xiàn)倒計時器

    Android CountDownTimer實(shí)現(xiàn)倒計時器

    這篇文章主要為大家詳細(xì)介紹了Android CountDownTimer實(shí)現(xiàn)倒計時效果的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-02-02
  • Android實(shí)現(xiàn)城市選擇三級聯(lián)動

    Android實(shí)現(xiàn)城市選擇三級聯(lián)動

    這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)城市選擇三級聯(lián)動,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-12-12
  • Android之自定義實(shí)現(xiàn)BaseAdapter(通用適配器一)

    Android之自定義實(shí)現(xiàn)BaseAdapter(通用適配器一)

    這篇文章主要為大家詳細(xì)介紹了Android之自定義實(shí)現(xiàn)BaseAdapter通用適配器第一篇,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-08-08
  • Flutter?DateTime獲取本月的開始時間與結(jié)束時間方法

    Flutter?DateTime獲取本月的開始時間與結(jié)束時間方法

    這篇文章主要為大家介紹了Flutter?DateTime獲取本月的開始時間與結(jié)束時間方法示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步早日升職加薪
    2023-05-05
  • Flutter使用JsBridge方式處理Webview與H5通信的方法

    Flutter使用JsBridge方式處理Webview與H5通信的方法

    這篇文章主要介紹了Flutter使用JsBridge方式處理Webview與H5通信的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-04-04
  • Android 利用 APT 技術(shù)在編譯期生成代碼

    Android 利用 APT 技術(shù)在編譯期生成代碼

    本文主要講解Android 利用 APT 技術(shù)在編譯期生成代碼,這里提供詳細(xì)的資料,并講解如何實(shí)現(xiàn),有興趣的小伙伴可以參考下
    2016-08-08

最新評論