限制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 ...
随机推荐
- gaea-editor 项目使用
项目地址:https://github.com/ascoders/gaea-editor 打开编辑器界面:运行npm run docs 下载gaea-editor项目,进行调试,注意点: gaea-e ...
- Jenkins 安装启动提示“iJob for jenkins.service failed because the control process exited with error code. See "systemctl status jenkins.service" and "journalctl -xe" for details.”
通过RPM安装Jenkins简单方便,不太需要复杂的过程,但是在安装完成以后启动Jenkins的时候提示“Starting jenkins (via systemctl): Job for jenki ...
- java 泛型实现原理
泛型思想最早在C++语言的模板(Templates)中产生,Java后来也借用了这种思想.虽然思想一致,但是他们存在着本质性的不同. C++中的模板是真正意义上的泛型,在编译时就将不同模板类型参数编译 ...
- JavaMail SMTP服务器发送邮件程序示例 java通过dns服务器解析ip地址
/** * JavaMail SMTP服务器发送邮件程序示例 * 扮演SMTP服务器角色与邮件客户端软件最大的区别就是: * SMTP服务器需要解析不同接收人邮件地址主机名对应的SMTP服务器主机名 ...
- 主流开源SQL(on Hadoop)总结
转载至 大数据杂谈 (BigdataTina2016),同时参考学习 http://www.cnblogs.com/barrywxx/p/4257166.html 进行整理. 使用SQL 引擎一词是有 ...
- PHP-不同Str 拼接方法性能对比
问题 在PHP中,有多种字符串拼接的方式可供选择,共有: 1 . , .= , sprintf, vprintf, join, implode 那么,那种才是最快的,或者那种才是最适合业务使用的,需要 ...
- IK分词器的使用
1.下载 根据自己的版本进行下载 https://github.com/medcl/elasticsearch-analysis-ik/releases wget https://github.com ...
- ubuntu-docker入门到放弃(四)容器的导入导出
上一次我们讲了如何搭建自己私有的镜像管理仓库,实际上我们使用的依然是别人或者公共的image,今天就来说说如何将自己定制化的images上传到自己的私有仓库中,以供符合自己业务场景的项目来使用,如:我 ...
- <亲测>用navicat连接mysql 8.0 报错2059
ERROR 2059 (HY000): Authentication plugin 'caching_sha2_password' cannot be loaded 2018年05月07日 15:56 ...
- ribbon的注解使用报错--No instances available for [IP]
使用RestTemplate类调用其他系统的url的时候,加上ribbon的注解@LoadBalanced上这个注解之后访问,就报错了. 报错如下: 因为这里你不能直接访问地址,需要把地址改成你所调用 ...