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最大输入长度;的更多相关文章

  1. android edittext 限制小数点后最多只能输入两位数字

    android:inputType="numberDecimal" private InputFilter lengthFilter = new InputFilter() { @ ...

  2. Android限定EditText的输入类型为数字或者英文(包括大小写),EditText,TextView只能输入两位小数

    Android限定EditText的输入类型为数字或者英文(包括大小写) // 监听密码输入框的输入内容类型,不可以输入中文    TextWatcher mTextWatcher = new Tex ...

  3. 代码动态设置edittext输入类型为密码类型

    开发中常常会用到EditText输入框,要将他的输入类型设置为密码输入,但是直接在布局文件中设置时,hint字体风格就会不一样 例如,在布局文件中直接设置是这样的(如下图),字体风格明显跟上一行的不一 ...

  4. android代码设置EditText只输入数字、字母

     如何设置EditText,使得只能输入数字或者某些字母呢? 一.设置EditText,只输入数字: 方法1:直接生成DigitsKeyListener对象就可以了. et_1.setKeyLis ...

  5. 设置EditText控件中提示消息hint的字体颜色和大小

    设置EditText控件中提示消息hint的字体颜色和大小 1.设置字体大小 代码例: public void init(){ hint= (EditText) findViewById(R.id.i ...

  6. EditText 详细信息(监听事件时,输入改变、透明背景、提示改变文字颜色、密文输入)

    1.对EditText输入监视.给EditText 捆绑 addTextChangedListener 监控事件 能够. 2.EditText输入内容.密文显示: android:password=& ...

  7. JS限制input输入的为数字并且有小数的时候最多保留两位小数

    JS限制input用户输入的为数字并且有小数的时候最多保留两位小数,代码如下: html部分: <input type="number" onkeypress="r ...

  8. Android 设置EditText光标Curso颜色及粗细

    在android的输入框里,如果要修改光标的颜色及粗细步骤如下两步即可搞定: 1.在资源文件drawable下新建一个光标控制color_cursor.xml <?xml version=&qu ...

  9. 手机调用系统的拍照和裁剪功能,假设界面有输入框EditText,在一些手机会出现点击EditText会弹出输入法,却不能输入的情况。

    1. 拍照裁剪后 点击EditText会弹出输入法,却不能输入.可是点击点一EdtiText就能够输入了,所以我就写了一个看不见的EdtiText,切换焦点,这样就攻克了这个奇怪的这问题,应该是and ...

随机推荐

  1. python string/list转换

    python的read.write方法的操作对象都是string.输入.输出和逻辑业务上很多时候都要用到string.list互转. 1.简单用法 import stringstr = 'abcde' ...

  2. 无界面运行Jmeter压测脚本 --后知者

    原文作者---后知者 原文地址:http://www.cnblogs.com/houzhizhe/p/8119735.html [后知者的故事]:针对单一接口压测时出现了从未遇到的问题,设好并发量后用 ...

  3. pyhanlp 停用词与用户自定义词典功能详解

    hanlp的词典模式 之前我们看了hanlp的词性标注,现在我们就要使用自定义词典与停用词功能了,首先关于HanLP的词性标注方式具体请看HanLP词性标注集. 其核心词典形式如下: 自定义词典 自定 ...

  4. cat命令详解

    命令cat cat 命令用于连接文件并打印到标准输出设备上 语法格式: cat [-AbeEnstTuv] [--help] [--version] fileName 参数说明: -n 或 --num ...

  5. c++ 函数中的部分代码执行一次

    编程时有时需要将一段代码中的某一块只执行一次: #include<iostream> using namespace std; int fun1(int a) { static bool ...

  6. 主流开源SQL(on Hadoop)总结

    转载至 大数据杂谈 (BigdataTina2016),同时参考学习 http://www.cnblogs.com/barrywxx/p/4257166.html 进行整理. 使用SQL 引擎一词是有 ...

  7. PHP代码实现3 [函数角度]

    内部函数 标准函数的实现存放在ext/standard扩展目录中. php函数种类 Zend engine中的function的存在形式 123456 struct _zend_execute_dat ...

  8. 关于java中Static关键字的加强理解

    static关键字是很多朋友在编写代码和阅读代码时碰到的比较难以理解的一个关键字,也是各大公司的面试官喜欢在面试时问到的知识点之一.下面就先讲述一下static关键字的用法和平常容易误解的地方,最后列 ...

  9. Hadoop概念学习系列之Hadoop新手学习指导之入门需知(二十)

    不多说,直接上干货! 零基础学习hadoop,没有想象的那么困难,也没有想象的那么容易.从一开始什么都不懂,到能够搭建集群,开发.整个过程,只要有Linux基础,虚拟机化和java基础,其实hadoo ...

  10. [转][C#]HttpClient 代码示例

    转自:https://www.cnblogs.com/amosli/p/3918538.html 也参考了:https://www.cnblogs.com/ShadowFiend007/p/80668 ...