欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

android 使用InputFilter 对金额和长度进行限制

程序员文章站 2022-04-06 08:21:48
...
public class MaxTextLengthFilter implements InputFilter {

    private int mMaxLength;
    private boolean isNum;
    private String note;
    private String message = null;

    /**
     *
     * @param max 支持的最大长度
     * @param isNum 是不是数字
     * @param note 超过后的提示
     */
    public MaxTextLengthFilter(int max, boolean isNum, String note) {
        mMaxLength = max;
        this.isNum = isNum;
        this.note = note;
    }

    public MaxTextLengthFilter setMessage(String message) {
        this.message = message;
        return this;
    }

    public CharSequence filter(CharSequence source, int start, int end,
                               Spanned dest, int dstart, int dend) {
        int keep = mMaxLength - (dest.length() - (dend - dstart));
        if (keep < (end - start)) {
            if (TextUtils.isEmpty(message))
                ToastUtils.showShortToast(String.format("%s最多%s位", note, mMaxLength));
            else {
                ToastUtils.showShortToast(message);
            }
        }
        if (keep <= 0) {
            return "";
        } else if (keep >= end - start) {
            if (isNum) {
                int posDot = dest.toString().indexOf(".");
                if (start < end && posDot > 0 && (dest.length() - posDot) > 2) {//小数点后要保留两位小数
                    ToastUtils.showShortToast("小数点后只能输两位");
                    return "";
                }
                if (start < end && posDot > 1 && dest.toString().equals("0.") && source.equals("0")) {//确保不会出现不是小数的第一位为0
                    ToastUtils.showShortToast("请输入正确格式的金额");
                    return "";
                } else if (start < end && posDot < 0 && dest.toString().equals("0") && !source.equals(".")) {
                    ToastUtils.showShortToast("请输入正确格式的金额");
                    return "";
                }
            } else
                return null;
        } else
            return source.subSequence(start, start + keep);
        return null;
    }
}