1

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

EditText 包含一个格式化数字的文本观察器。文本文件如下:

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
4

3 回答 3

1

嘿检查这段代码。

android:inputType="numberDecimal"

希望这有帮助。

于 2016-12-30T11:43:21.287 回答
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) });
于 2016-12-30T12:19:00.410 回答
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);
    }
}
});
于 2016-12-30T12:25:56.433 回答