android edittext inputfilter,android – EditText和InputFilter会导致重复的文本
我试图实现一个限制输入到alpha字符[A-Za-z]的EditText.
我从this post开始使用InputFilter方法.当我输入“a%”时,文本消失,如果我退回空格,文本是“a”.我已经尝试过滤器功能上的其他变体,如使用正则表达式仅匹配[A-Za-z],有时会看到像重复字符一样的疯狂行为,我将键入“a”然后键入“b”,然后获取“aab”键入“c”并得到“aabaabc”然后命中空格并获得“aabaabcaabaabc”!
这是我正在使用的代码与我尝试过的不同的方法.
EditText input = (EditText)findViewById( R.id.inputText );
InputFilter filter = new InputFilter() {
@Override
public CharSequence filter( CharSequence source, int start, int end, Spanned dest, int dstart, int dend ) {
//String data = source.toString();
//String ret = null;
/*
boolean isValid = data.matches( "[A-Za-z]" );
if( isValid ) {
ret = null;
}
else {
ret = data.replaceAll( "[@#$%^&*]", "" );
}
*/
/*
dest = new SpannableStringBuilder();
ret = data.replaceAll( "[@#$%^&*]", "" );
return ret;
*/
for( int i = start; i < end; i++ ) {
if( !Character.isLetter( source.charAt( i ) ) ) {
return "";
}
}
return null;
}
};
input.setFilters( new InputFilter[]{ filter } );
我完全忍不住在这一个,所以任何帮助在这里将不胜感激.
编辑:
好的,我已经对InputFilter进行了很多实验,并得出了一些结论,尽管没有解决问题.请参阅我的代码中的注释.我现在要尝试Imran Rana的解决方案.
EditText input = (EditText)findViewById( R.id.inputText );
InputFilter filter = new InputFilter() {
// It is not clear what this function should return!
// Docs say return null to allow the new char(s) and return "" to disallow
// but the behavior when returning "" is inconsistent.
//
// The source parameter is a SpannableStringBuilder if 1 char is entered but it
// equals the whole string from the EditText.
// If more than one char is entered (as is the case with some keyboards that auto insert
// a space after certain chars) then the source param is a CharSequence and equals only
// the new chars.
@Override
public CharSequence filter( CharSequence source, int start, int end, Spanned dest, int dstart, int dend ) {
String data = source.toString().substring( start, end );
String retData = null;
boolean isValid = data.matches( "[A-Za-z]+" );
if( !isValid ) {
if( source instanceof SpannableStringBuilder ) {
// This works until the next char is evaluated then you get repeats
// (Enter "a" then "^" gives "a". Then enter "b" gives "aab")
retData = data.replaceAll( "[@#$%^&*']", "" );
// If I instead always returns an empty string here then the EditText is blanked.
// (Enter "a" then "^" gives "")
//retData = "";
}
else { // source is instanceof CharSequence
// We only get here if more than 1 char was entered (like "& ").
// And again, this works until the next char is evaluated then you get repeats
// (Enter "a" then "& " gives "a". Then enter "b" gives "aab")
retData = "";
}
}
return retData;
}
};
input.setFilters( new InputFilter[]{ filter } );
android edittext inputfilter,android – EditText和InputFilter会导致重复的文本相关推荐
- android 代码设置inputtype,android – 如何正确设置EditText的InputType?
我希望我的EditText内容类型为数字.我根据 reference将InputType设置为2.但是仍然可以输入任何字符. final EditText input = new EditText(t ...
- android 只能输入汉字,EditText限制输入的几种方式及只显示中文汉字的做法
前段时间项目中有个需求是要求只能输入汉字,并且不能输入偏旁部首,于是总结了下EditText限制输入的几种方式,但是对于语音输入的还没找到好的解决方案: 通过设置EditText的inputType来 ...
- 【转】android 中如何限制 EditText 最大输入字符数
原文网址:http://blog.csdn.net/fulinwsuafcie/article/details/7437768 方法一: 在 xml 文件中设置文本编辑框属性作字符数限制 如:andr ...
- android edittext最多输入,android 中如何限制 EditText 最大输入字符数
方法一: 在 xml 文件中设置文本编辑框属性作字符数限制 如:android:maxLength="10" 即限制最大输入字符个数为10 方法二: 在代码中使用InputFilt ...
- Android字数限制的EditText实现方案研究
在应用开发中,有时需要实现有字数限制的EditText,首先来分析下市面上存在的类似实现方案吧,好有个感性的认识. [方案一:腾讯微博] 每个中文字符算一个字数,每两个英文字符算一个字数,当用户输入内 ...
- Android实战场景 - 限制EditText仅支持输入数字、英文、汉字,禁止输入表情等特殊符号
因项目需求,需要禁止用户输入表情符号,具体如下 ~ EditText相关Blog TextView.EditText属性大全 监听 EditText 文本变化 设置 EditText 光标颜色与下划线 ...
- Android字数限制的EditText实现
在应用开发中,有时需要实现有字数限制的EditText,首先来分析下市面上存在的类似实现方案吧,好有个感性的认识. [方案一:腾讯微博] 每个中文字符算一个字数,每两个英文字符算一个字数,当用户输入内 ...
- android edittext过滤表情,EditText过滤表情符号
项目中我们通常可以通过第三方输入法项Android文本框中输入表情符号,但是后台解析时可能会报错,所以通常我们都会屏蔽掉EditText的表情符号输入.下面介绍一下自己为了解决这个问题爬过的坑. 第一 ...
- android 中如何限制 EditText 最大输入字符数
方法一: 在 xml 文件中设置文本编辑框属性作字符数限制 如:android:maxLength="10" 即限制最大输入字符个数为10 方法二: 在代码中使用InputFilt ...
最新文章
- Python 程序如何高效地调试?
- php多维数组和对象,在PHP中将多维多对象数组转换为标准多维数组
- PCB板上字母表示的含义
- gulp几个常见问题及解决方案
- 在Kmplayer中设置多字幕
- 用python将图片写入ppt_用python做ppt服务用于导入图片
- 中班音乐活动 机器人_【家门口的好幼儿园】玩转音乐,释放天性——岳阳幼儿园开展松江区骨干共同体名师二团实践研究活动...
- 漫谈工业软件(4)-关于开源工业控制软件
- a标签去下划线或文字添加下修饰_a标签去掉下划线_百度经验
- ubuntu设置桌面图标
- 读书之二 --《程序员修炼之道》
- pacman安装ubuntu_pacman命令 – 软件包管理器
- 网易的又一款榜首之作,《倩女幽魂》营销负责人解析
- VB模拟满天星空闪烁的效果
- PC能登录微信,但是不能访问网页/代理服务器没有响应
- 文件重命名,文件名快速修改重命名
- 51单片机基础之蜂鸣器
- XP下如何共享文件,及开启相应的服务
- python爬虫之搜索51job并存入mysql数据库
- 如何带领好团队,增强团队的执行力?
热门文章
- native层 安卓_安卓逆向学习入门之过反调试(一)
- 通俗易懂的MonteCarlo积分方法(七)
- flutter笔记:使用flutter webvie
- Spark内核解析之五:Spark Shuffle解析
- java我的世界1.14.4_我的世界1.14.4国际版下载
- python实现logistic增长模型
- python︱imagehash中的四种图像哈希方式(phash/ahash/dhash/小波hash)
- 理解mini-batch、Momentum、RMSpror、Admin优化算法
- postgre ~模糊查询慢解决方式
- 【开源】微信小程序、小游戏以及 Web 通用 Canvas 渲染引擎 - Cax