android – EditText随时显示2位小数的数字

我想随时显示两个小数的EditText字段的输入.所以当用户输入5时,会显示5.00或当用户输入7.5时显示7.50.

除此之外,我也希望在字段为空而不是空的时候显示为零.

我已经将inputtype设置为:

android:inputType="number|numberDecimal"/>

我应该在这里使用inputfilters吗?

对不起我还是很新的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 before,int count) {
            if(s.toString().matches(""))
            {
                et.setText("0.00");
                Selection.setSelection(et.getText(),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 after) {}

        public void onTextChanged(CharSequence s,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 如何解决dialog弹出时无法捕捉Activity的back事件 在...
Android实现自定义带文字和图片的Button 在Android开发中经常...
Android 关于长按back键退出应用程序的实现最近在做一个Andr...
android自带的时间选择器只能精确到分,但是对于某些应用要求...