在Android EditText中无法在小数点后输入零

时间:2012-09-22 21:19:56

标签: android android-layout android-edittext

我有一个Android EditText,当用户输入数字时,它会编辑数字并使用十进制格式添加千位分隔符,但是当输入浮点数时,我不会在小数点后添加零。所以我不能输入1.000000008,因为零不会继续,但其他数字会这样做。 是否有任何java DecimalFormat模式允许用户在小数点后输入零? 这是我的EditText的代码。

am = new TextWatcher(){
                 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().contains(String.valueOf(df.getDecimalFormatSymbols().getDecimalSeparator())))
                        {
                            hasFractionalPart = true;
                        } else {
                            hasFractionalPart = false;
                        }
                 }
                public void afterTextChanged(Editable s) {
                    amount.removeTextChangedListener(this);
                    amount2.setText(s.toString());
                try {
                    int inilen, endlen;
                    inilen = amount.getText().length();

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

               }
            };

0 个答案:

没有答案