Android EditText字数限制:中文算2个字符,英文数字算1个字符,超出后弹Toast

最近有这个需求,在编辑框中输入字符,有长度的限制,中文算2个字符,英文算1个字符

1、中文、英文、数字都算一个字符:

/**
 * @author pengbo
 * @date 2018/11/12
 * 汉字、英文、数字都算一位,超出位数弹吐司
 */

public class MaxTextNormalLengthFilter implements InputFilter{
    private int mMaxLength;
    private Toast toast;

    public MaxTextNormalLengthFilter(Context context, int maxLen){
        mMaxLength = maxLen;
        toast = Toast.makeText(context, "限"+ maxLen + "个字符!)", Toast.LENGTH_SHORT);
        toast.setGravity(Gravity.CENTER, 0, 0);
    }

    @Override
    public CharSequence filter(CharSequence source, int start, int end,
                               Spanned dest, int dstart , int dend){
        // 判断是否到达最大长度
        int count = 0;
        // 之前就存在的内容
        int dindex = 0;
        while (count <= mMaxLength && dindex < dest.length()) {
            count++;
            dindex++;
        }
        if (count > mMaxLength) {
            toast.show();
            return dest.subSequence(0, dindex - 1);
        }
        // 从编辑框刚刚输入进去的内容
        int sindex = 0;
        while (count <= mMaxLength && sindex < source.length()) {
            count++;
            sindex++;
        }
        if (count > mMaxLength) {
            toast.show();
            sindex--;
        }
        return source.subSequence(0, sindex);
    }
}

2、中文算2个字符、英文和数字算1个字符:

/**
 * @author pengbo
 * @date 2018/11/8
 * 数字英文算一位,中文算两位,超出位数弹吐司
 */
public class MaxTextTwoLengthFilter implements InputFilter {

    private int mMaxLength;
    private Toast toast;

    public MaxTextTwoLengthFilter(Context context, int maxLen){
        mMaxLength = maxLen;
        toast = Toast.makeText(context, "限" + maxLen / 2 + "个汉字或" + maxLen + "个数字英文!)", Toast.LENGTH_SHORT);
        toast.setGravity(Gravity.CENTER, 0, 0);
    }

    @Override
    public CharSequence filter(CharSequence source, int start, int end,
                               Spanned dest, int dstart , int dend){
        // 判断是否到达最大长度
        int count = 0;
        // 之前就存在的内容
        int dindex = 0;
        while (count <= mMaxLength && dindex < dest.length()) {
            char c = dest.charAt(dindex++);
            if (c < 128) {
                count = count + 1;
            } else {
                count = count + 2;
            }
        }
        if (count > mMaxLength) {
            toast.show();
            return dest.subSequence(0, dindex - 1);
        }
        // 从编辑框刚刚输入进去的内容
        int sindex = 0;
        while (count <= mMaxLength && sindex < source.length()) {
            char c = source.charAt(sindex++);
            if (c < 128) {
                count = count + 1;
            } else {
                count = count + 2;
            }
        }
        if (count > mMaxLength) {
            toast.show();
            sindex--;
        }
        return source.subSequence(0, sindex);
    }
}

3、如果有那种输入大量篇幅的,肯定有一个字数的提示,或者在编辑框中输入了内容之后需要回调:

/**
 * @author pengbo
 * @date 2018/11/12
 * EditText添加文字限制的时候使用此TextWatcher,
 * 提供回调,有部分界面使用到判断
 */

public class MaxEditTextWatcher implements TextWatcher {
    /** 都算一位或 中文算两位 */
    public static final int ALL_ONE = 0;
    public static final int CHINESE_TWO = 1;
    private int mType;
    private int mMaxLen;
    private Context mContext;
    private EditText etText;
    private TextView tvByteLimit;
    private TextChangedCallBack mCallBack;

    public MaxEditTextWatcher(int type, int maxlen, Context context,  EditText editText) {
        this(type, maxlen, context, editText, null);
    }

    public MaxEditTextWatcher(int type, int maxLen, Context context, EditText editText, TextView textView) {
       this(type, maxLen, context, editText, textView, null);
    }

    public MaxEditTextWatcher(int type,int maxLen, Context context, EditText editText, TextView textView,
                              TextChangedCallBack callBack) {
        mType = type;
        mMaxLen = maxLen;
        mContext = context;
        etText = editText;
        tvByteLimit = textView;
        mCallBack = callBack;
        if (mType == ALL_ONE) {
            etText.setFilters(new InputFilter[]{new MaxTextNormalLengthFilter(mContext, mMaxLen)});
        } else if (mType == CHINESE_TWO){
            etText.setFilters(new InputFilter[]{new MaxTextTwoLengthFilter(mContext, mMaxLen)});
        }
    }

    @Override
    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

    }

    @Override
    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

    }

    @Override
    public void afterTextChanged(Editable editable) {
        if (tvByteLimit != null) {
            if (mType == ALL_ONE) {
                int length = etText.getText().length();
                tvByteLimit.setText(length + "/" + mMaxLen);
            } else if (mType == CHINESE_TWO) {
                int length = CommonUtil.judgeTextLength(etText.getText().toString());
                tvByteLimit.setText(length + "/" + mMaxLen);
            }
        }

        if (mCallBack != null) {
            mCallBack.changed(editable);
        }
    }

    public interface TextChangedCallBack {
        void changed(Editable editable);
    }
}

CommonUtil:

  /**
     * 返回text的长度,一个汉字算两个,数字和英文算一个
     * @param text
     * @return
     */
    public static int judgeTextLength(String text) {
        if (TextUtils.isEmpty(text)) {
            return 0;
        }
        int count = 0;
        for (int i = 0; i < text.length(); i++) {
            char item = text.charAt(i);
            if (item < 128) {
                count++;
            } else {
                count += 2;
            }
        }
        return count;
    }

猜你喜欢

转载自blog.csdn.net/pengbo6665631/article/details/84029417