无法使用EditText addType =“number”Android添加小数

时间:2016-12-30 11:37:44

标签: android android-edittext android-textinputlayout android-textwatcher

inputType =“number”或inputType =“number | Decimal”时,点或逗号按钮被禁用。它无法与 android:digits =“0123456789。”,一起使用。

EditText包含一个格式化数字的textwatcher。文字如下:

mEditWithdrawalAmount.addTextChangedListener(new TextWatcher() {
    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {}

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {}

    @Override
    public void afterTextChanged(Editable s) {

        if (!s.toString().equals(current)) {
            mEditWithdrawalAmount.removeTextChangedListener(this);

            String replaceable = String.format("[%s .\\s]", NumberFormat.getCurrencyInstance().getCurrency().getSymbol());
            String cleanString = s.toString().replaceAll(replaceable, "").replace("R","").replace(",","");
            double parsed;
            try {
                parsed = Double.parseDouble(cleanString);
            } catch (NumberFormatException e) {
                parsed = 0.00;
            }

            String formatted = Utils.formatCurrency(parsed);

            current = formatted;
            mEditWithdrawalAmount.setText(formatted);
            mEditWithdrawalAmount.setSelection(formatted.length());

            // Do whatever you want with position
            mEditWithdrawalAmount.addTextChangedListener(this);
        }
    }
});

问题是edittext还必须允许带小数位的数字。

  • 实际结果为:R1000 000
  • 期望的结果是R1000 000.00或R1000 000.40

3 个答案:

答案 0 :(得分:0)

嘿,请检查此代码。

android:inputType="numberDecimal"

希望得到这个帮助。

答案 1 :(得分:0)

您可以使用这样的输入过滤器:

InputFilter filter = new InputFilter() {
        public CharSequence filter(CharSequence source, int start, int end,
                                   Spanned dest, int dstart, int dend) {
            if(source.equals("")){ // for backspace
                return source;
            }

            if(source.toString().matches("[0-9.]+")){
                return source;
            }
            return "";
        }
    };

然后将其设置为您的编辑文本

 topedittext.setFilters(new InputFilter[] { filter,new InputFilter.LengthFilter(30) });

答案 2 :(得分:0)

试试这个: 这是一个示例代码

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

            }

        }
    });

或者它适用于我

我已经在onFocusChangedListener中实现了所有内容。另外一定要将EditText输入类型设置为“number | numberDecimal”。

更改为:如果输入为空,则替换为“0.00”。如果输入的精度小数超过两位,则转换为两位小数。一些小的重构。

editText.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override public void onFocusChange(View v, boolean hasFocus) {
    if (!hasFocus) {
        String userInput = ET.getText().toString();

        if (TextUtils.isEmpty(userInput)) {
            userInput = "0.00";
        } else {
            float floatValue = Float.parseFloat(userInput);
            userInput = String.format("%.2f",floatValue);
        }

        editText.setText(userInput);
    }
}
});