package com.inossem.smartstorage_pad.utils;
import android.icu.text.NumberFormat;
import android.os.Build;
import android.text.InputFilter;
import android.text.Spanned;
import android.text.TextUtils;
import com.inossem.smartstorage_pad.InossemApplication;
import com.inossem.smartstorage_pad.R;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* 數(shù)字輸入限制工具
*
* @author LinH
*/
public class EditInputFilterimplements InputFilter {
/**
? ? * 最大數(shù)字
? ? */
? ? private DoublemaxValue;
/**
? ? * 小數(shù)點(diǎn)后的數(shù)字的位數(shù)
? ? */
? ? private int pontInt;
private Patternpattern;
/**
? ? * 是否限制大小
? ? */
? ? private boolean isLimitTheSizeOfThe;
public EditInputFilter(Double maxValue, String unitCode) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
NumberFormat format = NumberFormat.getInstance();
format.setGroupingUsed(false);
if (maxValue !=null) {
format.format(maxValue);
}else {
format.format(9999999999.9999999999D);
}
}
this.pontInt = (int) Utils.getDecimalPlace(unitCode);
if (maxValue !=null) {
isLimitTheSizeOfThe =false;
this.maxValue = maxValue;
}else {
isLimitTheSizeOfThe =true;
this.maxValue =9999999999.9999999999D;
}
// 除數(shù)字外的其他的
? ? ? ? pattern = Pattern.compile("[0-9]*");
}
/**
? ? * @param src? ? 新輸入的字符串
? ? * @param start? 新輸入的字符串起始下標(biāo),一般為0
? ? * @param end? ? 新輸入的字符串終點(diǎn)下標(biāo),一般為source長(zhǎng)度-1
? ? * @param dest? 輸入之前文本框內(nèi)容
? ? * @param dstart 原內(nèi)容起始坐標(biāo),一般為0
? ? * @param dend? 原內(nèi)容終點(diǎn)坐標(biāo),一般為dest長(zhǎng)度-1
*/
? ? @Override
? ? public CharSequence filter(CharSequence src,int start,int end, Spanned dest,int dstart,int dend) {
// 全選
? ? ? ? // 選擇了多少位
? ? ? ? int destLength = dend - dstart;
// 是否全選
? ? ? ? if (dest.toString().length() == destLength) {
return null;
}
String oldtext = dest.toString();
// 驗(yàn)證刪除等按鍵
? ? ? ? if ("".equals(src.toString())) {
String[] ids = oldtext.split("");
// 如果刪除的是"."
? ? ? ? ? ? if (ids[dend].equals(".")) {
oldtext = oldtext.replace(".","");
Double splitDouble = Double.valueOf(oldtext);
if (splitDouble >this.maxValue) {
ToastUtils.showToast(InossemApplication.getContext().getString(R.string.input_module_maximum_number_of_inputs_cannot_be_greater_than) +maxValue);
return dest.subSequence(dstart, dend);
}else {
return null;
}
}else {
return null;
}
}
// 驗(yàn)證非數(shù)字或者小數(shù)點(diǎn)的情況
? ? ? ? Matcher m =pattern.matcher(src);
if (oldtext.contains(".")) {
// 已經(jīng)存在小數(shù)點(diǎn)的情況下,只能輸入數(shù)字
? ? ? ? ? ? if (!m.matches()) {
return null;
}
}else {
// 未輸入小數(shù)點(diǎn)的情況下,可以輸入小數(shù)點(diǎn)和數(shù)字
? ? ? ? ? ? if (!m.matches() && !".".contentEquals(src)) {
return null;
}
}
// 驗(yàn)證輸入金額的大小
? ? ? ? if (!"".equals(src.toString())) {
if (TextUtils.isEmpty(oldtext)) {
oldtext ="0";
}
double dold = Double.parseDouble(oldtext + src.toString());
if (dold >maxValue) {
if (isLimitTheSizeOfThe) {
ToastUtils.showToast(InossemApplication.getContext().getString(R.string.input_module_number_entered_cannot_exceed_10_digits));
return dest.subSequence(dstart, dend);
}else {
ToastUtils.showToast(InossemApplication.getContext().getString(R.string.input_module_maximum_number_of_inputs_cannot_be_greater_than) +maxValue);
return dest.subSequence(dstart, dend);
}
}else if (dold ==maxValue) {
if (isLimitTheSizeOfThe) {
ToastUtils.showToast(InossemApplication.getContext().getString(R.string.input_module_number_entered_cannot_exceed_10_digits));
return dest.subSequence(dstart, dend);
}
}
}
// 驗(yàn)證小數(shù)位精度是否正確
? ? ? ? if (oldtext.contains(".")) {
int index = oldtext.indexOf(".");
// 小數(shù)位只能2位
? ? ? ? ? ? int len = dend - index;
if (len >pontInt) {
ToastUtils.showToast(InossemApplication.getContext().getString(R.string.input_module_number_of_digits_after_the_decimal_point_is_zero) +pontInt);
return dest.subSequence(dstart, dend);
}
}
StringBuilder builder =new StringBuilder(dest);
builder.insert(dend, src);
String s = builder.toString();
String[] split = s.split("\\.");
if (split.length ==1) {
Double splitDouble;
try {
splitDouble = Double.valueOf(split[0]);
}catch (Exception e) {
splitDouble =0.0;
}
if (splitDouble >this.maxValue) {
ToastUtils.showToast(InossemApplication.getContext().getString(R.string.input_module_maximum_number_of_inputs_cannot_be_greater_than) +maxValue);
return dest.subSequence(dstart, dend);
}
}else if (split.length ==2) {
double splitInt = Double.valueOf(s);
if (splitInt >this.maxValue) {
ToastUtils.showToast(InossemApplication.getContext().getString(R.string.input_module_maximum_number_of_inputs_cannot_be_greater_than_100000000000));
return dest.subSequence(dstart, dend);
}
String splitS = String.valueOf(split[1]);
if (splitS.length() >this.pontInt) {
ToastUtils.showToast(InossemApplication.getContext().getString(R.string.input_module_maximum_number_of_inputs_cannot_be_greater_than_100000000000));
return dest.subSequence(dstart, dend);
}
}
return dest.subSequence(dstart, dend) + src.toString();
}
}