package com.zhaohaoting.framework.utils;

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

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

/**
 * author：Created by ZHT on 2017/12/25 10:38
 * email：526309416@qq.com
 * desc：首位不为0
 */
public class FristNotInputFilter implements InputFilter {

    Pattern mPattern;

    public FristNotInputFilter() {
        mPattern = Pattern.compile("([0-9]|\\.)*");
    }


    @Override
    public CharSequence filter(CharSequence source, int i, int i1, Spanned dest, int i2, int i3) {

        String destText = dest.toString().trim();
        String sourceText = source.toString().trim();
        //验证删除等按键
        if (TextUtils.isEmpty(sourceText)) {
            return "";
        }
        Matcher matcher = mPattern.matcher(sourceText);
        if (!matcher.matches()) {
            return "";
        }
        if (destText.length() == 0 && sourceText.equals("0")) {
            return "";
        }
        return dest.subSequence(i2, i3) + sourceText;
    }
}
