editText设置输入最大值(不是长度)

package com.myplas.q.common.view;

import android.text.InputFilter;
import android.text.Spanned;

/**
 * 编写: 黄双
 * 时间: 2017/6/121519.
 * 邮箱: [email protected]
 */

public class InputFilterMinMax implements 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 {
            int input = Integer.parseInt(dest.toString() + source.toString());
            if (isInRange(min, max, input))
                return null;
        } catch (Exception nfe) { }
        return "";
    }

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

用法:

mEditText = (EditText) view.findViewById(R.id.dialog_layout_modify_editText);     
mEditText.setFilters(new InputFilter[]{ new InputFilterMinMax("1", "12")});

你可能感兴趣的:(Android最大值)