美文网首页
EditText 限制只保留两位小数

EditText 限制只保留两位小数

作者: 冬絮 | 来源:发表于2022-07-12 18:08 被阅读0次

最近在开发中遇到一个需求,在输入框中,只保留两位小数,多余的输入不显示.例如输入100.100 只显示100.10 后边的小数不在EditText中显示.
下边上代码

1. xml中的EditText中inputType设置为

<EditText
    android:id="@+id/et_test"
    android:inputType="number|numberDecimal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>

2. 获取EditText控件

    mEditText = findViewById(R.id.et_test);
    mEditText.setFilters(new InputFilter[]{new PointLengthFilter(2)});

3. PointLengthFilter

package com.hfy.nestedscrolling;

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


public class PointLengthFilter implements InputFilter {

    /**
     * 输入框小数的位数  示例保留一位小数
     */
    private int KEEP_SEVERAL_DECIMAL_PLACES = 1;

    public PointLengthFilter(int index) {
        this.KEEP_SEVERAL_DECIMAL_PLACES = index;
    }

    public CharSequence filter(CharSequence source, int start, int end,
                               Spanned dest, int dstart, int dend) {
        // 删除等特殊字符,直接返回
        if ("".equals(source.toString())) {
            return null;
        }
        String dValue = dest.toString();
        if(TextUtils.isEmpty(dValue)){
            //如果直接copy过来的数字 之前没有输入数字
            if (source.toString().contains(".")){
                int i = source.toString().indexOf(".");
                if (end > i + KEEP_SEVERAL_DECIMAL_PLACES + 1){
                    return source.subSequence(start, i + KEEP_SEVERAL_DECIMAL_PLACES + 1);
                }
            }
            return source;
        }
        String[] splitArray = dValue.split("\\.");
        if(splitArray != null && splitArray.length > 1){
            int cursorIndex = dValue.indexOf(".");
            if(dend > cursorIndex){
                String dotValue = splitArray[1];
                int diff = dotValue.length() + 1 - KEEP_SEVERAL_DECIMAL_PLACES;
                if(diff > 0){
                    return "";
                }
            }
        }
        //如果是粘贴过来的一串,判断长度,只保留小数点后KEEP_SEVERAL_DECIMAL_PLACES位
        if (end >= KEEP_SEVERAL_DECIMAL_PLACES && dValue.contains(".")){
            if (splitArray != null){
                if (splitArray.length == 1){
                    //小数点后边没有东西,保留KEEP_SEVERAL_DECIMAL_PLACES位
                    return source.subSequence(start, KEEP_SEVERAL_DECIMAL_PLACES);
                }else {
                    //小数点后边有东西
                    int i = KEEP_SEVERAL_DECIMAL_PLACES - splitArray[1].length();
                    if (i > 0){
                        //保留KEEP_SEVERAL_DECIMAL_PLACES - 已经存在的长度
                        return source.subSequence(start, i);
                    }else {
                        //成负数了,说明小数点后边的长度超过了KEEP_SEVERAL_DECIMAL_PLACES.
                        return "";
                    }
                }
            }
        }
        return source;

    }
}

相关文章

网友评论

      本文标题:EditText 限制只保留两位小数

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