Android 最简单的限制输入方式之一
今天带来工作中的一个小安利,产品要求对用户名输入需要限制,只能是数字和字母,符号,不能包含空格和键盘上输入的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 中定义方法,大家需要什么加入什么就好了!
原文发布时间为:2018-11-20
本文来自云栖社区合作伙伴“Android开发中文站”,了解相关信息可以关注“Android开发中文站”。
低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
Android 中内存泄漏的原因和解决方案
前言 之前研究过一段时间关于 Android 内存泄漏的知识,大致了解了导致内存泄漏的一些原因,但是没有深入去探究,很多细节也理解的不够透彻,基本上处于一种似懂非懂的状态,最近又研究了一波,发现有很多新的收获,遂在此记录一些心得体会。 首先引用一下开源项目 LearningNotes 中关于 Java 内存分配策略和 Java 是如何管理内存的说明。 Java 内存分配策略 Java 程序运行时的内存分配策略有三种,分别是静态分配,栈式分配,和堆式分配,对应的,三种存储策略使用的内存空间主要分别是静态存储区(也称方法区)、栈区和堆区。 ● 静态存储区(方法区):主要存放静态数据、全局 static 数据和常量。这块内存在程序编译时就已经分配好,并且在程序整个运行期间都存在。 ● 栈区 :当方法被执行时,方法体内的局部变量(其中包括基础数据类型、对象的引用)都在栈上创建,并在方法执行结束时这些局部变量所持有的内存将会自动被释放。因为栈内存分配运算内置于处理器的指令集中,效率很高,但是分配的内存容量有限。 ●堆区 : 又称动态内存分配,通常就是指在程序运行时直接 new 出来的内存,也就是...
- 下一篇
Notification通知栏
目录介绍 1.Notification简单概述 2.Notification通知用途 3.Notification的基本操作 3.1 Notification创建必要的属性 3.2 Notification简单创建步骤 3.3 关于setSmallIcon()与setLargeIcon()区别 3.4 Notification的Action属性【交互作用】 3.5 更新Notification 3.6 取消Notification 3.7 设置flag属性 3.8 设置Notification的通知效果 3.9 设置自定义Notification通知栏布局 4.Notification相关属性说明 4.1 PendingIntent说明 4.2 创建返回栈PendingIntent 4.3 注意要点 5.部分源码分析思考 5.1 RemoteView是什么? 5.2 查看源码,了解Notification如何创建布局 6.关于Android8.0通知栏适配 6.1 Android O(8.0)通知的改变 6.2 报错内容和解决方案 6.3 最终解决方案 7.关于其他 好消息 已经解决了...
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
推荐阅读
最新文章
- Docker使用Oracle官方镜像安装(12C,18C,19C)
- CentOS7,CentOS8安装Elasticsearch6.8.6
- CentOS6,CentOS7官方镜像安装Oracle11G
- CentOS7编译安装Cmake3.16.3,解决mysql等软件编译问题
- SpringBoot2编写第一个Controller,响应你的http请求并返回结果
- CentOS8编译安装MySQL8.0.19
- SpringBoot2配置默认Tomcat设置,开启更多高级功能
- Windows10,CentOS7,CentOS8安装Nodejs环境
- Docker快速安装Oracle11G,搭建oracle11g学习环境
- CentOS8,CentOS7,CentOS6编译安装Redis5.0.7