显示的EditText在任何时候都与2位小数号码 [英] EditText showing numbers with 2 decimals at all times

查看:143
本文介绍了显示的EditText在任何时候都与2位小数号码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想有两个小数在任何时候都显示的EditText领域的投入。因此,当用户输入5,它会显示5​​.00或当用户输入7.5它将显示7.50

另外,我想也显示为零时,该字段为空,而不是什么都没有。

我的本钱已是inputtype设置为:

 安卓inputType =号| numberDecimal/>
 

我应该用inputfilters这里工作?

对不起,我还是很新至Android / Java的...

感谢您的帮助!

编辑2011-07-09 23.35 - 解决了部分1 2:在0.00

通过nickfox的答案,我能解决一半的我的问题。

  et.addTextChangedListener(新TextWatcher(){
        公共无效afterTextChanged(编辑S){}
        公共无效beforeTextChanged(CharSequence中,诠释开始,诠释计数,之后INT){}
        公共无效onTextChanged(CharSequence中,诠释开始,诠释之前,诠释计数){
            如果(s.toString()。匹配())
            {
                et.setText(0.00);
                Selection.setSelection(et.getText(),0,4);
            }
        }
    });
 

我仍然在为我的问题的另一半的解决方案。如果我找到了解决办法,我将它张贴在这里了。

编辑2011-07-09 23.35 - 解决了2第2部分:改变用户输入一个数字有两个小数

  OnFocusChangeListener FocusChanged =新OnFocusChangeListener(){

    @覆盖
    公共无效onFocusChange(视图V,布尔hasFocus){
        如果(!hasFocus){
            。字符串userInput = et.getText()的toString();

            INT dotPos = -1;

            的for(int i = 0; I< userInput.length();我++){
                焦炭C = userInput.charAt(我);
                如果(三=='。'){
                    dotPos =我;
                }
            }

            如果(dotPos == -1){
                et.setText(userInput +0.00);
            } 其他 {
                如果(userInput.length() -  dotPos == 1){
                    et.setText(userInput +00);
                }否则如果(userInput.length() -  dotPos == 2){
                    et.setText(userInput +0);
                }
            }
        }
    }
 

解决方案

下面是我用它来对美元投入。它确保只有2过去小数点位在任何时候。您应该能够以使其适应您的需求通​​过删除$符号。

  amountEditText.setRawInputType(Configuration.KEYBOARD_12KEY);
    amountEditText.addTextChangedListener(新TextWatcher(){
        公共无效afterTextChanged(编辑S){}
        公共无效beforeTextChanged(CharSequence中,诠释开始,诠释计数,之后INT){}

        公共无效onTextChanged(CharSequence中,诠释开始,诠释之前,诠释计数){
            如果(s.toString()的比赛(^ \\ $(\\Ð{1,3}(\\ \\ð{3})* |!。(\\ D +))(\\ ð{2})?$))
            {
                串userInput =+ s.toString()的replaceAll。([^ \\ D]。,);
                StringBuilder的cashAmountBuilder =新的StringBuilder(userInput);

                而(cashAmountBuilder.length()→3&安培;&安培; cashAmountBuilder.charAt(0)=='0'){
                    cashAmountBuilder.deleteCharAt(0);
                }
                而(cashAmountBuilder.length()3;){
                    cashAmountBuilder.insert(0,'0');
                }
                cashAmountBuilder.insert(cashAmountBuilder.length() -  2,'。');
                cashAmountBuilder.insert(0,'$');

                amountEditText.setText(cashAmountBuilder.toString());
                //保持光标总是向右
                Selection.setSelection(amountEditText.getText(),cashAmountBuilder.toString()长度());

            }

        }
    });
 

I would like to display the input of the EditText fields with two decimals at all times. So when the user enters 5 it will show 5.00 or when the user enters 7.5 it will show 7.50.

Besides that I would like to also show zero when the field is empty instead of nothing.

What I've got already is the inputtype set to:

android:inputType="number|numberDecimal"/>

Should I work with inputfilters here?

Sorry I still quite new to android / java...

Thanks for your help!

Edit 2011-07-09 23.35 - Solved part 1 of 2: The "" to 0.00.

With the answer of nickfox I was able to solve half of my question.

    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);
            } 
        }
    });

I'm still working on a solution for the other half of my question. If I found the solution I will post it here too.

Edit 2011-07-09 23.35 - Solved part 2 of 2: Change user input to a number with two decimals.

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");
                }
            }
        }
    }

解决方案

Here is something I use to for dollar input. It makes sure that there are only 2 places past the decimal point at all times. You should be able to adapt it to your needs by removing the $ sign.

    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());

            }

        }
    });

这篇关于显示的EditText在任何时候都与2位小数号码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆