限制EditText的输入字数
private EditText edit_student_name;
edit_student_name.addTextChangedListener(changeStudentNameWatcher); private TextWatcher changeStudentNameWatcher = new TextWatcher() {
private int editStart ;
private int editEnd ;
public void afterTextChanged(Editable s) { edit_student_name.setSelection(edit_student_name.length());
editStart = edit_student_name.getSelectionStart();
editEnd = edit_student_name.getSelectionEnd();
// 先去掉监听器,否则会出现栈溢出
edit_student_name
.removeTextChangedListener(changeStudentNameWatcher);
// 注意这里只能每次都对整个EditText的内容求长度,不能对删除的单个字符求长度
// 因为是中英文混合,单个字符而言,calculateLength函数会返回1或2
long calculateLength = CalculateStringLength(s.toString());
if (calculateLength > MAX_NAME_COUNT_CLASSNAME ) {
Toast. makeText(getApplicationContext(), "最多输入10个字符",
Toast. LENGTH_SHORT).show();
}
while (calculateLength > MAX_NAME_COUNT_CLASSNAME ) { // 当输入字符个数超过限制的大小时,进行截断操作
s.delete( editStart - 1, editEnd );
editStart--;
editEnd--;
calculateLength =CalculateStringLength(s.toString());
}
edit_student_name.setSelection(editStart );
// 恢复监听器
edit_student_name.addTextChangedListener(changeStudentNameWatcher );
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
public void onTextChanged(CharSequence s, int start, int before,
int count) {
}
}; /**
*
* @方法名称:CalculateStringLength
* @描述: 计算字符长度(对于单个字符,汉字返回2其他字符返回1)
* @创建人:LiPengBo
* @创建时间:2014-6-5 下午3:06:09
* @备注:
* @param str
* @return
* @返回类型:int
*/
public int CalculateStringLength(String str){
String aString =str;
String anotherString = null;
try {
anotherString = new String(aString.getBytes("GBK"), "ISO8859_1");
}
catch (UnsupportedEncodingException ex) {
}
return anotherString.length();
}
限制EditText的输入字数的更多相关文章
- 【移动开发】EditText输入字数限制总结(包括中文输入内存溢出的解决方法)
限定EditText输入个数的解决方案很多,但是一般主要考虑两点,也就是处理两件事:(1)不同语言字符(英文.中文等)处理方式(2)输入字符达到数目后,是否仍然允许用户输入 第一点,涉及的东东其实蛮多 ...
- Android EditText输入字数限制总结(包含中文输入内存溢出的解决方法)
转载请注明,大飞:http://blog.csdn.net/rflyee/article/details/38856539 限定EditText输入个数的解决方式非常多,可是一般主要考虑两点.也就是处 ...
- Android---3种方式限制EditView输入字数(转载)
方法一:利用TextWatcher editText.addTextChangedListener(new TextWatcher() { private CharSequence temp; pri ...
- Android限定EditText的输入类型为数字或者英文(包括大小写),EditText,TextView只能输入两位小数
Android限定EditText的输入类型为数字或者英文(包括大小写) // 监听密码输入框的输入内容类型,不可以输入中文 TextWatcher mTextWatcher = new Tex ...
- 限制EditText只能输入小数点后两位
设置EditText只能输入小数点后两位,在价格等有限制的输入时特别有效 TextWatcher textWatcher = new TextWatcher() { @Override public ...
- Android 开发之拦截EditText的输入内容,定制输入内容
1.EditText作为一个比较成熟的View,在Android的应用开发中得到极为广泛的使用.在某些特殊情况下,我们可能需要定制EditText的输入内容, 只允许指定功能的输入,例如输入一个”dd ...
- 实现textarea限制输入字数(包含中文只能输入10个,全ASCII码能够输入20个)
document.getElementById("<%=textBox1.ClientID %>").value 实现textarea限制输入字数(包含中文只能输入10 ...
- 监听文本框输入开发仿新浪微博限制输入字数的textarea插件
监听文本框输入 Firefox.Chrome.IE9,IE10 均支持 oninput 事件,此外所有版本的 IE 均支持 onpropertychange 事件. oninput 事件在用户输入.退 ...
- JS动态呈现还可以输入字数
现在觉得当我们使用js或者jquery来呈现一个动态效果时,主要还是要想清楚它的思想.它的原理.而动态呈现输入字数,其实就是给它设置一个最大输入字数,然后获取已输入的字数,自然想做什么都可以. < ...
随机推荐
- Ubuntu Linux 环境变量
2011年09月17日 Ubuntu 下设置adb环境变量 分类: 同Windows一样,Ubuntu Linux系统包含两类环境变量:系统环境变量和用户环境变量.系统环境变量对所有系统用户都有效,用 ...
- Swaps in Permutation
Swaps in Permutation You are given a permutation of the numbers 1, 2, ..., n and m pairs of position ...
- 【2013 ICCV】Abnormal Event Detection at 150 FPS in MATLAB
2. method 把每一帧变成不同尺度,并且把每一层分成互不覆盖的patch.连续的5帧堆叠在一起形成时空立方体.对时空立方体计算3D梯度特征[11].这些视屏序列中的特征,根据空间位置的相关性,进 ...
- Python3基础 list(reversed()) 将一个列表逆转并输出
镇场诗: 诚听如来语,顿舍世间名与利.愿做地藏徒,广演是经阎浮提. 愿尽吾所学,成就一良心博客.愿诸后来人,重现智慧清净体.-------------------------------------- ...
- mark一篇文章--用nodejs搭建一个本地反向代理环境
调试线上代码的时候,我们经常遇到的一个问题就是 本地一套环境,线上一套环境,本地没有的文件用线上的这种需求.我简单来说下使用nodejs如何做到. 先说下不用nodejs我们怎么做,工具比如fiddl ...
- mtp
http://www.android.gs/mount-google-nexus-4-mtp-sd-card-on-ubuntu-and-other-linux-computers/ https:// ...
- read gc log
http://www.aichengxu.com/article/Java/18368_2.html https://blogs.oracle.com/poonam/entry/understandi ...
- Netbeans 6.8 + apktool_2.0.0b9 动态调试smali文件
前言 很早就知道用Netbeans能够单步调试smali,一直拖到现在才真正的自己实现了一次~ 下面是详细步骤! 0×1 环境及工具 a.apktool_2.0.0b9 下载地址:http://con ...
- Linux的iptables常用配置范例(3)
编辑/etc/rc.local,加入iptables -t nat -A POSTROUTING -s 192.168.0.0/24 -o eth1 -j MASQUERADE,外网口eth1为dhc ...
- pandas 基础
pandas 是基于 Numpy 构建的含有更高级数据结构和工具的数据分析包 类似于 Numpy 的核心是 ndarray,pandas 也是围绕着 Series 和 DataFrame 两个核心数据 ...