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

pat*_*ick 12 android decimal input-filtering android-edittext

我想一直显示带有两位小数的EditText字段的输入.因此,当用户输入5时它将显示5.00或当用户输入7.5时它将显示7.50.

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

我已经得到的输入类型设置为:

android:inputType="number|numberDecimal"/>
Run Code Online (Sandbox Code Playgroud)

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

对不起,我还是刚接触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);
            } 
        }
    });
Run Code Online (Sandbox Code Playgroud)

我还在为我的另一半问题解决问题.如果我找到了解决方案,我也会在这里发布.

编辑2011-07-09 23.35 - 解决了第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");
                }
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

nic*_*fox 17

这是我用来输入美元的东西.它确保始终只有小数点后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());

            }

        }
    });
Run Code Online (Sandbox Code Playgroud)

  • 我不需要$ symbole,我尝试两个删除cashAmountBuilder.insert(0,'$'); 但它强行关闭.有帮助吗? (3认同)