限制小数位数

xiaoxiao2021-02-27  558

private static final int DECIMAL_DIGITS = 2; private InputFilter decimalFilter = new InputFilter() { @Override public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) { // source:当前输入的字符 // start:输入字符的开始位置 // end:输入字符的结束位置 // dest:当前已显示的内容 // dstart:当前光标开始位置 // dent:当前光标结束位置 if (dest.length() == 0 && source.equals(".")) { return "0."; } String dValue = dest.toString(); String[] splitArray = dValue.split("\\."); if (splitArray.length > 1) { String dotValue = splitArray[1]; if (dotValue.length() == DECIMAL_DIGITS) { return ""; } } return null; } }; mBonusMoney.setFilters(new InputFilter[] {decimalFilter,new InputFilter.LengthFilter(5) }); mBonusondition.setFilters(new InputFilter[] {decimalFilter,new InputFilter.LengthFilter(5) });
转载请注明原文地址: https://www.6miu.com/read-1275.html

最新回复(0)