在android中TextView的maxLength是指定最大的字个数,即中英文都算一个,但很多时候我们想让中文为2个字符,英文和其他符号则认为1个字符,那么就需要自己计算了,下面就是我写的一个工具类:
/**
* Created by star on 2016/10/17
* 功能:截取指定字符的字符串,中文两个字符,特殊符号为一个字符
*/
public class TextLimitUtil {
public static boolean isLetter(char c) {
int k = 0x80; //因为特殊符号的ASCII为0-127所以可以用这个来判断是否占一个字符
return c / k == 0;
}
/**
* 获取字符串的字符长度
*
* @param s
* @return
*/
public static int getLength(String s) {
if (Assert.isInvalidString(s)) {
return 0;
}
char[] c = s.toCharArray();
int len = 0;
for (int i = 0; i < c.length; i++) {
len++;
if (!isLetter(c[i])) {
len++;
}
}
return len;
}
/**
* 截取固定字符个数的字符串
*
* @param s
* @param len
* @return
*/
public static String subStringByLength(String s, int len) {
int length = getLength(s);
if (length > len) {
char[] c = s.toCharArray();
int curLen = 0;
for (int i = 0; i < c.length; i++) {
curLen++;
if (curLen == len) {
for (int j = 0; j < i; j++) {
//有时候会字重复,所以不能从0开始,得从当前位置的前一个位置开始
int start = 0;
if (i - j - 1 > 0) {
start = i - j - 1;
}
int t = s.indexOf(c[i], start);
if (t != -1) {
return s.substring(0, t + 1);
}
}
}
if (!isLetter(c[i])) {
curLen++;
if (curLen == len) {
for (int j = 0; j < i; j++) {
int start = 0;
if (i - j - 1 > 0) {
start = i - j - 1;
}
int t = s.indexOf(c[i], start);
if (t != -1) {
return s.substring(0, t + 1);
}
}
}
}
}
}
return s;
}
}
/**
* 判断对象非空与否工具类
*/
public class Assert{
/**
* 判断对象数组是否非空
* @param objCollect
* @return
*/
public static boolean isValidArray(Object[] objCollect) {
return null != objCollect && objCollect.length > 0;
}
/**
* 判断对象数组是否为空
* @param objCollect
* @return
*/
public static boolean isInvalidArray(Object[] objCollect) {
return !Assert.isValidArray(objCollect);
}
/**
* 判断字符串是否非空
* @param string
* @return
*/
public static boolean isValidString(String string) {
return null != string && string.trim().length() > 0;
}
/**
* 判断字符串是否为空
* @param string
* @return
*/
public static boolean isInvalidString(String string) {
return !Assert.isValidString(string);
}
/**
* 判断集合是否非空
* @param collection
* @return
*/
public static boolean isValidCollection(Collection<?> collection) {
return null != collection && collection.size() > 0;
}
/**
* 判断集合是否为空
* @param collection
* @return
*/
public static boolean isInvalidCollection(Collection<?> collection) {
return !isValidCollection(collection);
}
}
应用:1.用来显示用户名的时候,用户名过长,就可以适当截取
2.输入用户名或昵称的时候,限制输入的字符个数,代码如下
EditText editText =(EditText)findViewById(R.id.edit_text);
editText.setFilters(new InputFilter[]{new MyInputFilter(this, 24)});
/**
* Created by star on 2016/11/28
* 功能:
*/
public class MyInputFilter implements InputFilter {
private int max;
private Context context;
private boolean isHaveToast;
public MyInputFilter(Context context, int max) {
this.context = context;
this.max = max;
remain = max;
}
private int remain; //还能输入多少字
public int getRemain() {
return remain / 2;
}
@Override
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
int sourceLen = TextLimitUtil.getLength(source.toString()); //新输入的文本长度
int destLen = TextLimitUtil.getLength(dest.toString()); //原来的文本长度
LogUtils.d("dest", dest.toString());
LogUtils.d("destLen---->" + destLen);
LogUtils.d("source--->" + source.toString());
LogUtils.d("sourceLen--->" + sourceLen);
if (destLen >= max) {
//如果原来的文本长度就足够了,那么就不让输入了
if (!isHaveToast) {
ToastUtils.toastAbove(context, MyApplication.getToString(R.string.input_over));
isHaveToast = true;
}
remain = 0;
return "";
} else if (destLen + sourceLen > max) {
//如果原来的文本长度+新输入的文本长度超过,那么需要对新输入的进行截取
remain = 0;
int keep = max - destLen;
return TextLimitUtil.subStringByLength(source.toString(), keep);
}
remain = max - (destLen + sourceLen);
isHaveToast = false;
//如果没有超过,则返回新输入的
return source;
}
}