负数输入滤波器

时间:2017-02-25 01:59:15

标签: android negative-number input-filter

我想将edittext输入限制为-99.99。但我的inputfilter阻止我进入" - "。你能帮助我检查一下它的错误吗?

private int min, max;

public InputFilterMinMax(int min, int max) {
    this.min = min;
    this.max = max;
}

public InputFilterMinMax(String min, String max) {
    this.min = Integer.parseInt(min);
    this.max = Integer.parseInt(max);
}

@Override
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
    try {
        double input = Double.valueOf(dest.toString() + source.toString());
        if (isInRange(min, max, input))
            return null;
    } catch (NumberFormatException nfe) { }
    return "";
}

private boolean isInRange(int a, int b, double c) {
    return b > a ? c >= a && c <= b : c >= b && c <= a;
}

mainactivity

text.setFilters(new InputFilter[]{new InputFilterMinMax("-99", "99")});

XML

android:inputType="numberDecimal|numberSigned"
        android:digits="0123456789+-."

谢谢:)

3 个答案:

答案 0 :(得分:0)

然后,您可以将数据类型更改为字符串以允许它,然后提取相关数字(具体取决于您想要的清晰编码)。

目前,它是双倍的...所以它不会消​​极。

答案 1 :(得分:0)

用下面的代码替换您的过滤器方法-

  @Override
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dStart, int dEnd) {
    try {
        // Remove the string out of destination that is to be replaced
        String newVal = dest.toString().substring(0, dStart) + dest.toString().substring(dEnd, dest.toString().length());
        newVal = newVal.substring(0, dStart) + source.toString() + newVal.substring(dStart, newVal.length());
        if (newVal.equals("-") || newVal.equals(".")){
            if(end == 0){
                return "";
            }
            return newVal;
        }
        double input = Double.parseDouble(newVal);

        if (isInRange(minValue, maxValue, input)) {
            return null;
        }
    } catch (NumberFormatException e) {
        e.printStackTrace();
    }
    return "";
}

答案 2 :(得分:0)

试试这个!

最小最大过滤器

 public class MinMaxFilter  implements InputFilter {
    private int min,max;
    Context context;

    //contructor
    public MinMaxFilter(int min, int max, Context context){
        this.min = min;
        this.max = max;
        this.context = context;
    }

    @Override
    public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dStart, int dEnd) {
        //code for filter input between -25 to 100
        //get current number
        String newValue = dest.subSequence(0,dStart)
                + source.subSequence(start,end).toString()
                + dest.subSequence(dEnd,dest.length());

        //need to define because prevent error in null exception in line 40
        int input = 0;

        try{
            //if first input is "-" = false
            //if dest variabel has 0 length = false
            if(!(dest.length() == 0) || !newValue.trim().equals("-")){
                input = Integer.parseInt(newValue);
            }

            //if input <= max and input >= min it will return true
            if(isInRange(min,max,input)){
                //if true keyboard can click
                return null;
            }
        }catch (NumberFormatException ex){
            ex.printStackTrace();
        }

        //if input doesn't complete requirement it will prevent keyboard input to edit text
        return "";
    }

    //if input <= max and input >= min it will return true

    private boolean isInRange(int min, int max, int input)
    {
        return input >= min && input <= max;
    }
}

使用 minmax 过滤器设置编辑文本的过滤器

//your edit text
    temperatureET = findViewById(R.id.editTextNumber);
    temperatureET.setFilters(new InputFilter[]{ new MinMaxFilter(-25,100,getApplicationContext())});

顺便说一句,这是我在 stackoverflow 中的第一个答案。所以如果这个答案很丑,我很抱歉。