正文  UI设计 > 文本编辑/EditText >

android edittext 输入长度限制并提示

public static void lengthFilter(final Context context, final EditText editText, final int ...

 

public static void lengthFilter(final Context context, final EditText editText, final int max_length, final String err_msg) {

 

InputFilter[] filters = new InputFilter[1];

 

filters[0] = new InputFilter.LengthFilter(max_length) {

@Override

public CharSequence filter(CharSequence source, int start, int end,

Spanned dest, int dstart, int dend) {

int destLen = StringUtils.getCharacterNum(dest.toString()); //获取字符个数(一个中文算2个字符)

int sourceLen =  StringUtils.getCharacterNum(source.toString());

if (destLen + sourceLen > max_length) {

Toast.makeText(context, err_msg,Toast.LENGTH_SHORT).show();

return "";

}

return source;

}

};

editText.setFilters(filters);

}

 

 

 

附加方法:

 

 

/**

 * @description 获取一段字符串的字符个数(包含中英文,一个中文算2个字符)

 * @param content

 * @return

 */

public static int getCharacterNum(final String content) {

if (null == content || "".equals(content)) {

return 0;

}else {

return (content.length() + getChineseNum(content));

}

}

 

/**

* @description 返回字符串里中文字或者全角字符的个数

* @param s

* @return

*/

public static int getChineseNum(String s) {

 

int num = 0;

char[] myChar = s.toCharArray();

for (int i = 0; i < myChar.length; i++) {

if ((char)(byte)myChar[i] != myChar[i]) {

num++;

}

}

return num;

}

 

摘自 青峰的博客