根據字符個數限制文字長短

在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;
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章