Edit输入money格式过滤

之前在网上也找了些别人写的过滤器,感觉有点问题,所以自己也写了一个,下面是代码,哪些不对的地方请指正谢谢

import android.text.InputFilter;
import android.text.Spanned;
import android.text.TextUtils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * edit输入金额过滤器
 */
public class EditInputFilter implements InputFilter {
    private final double mMaxMoney;
    private int DOT_LENGTH = 2;
    private static final String DOT = ".";
    private static final String ZERO = "0";
    private Pattern mPattern;

    /**
     * @param maxMoney  最大值
     * @param dotLength 小数点后保留位数
     */
    public EditInputFilter(double maxMoney, int dotLength) {
        mPattern = Pattern.compile("(\\d|\\.)*");
        mMaxMoney = maxMoney;
        DOT_LENGTH = dotLength;
    }

    @Override
    public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
        String sourceString = source.toString();
        String destString = dest.toString();
        int destLength = dest.length();

        if (TextUtils.isEmpty(source)) {
            //删除时保证小数点前一位不被删除
            int index = destString.indexOf(DOT);
            if (index == 1 && dend == 1) {
                return dest.subSequence(0, 1);
            } else {
                return "";
            }
        }
        Matcher matcher = mPattern.matcher(sourceString);
        if (!matcher.matches()) {
            return "";
        }
        //输入时不能以小数点开头
        if (dstart == 0 && DOT.equals(sourceString.subSequence(0, 1))) {
            return "";
        }
        //数据前面不能在前面插入0
        if (dstart == 0 && sourceString.contains(ZERO)) {
            if (TextUtils.isEmpty(dest)) {
                return sourceString;
            }
            int outsideIndex = getOutsideIndex(sourceString, ZERO.charAt(0));
            if (outsideIndex > 0) {
                return sourceString.substring(outsideIndex);
            } else if (outsideIndex == -1) {
                return "";
            }
        }
        //0后面只能输入"."
        if (ZERO.equals(destString) && !DOT.equals(sourceString.subSequence(0, 1))) {
            return "";
        }
        if (destString.contains(DOT)) {
            //包含小数点时不能再输小数点
            if (sourceString.contains(DOT)) {
                return "";
            }
            int index = destString.indexOf(DOT);
            int destDotLength = destLength - index - 1;
            if (dend - index > 0) {
                int length = destDotLength + sourceString.length();
                if (length > DOT_LENGTH) {
                    int endIndex = DOT_LENGTH - destDotLength;
                    if (endIndex < sourceString.length()) {
                        return sourceString.subSequence(0, endIndex);
                    } else {
                        return "";
                    }
                }
            }
        }
        //输入有小数点时
        if (sourceString.contains(DOT)) {
            //小数点不能打在后两位之前
            if (destLength - dend > 2) {
                return "";
            }
        }

        //判断是否超过最大值
        StringBuilder sb = new StringBuilder(destString);
        sb.insert(dstart, sourceString);
        double value = Double.parseDouble(sb.toString());
        if (mMaxMoney >= value) {
            return sourceString;
        } else {
            return "";
        }
    }

    /**
     * 找到第一个非该数值的索引,-1为全部为指定元素
     */
    private int getOutsideIndex(String str, char outside) {
        for (int i = 0; i < str.length(); i++) {
            if (str.charAt(i) != outside) {
                return i;
            }
        }
        return -1;
    }
}
配置如下

Edit.setInputType(EditorInfo.TYPE_CLASS_NUMBER|EditorInfo.TYPE_NUMBER_FLAG_DECIMAL);
Edit.setFilters(new InputFilter[]{new EditInputFilter(1999999999,2)});


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值