发布
社区首页 >问答首页 >EditText输入的DecimalFormat问题

EditText输入的DecimalFormat问题
EN

Stack Overflow用户
提问于 2018-08-20 00:06:25
回答 1查看 97关注 0票数 2

你好,我正在写这个程序,以获取用户输入和格式为0,000.00例如,当我尝试5,768.80时,程序运行良好,但当我尝试5,768.08时,它就不起作用了,因为你可以看到问题是我不能在任何其他数字之前把一个0放在美分位置……下面是我的代码:

代码语言:javascript
代码运行次数:0
复制
package com.calculadorabss.gorydev.calculadorabolivaressoberanos;

import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;

import java.text.DecimalFormat;
import java.text.ParseException;

class NumberTextWatcher implements TextWatcher {

    private DecimalFormat df;
    private DecimalFormat dfnd;
    private boolean hasFractionalPart;

    private EditText et;
    //Dar formato al texto de entrada separando por comas
    public NumberTextWatcher(EditText et)
    {
        df = new DecimalFormat("#,###,##");
        df.setDecimalSeparatorAlwaysShown(true);
        dfnd = new DecimalFormat("#,###,##");
        this.et = et;
        hasFractionalPart = false;
    }

    @SuppressWarnings("unused")
    private static final String TAG = "NumberTextWatcher";

    public void afterTextChanged(Editable s)
    {
        et.removeTextChangedListener(this);

        try {
            int inilen, endlen;
            inilen = et.getText().length();

            String v = s.toString().replace(String.valueOf(df.getDecimalFormatSymbols().getGroupingSeparator()), "");
            Number n = df.parse(v);
            int cp = et.getSelectionStart();
            if (hasFractionalPart) {
                et.setText(df.format(n));
            } else {
                et.setText(dfnd.format(n));
            }
            endlen = et.getText().length();
            int sel = (cp + (endlen - inilen));
            if (sel > 0 && sel <= et.getText().length()) {
                et.setSelection(sel);
            } else {
                // place cursor at the end?
                et.setSelection(et.getText().length() - 1);
            }
        } catch (NumberFormatException nfe) {
            // do nothing?
        } catch (ParseException e) {
            // do nothing?
        }

        et.addTextChangedListener(this);
    }
}

我希望程序能够收到0作为美分,例如67,789.05

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-08-20 00:29:50

试试这个,我想它行得通。

代码语言:javascript
代码运行次数:0
复制
public void afterTextChanged(Editable s)
    {
        et.removeTextChangedListener(this);

        try {
            int inilen, endlen;
            inilen = et.getText().length();

            String v = s.toString().replace(String.valueOf(df.getDecimalFormatSymbols().getGroupingSeparator()), "");
            hasFractionalPart = v.contains(".");
            Number n = df.parse(v);
            int cp = et.getSelectionStart();
            if (!hasFractionalPart) {
                et.setText(dfnd.format(n));
            }

            endlen = et.getText().length();
            int sel = (cp + (endlen - inilen));
            if (sel > 0 && sel <= et.getText().length()) {
                et.setSelection(sel);
            } else {
                // place cursor at the end?
                et.setSelection(et.getText().length() - 1);
            }
        } catch (NumberFormatException nfe) {
            // do nothing?
        } catch (ParseException e) {
            // do nothing?
        }

        et.addTextChangedListener(this);
    }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51919496

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档