美文网首页
textview设置只输入数字和小数点 并限制小数点后4位

textview设置只输入数字和小数点 并限制小数点后4位

作者: CoolKin | 来源:发表于2020-04-29 16:26 被阅读0次

如上标题问题

tvDesValue.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
                InputFilter lengthfilter = new InputFilter() {
                    public CharSequence filter(CharSequence source, int start, int end,
                                               Spanned dest, int dstart, int dend) {
                        // 删除等特殊字符,直接返回
                        if (".".equals(source.toString()) || "0".equals(source.toString())) {
                            if (dest.toString().length() == 0)
                            return "";
                        }
                        String dValue = dest.toString();
                        String[] splitArray = dValue.split("\\.");
                        if (splitArray.length > 1) {
                            String dotValue = splitArray[1];
                            int diff = dotValue.length() + 1 - 4;
                            if (diff > 0) {
                                return "";
                            }
                        }
                        return null;
                    }
                };
                tvDesValue.setFilters(new InputFilter[] { lengthfilter });

相关文章

网友评论

      本文标题:textview设置只输入数字和小数点 并限制小数点后4位

      本文链接:https://www.haomeiwen.com/subject/qshgwhtx.html