限制EditText最多输入n位,设置EditText最大输入长度;
1、比较笨的方法:
/**
* 设置edittext最大的输入限制
* @param editText
* @param length
*/
private void setMaxLength(EditText editText , int length){
editText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
String str = s.toString();
if (str.length()>length){
editText.setText(str.substring(0,length)); //截取前x位
editText.requestFocus();
editText.setSelection(editText.getText().length()); //光标移动到最后
}
}
@Override
public void afterTextChanged(Editable s) {
}
});
}
如果重复设置,需要清空前一个;这是edittext的源码;
/**
* Adds a TextWatcher to the list of those whose methods are called
* whenever this TextView's text changes.
* <p>
* In 1.0, the {@link TextWatcher#afterTextChanged} method was erroneously
* not called after {@link #setText} calls. Now, doing {@link #setText}
* if there are any text changed listeners forces the buffer type to
* Editable if it would not otherwise be and does call this method.
*/
public void addTextChangedListener(TextWatcher watcher) {
if (mListeners == null) {
mListeners = new ArrayList<TextWatcher>();
}
mListeners.add(watcher);
}
/**
* Removes the specified TextWatcher from the list of those whose
* methods are called
* whenever this TextView's text changes.
*/
public void removeTextChangedListener(TextWatcher watcher) {
if (mListeners != null) {
int i = mListeners.indexOf(watcher);
if (i >= 0) {
mListeners.remove(i);
}
}
}
2、简单实用:
et.setFilters(new InputFilter[]{new NotChineseFilter(), new InputFilter.LengthFilter(4)}); //限制输入4位
限制EditText最多输入n位,设置EditText最大输入长度;的更多相关文章
- android edittext 限制小数点后最多只能输入两位数字
android:inputType="numberDecimal" private InputFilter lengthFilter = new InputFilter() { @ ...
- Android限定EditText的输入类型为数字或者英文(包括大小写),EditText,TextView只能输入两位小数
Android限定EditText的输入类型为数字或者英文(包括大小写) // 监听密码输入框的输入内容类型,不可以输入中文 TextWatcher mTextWatcher = new Tex ...
- 代码动态设置edittext输入类型为密码类型
开发中常常会用到EditText输入框,要将他的输入类型设置为密码输入,但是直接在布局文件中设置时,hint字体风格就会不一样 例如,在布局文件中直接设置是这样的(如下图),字体风格明显跟上一行的不一 ...
- android代码设置EditText只输入数字、字母
如何设置EditText,使得只能输入数字或者某些字母呢? 一.设置EditText,只输入数字: 方法1:直接生成DigitsKeyListener对象就可以了. et_1.setKeyLis ...
- 设置EditText控件中提示消息hint的字体颜色和大小
设置EditText控件中提示消息hint的字体颜色和大小 1.设置字体大小 代码例: public void init(){ hint= (EditText) findViewById(R.id.i ...
- EditText 详细信息(监听事件时,输入改变、透明背景、提示改变文字颜色、密文输入)
1.对EditText输入监视.给EditText 捆绑 addTextChangedListener 监控事件 能够. 2.EditText输入内容.密文显示: android:password=& ...
- JS限制input输入的为数字并且有小数的时候最多保留两位小数
JS限制input用户输入的为数字并且有小数的时候最多保留两位小数,代码如下: html部分: <input type="number" onkeypress="r ...
- Android 设置EditText光标Curso颜色及粗细
在android的输入框里,如果要修改光标的颜色及粗细步骤如下两步即可搞定: 1.在资源文件drawable下新建一个光标控制color_cursor.xml <?xml version=&qu ...
- 手机调用系统的拍照和裁剪功能,假设界面有输入框EditText,在一些手机会出现点击EditText会弹出输入法,却不能输入的情况。
1. 拍照裁剪后 点击EditText会弹出输入法,却不能输入.可是点击点一EdtiText就能够输入了,所以我就写了一个看不见的EdtiText,切换焦点,这样就攻克了这个奇怪的这问题,应该是and ...
随机推荐
- antd、react-router-dom v4 解决菜单和地址同步问题
点击不同的菜单,右边内容做相应的变化. 不过,对于后退操作和刷新页面操作无效,左边菜单无法保持选中项高亮. 解决思路如下: Menu 用 seletedKeys 来决定哪项被选中.需要判断当前选前的路 ...
- html 简单的table样式
效果预览: 代码: <!DOCTYPE html> <html> <head> <meta charset="utf-8"> < ...
- 2、Zookeeper端口和ip修改
clientPort=2181clientPortAddress=192.168.0.120
- Linux之chown
命令功能: chown将指定文件的拥有者改为指定的用户或组,用户可以是用户名或者用户ID:组可以是组名或者组ID:文件是以空格分开的要改变权限的文件列表,支持通配符.系统管理员经常使用chown命令, ...
- Video to SDI Tx Bridge模块video_data(SD-SDI)处理过程
Video to SDI Tx Bridge模块video_data(SD-SDI)处理过程 1.Top Level Block Diagram of Video to SDI TX Bridge V ...
- NSIS 制作简单安装包
;replace NSIS/Contrib/zip2exe/Modern.nsh;NSI format: Asni;http://nsis.sourceforge.net/ ;UAC级别Request ...
- linux svn客户端通过 https访问windows VisualSVN Server Manager
1)需求: 已经在阿里云windwos系统 下面安装了VisualSVN Server Manager 做为svn服务器: 现在要在腾讯云源码安装新版本客户端 2)开始源码编译安装TortoiseSV ...
- ALGO-17_蓝桥杯_算法训练_乘积最大(DP)
问题描述 今年是国际数学联盟确定的“——世界数学年”,又恰逢我国著名数学家华罗庚先生诞辰90周年.在华罗庚先生的家乡江苏金坛,组织了一场别开生面的数学智力竞赛的活动,你的一个好朋友XZ也有幸得以参加. ...
- C++进阶--显式类型转换(casting)
//############################################################################ /* * 显式类型转换 * * 类型转换 ...
- java中遍历实体类,获取属性名和属性值
方式一(实体类): //java中遍历实体类,获取属性名和属性值 public static void testReflect(Object model) throws Exception{ for ...