android - EditText 始终显示带 2 位小数的数字

标签 android decimal android-edittext input-filtering

我想始终以两位小数显示 EditText 字段的输入。因此,当用户输入 5 时,它将显示 5.00,或者当用户输入 7.5 时,它将显示 7.50。

除此之外,我还想在字段为空时显示零,而不是什么都没有。

我已经将输入类型设置为:

android:inputType="number|numberDecimal"/>

我应该在这里使用输入过滤器吗?

抱歉,我对 android/java 还是很陌生...

感谢您的帮助!

编辑 2011-07-09 23.35 - 已解决第 1 部分(共 2 部分):“”变为 0.00。

有了 nickfox 的回答,我的问题解决了一半。

    et.addTextChangedListener(new TextWatcher() {
        public void afterTextChanged(Editable s) {}
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            if(s.toString().matches(""))
            {
                et.setText("0.00");
                Selection.setSelection(et.getText(), 0, 4);
            } 
        }
    });

我仍在为我的另一半问题寻找解决方案。如果我找到了解决方案,我也会将其发布在这里。

编辑 2011-07-09 23.35 - 已解决第 2 部分(共 2 部分):将用户输入更改为带两位小数的数字。

OnFocusChangeListener FocusChanged = new OnFocusChangeListener() {

    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        if(!hasFocus){
            String userInput = et.getText().toString();

            int dotPos = -1;    

            for (int i = 0; i < userInput.length(); i++) {
                char c = userInput.charAt(i);
                if (c == '.') {
                    dotPos = i;
                }
            }

            if (dotPos == -1){
                et.setText(userInput + ".00");
            } else {
                if ( userInput.length() - dotPos == 1 ) {
                    et.setText(userInput + "00");
                } else if ( userInput.length() - dotPos == 2 ) {
                    et.setText(userInput + "0");
                }
            }
        }
    }

最佳答案

这是我用来输入美元的东西。它确保始终只有小数点后 2 位。您应该能够通过删除 $ 符号来适应您的需要。

    amountEditText.setRawInputType(Configuration.KEYBOARD_12KEY);
    amountEditText.addTextChangedListener(new TextWatcher() {
        public void afterTextChanged(Editable s) {}
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {}

        public void onTextChanged(CharSequence s, int start, int before, int count) {
            if(!s.toString().matches("^\\$(\\d{1,3}(\\,\\d{3})*|(\\d+))(\\.\\d{2})?$"))
            {
                String userInput= ""+s.toString().replaceAll("[^\\d]", "");
                StringBuilder cashAmountBuilder = new StringBuilder(userInput);

                while (cashAmountBuilder.length() > 3 && cashAmountBuilder.charAt(0) == '0') {
                    cashAmountBuilder.deleteCharAt(0);
                }
                while (cashAmountBuilder.length() < 3) {
                    cashAmountBuilder.insert(0, '0');
                }
                cashAmountBuilder.insert(cashAmountBuilder.length()-2, '.');
                cashAmountBuilder.insert(0, '$');

                amountEditText.setText(cashAmountBuilder.toString());
                // keeps the cursor always to the right
                Selection.setSelection(amountEditText.getText(), cashAmountBuilder.toString().length());

            }

        }
    });

关于android - EditText 始终显示带 2 位小数的数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6636444/

相关文章:

android - 无法使用 android ndk 使图像变灰

android - 如何知道listview何时完成在android上加载数据

android - 我可以在代码中返回编辑文本的 id 字符串吗?

android - 单击 EditText 时检查空格

android - 编辑文本 "lag"android

android - 如何显示累积推送通知而不是显示往往同时出现的多个通知?

android - 高于 2dp 的高度会导致按钮消失

c - C语言中的位与

带有双 0 的 Ruby float

C中十进制转二进制