EditTexit限制字符长度大家都用过通常做法分2种:一种是在xml里给EditTexit设置maxLength属性,一种是在代码中添加过滤器LengthFilter,其实这两种方式最终都是通过设置过滤器的方式实现的,其中计算文字长度都是按照每个字符占1位来计算了,下面我们修改计算方式,以中文计为2个字符,英文计为1个字符方式计算。
重写了EditText类如下:
package com.yiming.demo;
import android.content.Context;
import android.text.InputFilter;
import android.text.Spanned;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.EditText;
import java.lang.reflect.Field;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Created by Yiming on 2016/2/26.
* 对最长字符控制的计算方式进行修改(InputFilter.LengthFilter的计算方式)
* 中文和中文符号计为2个字符,英文、数字和英文符号计为1个字符。
*/
public class SuperEditTexit extends EditText {
public SuperEditTexit(Context context) {
super(context);
}
public SuperEditTexit(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public void setFilters(InputFilter[] filters) {
if(filters == null) {
return;
}
int len = filters.length;
if(len <= 0) {
return;
}
for (int i=len-1; i>=0; i--) {
InputFilter filter = filters[i];
if(filter instanceof InputFilter.LengthFilter) {
filters[i] = new SMLengthFilter(getMaxLength((InputFilter.LengthFilter) filter));
break;
}
}
super.setFilters(filters);
}
private int getMaxLength(InputFilter.LengthFilter filter) {
int max = 0;
Class clazz = (Class) filter.getClass();
try {
Field field = clazz.getDeclaredField("mMax");
field.setAccessible(true);
max = field.getInt(filter);
Log.d("getMaxLength", "max=" + max);
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
return max;
}
public static class SMLengthFilter extends InputFilter.LengthFilter {
int selfMax = 0;
public SMLengthFilter(int max) {
super(max);
selfMax = max;
}
@Override
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
// Log.d("debug", source + "/" + start + "/" + end + "/" + dest.toString() + "/" + dstart + "/" + dend);
int destLen = dend= getSequenceLength(dest);
int sourceLen = getSequenceLength(source);
int keep = selfMax - destLen;
if (keep <= 0) {
return "";
} else if (keep >= sourceLen) {
return null; // keep original
} else {
return subSequence(source, keep);
}
}
private CharSequence subSequence(CharSequence source, int keep) {
if(source == null || source.length()<=0) {
return "";
}
int length = 0;
char c;
for (int i=0; i<source.length(); i++) {
c = source.charAt(i);
if(isChinese(c)) {
length += 2;
} else {
length ++;
}
if(length > keep) {
if(i == 0) {
return "";
} else {
return source.subSequence(0, i);
}
}
}
return source;
}
private int getSequenceLength(CharSequence cs) {
int length = 0;
if(cs == null || cs.length() == 0) return length;
char c;
for(int i=0; i<cs.length(); i++) {
c = cs.charAt(i);
if(isChinese(c)) {
length += 2;
} else {
length ++;
}
}
return length;
}
private boolean isChinese(char c) {
Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);
if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS
|| ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS
|| ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A
|| ub == Character.UnicodeBlock.GENERAL_PUNCTUATION
|| ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION
|| ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS) {
return true;
}
return false;
}
/* private boolean isChinese(String s) {
Pattern p= Pattern.compile("[\u4e00-\u9fa5]");
Matcher m=p.matcher(s);
if(m.matches()){
return true;
} else {
return false;
}
}*/
}
}
使用时只需使用旧的办法在xml中设置maxLength属性:
<com.yiming.demo.SuperEditTexit
android:id="@+id/et"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_below="@id/tv"
android:maxLength="5"
/>
或在代码中添加过滤器LengthFilter:
EditText et = (EditText) findViewById(R.id.et);
et.setFilters(new InputFilter[]{new InputFilter.LengthFilter(5)});
2种方式都可以实现。很简单…