Android编程中EditText限制文字输入的方法
作者:Just run 时间:2023-07-19 07:30:48
本文实例讲述了Android编程中EditText限制文字输入的方法。分享给大家供大家参考,具体如下:
Android的编辑框控件EditText在平常编程时会经常用到,有时候会对编辑框增加某些限制,如限制只能输入数字,最大输入的文字个数,不能输入一些非法字符等,这些需求有些可以使用android控件属性直接写在布局xml文件里,比如android:numeric="integer"(只允许输入数字);
对于一些需求,如非法字符限制(例如不允许输入#号,如果输入了#给出错误提示),做成动态判断更方便一些,而且容易扩展;
在Android里使用TextWatcher接口可以很方便的对EditText进行监听;TextWatcher中有3个函数需要重载:
public void beforeTextChanged(CharSequence s, int start, int count, int after);
public void onTextChanged(CharSequence s, int start, int before, int count);
public void afterTextChanged(Editable s);
从函数名就可以知道其意思,每当敲击键盘编辑框的文字改变时,上面的三个函数都会执行,beforeTextChanged可以给出变化之前的内容,onTextChanged和afterTextChanged给出追加上新的字符之后的文本;
所以对字符的限制判断可以在afterTextChanged函数中进行,如果检查到新追加的字符为认定的非法字符,则在这里将其delete掉,那么他就不会显示在编辑框里了:
private final TextWatcher mTextWatcher = new TextWatcher() {
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
public void afterTextChanged(Editable s) {
if (s.length() > 0) {
int pos = s.length() - 1;
char c = s.charAt(pos);
if (c == '#') {
//这里限制在字串最后追加#
s.delete(pos,pos+1);
Toast.makeText(MyActivity.this, "Error letter.",Toast.LENGTH_SHORT).show();
}
}
}
};
注册监听:
EditText mEditor = (EditText)findViewById(R.id.editor_input);
mEditor.addTextChangedListener(mTextWatcher);
希望本文所述对大家Android程序设计有所帮助。
标签:Android,EditText
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Android游戏开发学习①弹跳小球实现方法
2022-04-27 02:48:14
![](https://img.aspxhome.com/file/2023/4/117154_0s.png)
Android开发手册TextView属性实现效果盘点
2022-03-10 09:29:53
![](https://img.aspxhome.com/file/2023/2/109192_0s.jpg)
IDEA部署JavaWeb项目到Tomcat服务器的方法
2023-11-02 23:21:22
android轻量级无侵入式管理数据库自动升级组件
2023-12-09 15:36:01
![](https://img.aspxhome.com/file/2023/4/130334_0s.png)
idea2017建立jsp工程及tomcat配置教程
2023-06-04 20:24:40
![](https://img.aspxhome.com/file/2023/2/119052_0s.png)
Java Timer使用讲解
2023-11-28 20:30:33
使用@Value值注入及配置文件组件扫描
2023-12-01 21:24:12
java poi解析word的方法
2023-08-28 07:31:28
IDEA设置背景为自定义照片的操作方法
2022-12-28 09:13:08
![](https://img.aspxhome.com/file/2023/5/61645_0s.jpg)
FileShare枚举的使用小结(文件读写锁)
2023-10-21 09:26:30
![](https://img.aspxhome.com/file/2023/0/100210_0s.png)
java8 Stream大数据量List分批处理切割方式
2023-02-10 22:34:36
详解J2EE开发的网站部署到阿里云服务器的方法
2022-06-21 07:50:16
![](https://img.aspxhome.com/file/2023/9/64959_0s.jpg)
SpringBoot多模块项目框架搭建过程解析
2022-10-02 06:12:34
spring boot使用logback实现多环境日志配置详解
2022-10-03 20:38:24
![](https://img.aspxhome.com/file/2023/3/99853_0s.png)
springboot logback调整mybatis日志级别无效的解决
2023-05-03 05:36:21
详解Java利用深度优先遍历解决迷宫问题
2022-08-20 02:46:54
c# WPF中CheckBox样式的使用总结
2023-07-17 15:44:46
![](https://img.aspxhome.com/file/2023/1/88421_0s.gif)
C# ComboBox控件“设置 DataSource 属性后无法修改项集合”的完美解决方法
2023-01-30 04:11:58
SpringBoot多数据源切换实现代码(Mybaitis)
2021-11-07 11:15:50
![](https://img.aspxhome.com/file/2023/6/92716_0s.png)
SpringCloud分布式链路追踪组件Sleuth配置详解
2023-11-28 23:58:42
![](https://img.aspxhome.com/file/2023/6/60666_0s.png)