如何限制numberdecimal EditText中point之前和之后的数字

时间:2016-10-22 21:01:52

标签: android filter android-edittext numbers digit

我的应用程序包含单个numberdecimal [0,1)。我想在点到1之前过滤数字。我想让用户在点后输入一位数。 示例:用户可以输入1,2.3,23,45.7,89.6,119.9,120.0等。 用户无法输入3.34,45.76,89.652,120.00,121.00等。 我怎么做那个过滤器?请在我的条款上写下符合EditText的过滤器

2 个答案:

答案 0 :(得分:1)

mEditText.setFilters(new InputFilter[]{new DigitsInputFilter(maxDigitsBeforeDot, maxDigitsAfterDot, maxValue)});

如果不希望限制点之前或之后的数字,只需将Integer.MAX_VALUE添加到禁用最大值限制,请使用Double.POSITIVE_INFINITY

public class DigitsInputFilter implements InputFilter {

    private final String DOT = ".";

    private int mMaxIntegerDigitsLength;
    private int mMaxDigitsAfterLength;
    private double mMax;


    public DigitsInputFilter(int maxDigitsBeforeDot, int maxDigitsAfterDot, double maxValue) {
        mMaxIntegerDigitsLength = maxDigitsBeforeDot;
        mMaxDigitsAfterLength = maxDigitsAfterDot;
        mMax = maxValue;
    }

    @Override
    public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
        String allText = getAllText(source, dest, dstart);
        String onlyDigitsText = getOnlyDigitsPart(allText);

        if (allText.isEmpty()) {
            return null;
        } else {
            double enteredValue;
            try {
                enteredValue = Double.parseDouble(onlyDigitsText);
            } catch (NumberFormatException e) {
                return "";
            }
            return checkMaxValueRule(enteredValue, onlyDigitsText);
        }
    }


    private CharSequence checkMaxValueRule(double enteredValue, String onlyDigitsText) {
        if (enteredValue > mMax) {
            return "";
        } else {
            return handleInputRules(onlyDigitsText);
        }
    }

    private CharSequence handleInputRules(String onlyDigitsText) {
        if (isDecimalDigit(onlyDigitsText)) {
            return checkRuleForDecimalDigits(onlyDigitsText);
        } else {
            return checkRuleForIntegerDigits(onlyDigitsText.length());
        }
    }

    private boolean isDecimalDigit(String onlyDigitsText) {
        return onlyDigitsText.contains(DOT);
    }

    private CharSequence checkRuleForDecimalDigits(String onlyDigitsPart) {
        String afterDotPart = onlyDigitsPart.substring(onlyDigitsPart.indexOf(DOT), onlyDigitsPart.length() - 1);
        if (afterDotPart.length() > mMaxDigitsAfterLength) {
            return "";
        }
        return null;
    }

    private CharSequence checkRuleForIntegerDigits(int allTextLength) {
        if (allTextLength > mMaxIntegerDigitsLength) {
            return "";
        }
        return null;
    }

    private String getOnlyDigitsPart(String text) {
        return text.replaceAll("[^0-9?!\\.]", "");
    }

    private String getAllText(CharSequence source, Spanned dest, int dstart) {
        String allText = "";
        if (!dest.toString().isEmpty()) {
            if (source.toString().isEmpty()) {
                allText = deleteCharAtIndex(dest, dstart);
            } else {
                allText = new StringBuilder(dest).insert(dstart, source).toString();
            }
        }
        return allText;
    }

    private String deleteCharAtIndex(Spanned dest, int dstart) {
        StringBuilder builder = new StringBuilder(dest);
        builder.deleteCharAt(dstart);
        return builder.toString();
    }
}

答案 1 :(得分:0)

您可以使用onTextChanged

TextWatcher方法实现逻辑
editText.addTextChangedListener(new TextWatcher() {

       @Override
       public void afterTextChanged(Editable s) {}

       @Override    
       public void beforeTextChanged(CharSequence s, int start,
         int count, int after) {
       }

       @Override    
       public void onTextChanged(CharSequence s, int start,

       }
      });

<强>更新 开始你可以使用这样的东西:

editText.addTextChangedListener(new TextWatcher() {

            boolean ignoreChange = false;

            @Override
            public void afterTextChanged(Editable s) {}

            @Override
            public void beforeTextChanged(CharSequence s, int start,
                                          int count, int after) {
            }

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

                if (!ignoreChange) {
                    String string = charSequence.toString();

                    String[] parts = string.split("\\.");
                    if (parts.length > 1) {
                        String digitsAfterPoint = parts[1];
                        if (digitsAfterPoint.length() > 1) {
                            ignoreChange = true;
                            editText.setText(string.substring(0, string.indexOf(".") + 2));
                            editText.setSelection(editText.getText().length());
                            ignoreChange = false;
                        }
                    }
                }

            }

        });

但你也应该控制“。”的重复。符号或其他东西。

更新#2 对于值您可以使用例如代码:

        editText.addTextChangedListener(new TextWatcher() {

            boolean ignoreChange = false;
            String beforeChange;

            @Override
            public void afterTextChanged(Editable s) {}

            @Override
            public void beforeTextChanged(CharSequence charSequence, int start,
                                          int count, int after) {
            }

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

                if (!ignoreChange) {
                    String string = charSequence.toString();

                    String[] parts = string.split("\\.");
                    if (parts.length > 1) {
                        String digitsAfterPoint = parts[1];
                        if (digitsAfterPoint.length() > 1) {
                            ignoreChange = true;
                            editText.setText(string.substring(0, string.indexOf(".") + 2));
                            editText.setSelection(editText.getText().length());
                            ignoreChange = false;
                        }
                    }

                    String strValue = editText.getText().toString();
                    float value = Float.valueOf(strValue);
                    if (value < 1 || value > 120) {
                        ignoreChange = true;
                        editText.setText(beforeChange);
                        editText.setSelection(editText.getText().length());
                        ignoreChange = false;
                    }
                    beforeChange = editText.getText().toString();
                }

            }

但你应该携带正确的值输入。并考虑每个数字NumberPickerSpinner