Android最简单的限制输入方法(只包含数字、字母和符号)
作者:天星技术团队 时间:2022-01-22 00:15:45
前言
Android的编辑框控件EditText在平常编程时会经常用到,有时候会对编辑框增加某些限制,如限制只能输入数字,最大输入的文字个数,不能输入一些非法字符等,本文就来给大家介绍了一种最简单的输入限制方法。
效果图
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 中定义方法,大家需要什么加入什么就好了!
来源:https://juejin.im/post/5be45876f265da616413820b
标签:android,输入,限制
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java接口默认方法带来的问题分析【二义性问题】
2023-11-27 20:32:55
![](https://img.aspxhome.com/file/2023/7/60117_0s.png)
Android 显示GIF图片实例详解
2023-08-06 09:11:52
![](https://img.aspxhome.com/file/2023/8/86758_0s.jpg)
Java实现小型图书馆管理系统
2021-06-14 04:27:35
![](https://img.aspxhome.com/file/2023/8/64878_0s.jpg)
java中timer的schedule和scheduleAtFixedRate方法区别详解
2023-05-25 10:47:45
Springboot笔记之热部署及不生效的解决方案
2023-11-03 02:40:00
![](https://img.aspxhome.com/file/2023/9/83569_0s.png)
C#异步调用的好处和方法分享
2023-04-08 11:10:56
Java基础教程之理解Annotation详细介绍
2022-02-28 10:07:12
示例解析java重载Overloading与覆盖Overriding
2021-11-03 17:22:19
Flutter控制组件显示和隐藏三种方式详解
2023-07-07 03:03:30
java开发中遇到的异常汇总详解
2023-03-21 02:54:15
Java Spring AOP源码解析之事务实现原理
2023-09-10 02:39:46
![](https://img.aspxhome.com/file/2023/5/66945_0s.png)
Spring的自动装配Bean的三种方式
2023-08-24 23:05:15
MyBatis-Plus拦截器实现数据权限控制的示例
2022-12-10 05:10:17
Java面试题冲刺第二十四天--并发编程
2023-08-31 05:39:02
![](https://img.aspxhome.com/file/2023/7/58447_0s.png)
java连接sql server 2008数据库代码
2023-05-27 10:40:01
JavaWeb中的常用的请求传参注解说明
2023-06-19 03:12:06
![](https://img.aspxhome.com/file/2023/1/76201_0s.png)
Java 10 局部变量类型推断浅析
2023-11-25 06:24:13
Spring框架中一个有用的小组件之Spring Retry组件详解
2021-05-27 10:07:40
Java多线程编程小实例模拟停车场系统
2022-07-07 06:36:29
Java同步容器和并发容器详解
2023-03-22 06:45:28
![](https://img.aspxhome.com/file/2023/5/82445_0s.jpg)