栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Android Edtext 保留小数后几位

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Android Edtext 保留小数后几位

大家在开发过过程中可能会遇到 这样的需求 就是 只是需要保留 小数点后几位如图

这样的需求 虽然大家可以实现 但是 为这样一个简单的需求 耗费时间感觉跟不值得 在这里 小编 使用的是一个封装好了的 Edtext方便大家使用

这个是封装好的一个Edtext

public class KeepDecimalEditText extends AppCompatEditText {
    public int DECIMAL_DIGITS = 5;//可输入小数的位数
    public int defaultDecimal = 5;//显示小数的位数
    public boolean isChangeDecimal = false;//是否转换只输入整数
    public boolean isInput = false;//是否是输入的

    
    public interface ICallBack {
        void onInputEditText(String str);
    }

    
    ICallBack icallBack = null;

    
    public void setOnInputEditText(ICallBack iBack) {
        icallBack = iBack;
    }

    public KeepDecimalEditText(Context context) {
        super(context, null);
    }

    public KeepDecimalEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
        TypedArray typedArray = context.getTheme().obtainStyledAttributes(attrs, R.styleable.KeepDecimalEditText, 0, 0);
        typedArray.getIndexCount();
        typedArray.getIndex(0);
        DECIMAL_DIGITS = typedArray.getInteger(R.styleable.KeepDecimalEditText_decimal, 5);
        defaultDecimal = typedArray.getInteger(R.styleable.KeepDecimalEditText_decimal, 5);
        initOnTouchListener();
        initTextChangedListener();
    }


    public void setDECIMAL_DIGITS() {
        this.DECIMAL_DIGITS = defaultDecimal;
        isInput = false;
    }

    public void setDECIMAL_DIGITS(int DECIMAL_DIGITS) {
        this.DECIMAL_DIGITS = DECIMAL_DIGITS;
    }

    public int getDECIMAL_DIGITS() {
        return DECIMAL_DIGITS;
    }

    public void setChangeDecimal(boolean changeDecimal) {
        isChangeDecimal = changeDecimal;
    }

    private void initTextChangedListener() {
        addTextChangedListener(new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s.toString().contains(".")) {
                    if ((s.toString()+"a").split("\.").length > 2){//防止输入多个“.”
                        s = s.toString().substring(0,s.toString().length() -1);
                        setText(s);
                        setSelection(s.length());
                    }
                    if (s.length() - 1 - s.toString().indexOf(".") == 0 && DECIMAL_DIGITS == 0) {
                        s = s.toString().subSequence(0, s.toString().indexOf("."));
                        setText(s);
                        setSelection(s.length());
                        return;
                    }
                    if (s.length() - 1 - s.toString().indexOf(".") > DECIMAL_DIGITS) {
                        if (DECIMAL_DIGITS == 0) {
                            s = s.toString().subSequence(0,
                                    s.toString().indexOf(".") + DECIMAL_DIGITS);
                        } else {
                            s = s.toString().subSequence(0,
                                    s.toString().indexOf(".") + DECIMAL_DIGITS + 1);
                        }
                        setText(s);
                        setSelection(s.length());
                    }
                }
                if (s.toString().trim().equals(".")) {
                    s = "0" + s;
                    setText(s);
                    setSelection(s.toString().length());
                }
                if (s.toString().startsWith("0")
                        && s.toString().trim().length() > 1) {
                    if (DECIMAL_DIGITS == 0) {
                        setText(s.subSequence(1, s.toString().length()));
                        setSelection(1);
                        return;
                    }
//                    else if (!s.toString().substring(1).equals(".")) {//注释掉,否则无法输入0.*
//                        setText(s.subSequence(0, 1));
//                        setSelection(1);
//                        return;
//                    }
                }
                if (icallBack != null && isInput) {
                    icallBack.onInputEditText(s.toString());
                }
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    private void initOnTouchListener() {
        setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                isInput = true;
                if (isChangeDecimal) {
                    DECIMAL_DIGITS = 0;
                }else{
                    DECIMAL_DIGITS = defaultDecimal;
                }
                return false;
            }
        });
    }
}

然后在 Values中创建一个 attrs.xml文件



    
        
    

使用到的一些工具类

public class CalculateUtils {

    //保留五位小数
    public static String keepTwoDecimal(double data){
        return new DecimalFormat("#0.00").format(data);
    }

    //获取件的数量
    public static Double getPieceCount(Double currentCount, List packageConfigsBeen){
        int pieceUom = 1;
        for (PackageConfigsBean packageConfigsBean : packageConfigsBeen){
            if ("EA".equals(packageConfigsBean.getPackageCode())){
                pieceUom = packageConfigsBean.getContainerValue();
            }
        }
        return currentCount / pieceUom;
    }

    //字符串转换成double类型
    public static Double stringToDouble(String value){
        if (TextUtils.isEmpty(value)){
            return 0d;
        }
        return Double.valueOf(value);
    }

    public static String controlDecimal(String data){
        if (TextUtils.isEmpty(data)){
            return "";
        }
        if (data.contains(".")){

        }
        return data;
    }
}

使用方法 xml布局中
app:decimal=“2” 是保留的小数位数

      

Activity中的使用方法

        etCurrentIncome.setOnInputEditText(new KeepDecimalEditText.ICallBack() {
            @Override
            public void onInputEditText(String str) {
//                inputCurrentIncome = judgeNum(str, currentUom, asnResponseBean.getPackageConfigs());

                if (!TextUtils.isEmpty(etWeightOfGoods.getText().toString()) &&
                        !TextUtils.isEmpty(str)) {
                    //先转换为件的数量
                    Double pieceCount = CalculateUtils.stringToDouble(str);
                    if (Double.valueOf(etWeightOfGoods.getText().toString()) >= 0) {
                        etGoodsWeight.setText(CalculateUtils.keepTwoDecimal(pieceCount * Double.valueOf(etWeightOfGoods.getText().toString())));
                    }
                }
            }
        });
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/271265.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号