import android.text.InputFilter; import android.text.Spanned; import android.util.Log; import com.example.otgdemo.utils.ToastUtil; import java.util.regex.Matcher; import java.util.regex.Pattern; public class InputFilterMinMax implements InputFilter { private float min, max; private int dot_num; private int plus_minus = 0; public InputFilterMinMax(float min, float max, int dot_num, int plus_minus) { this.min = min; this.max = max; this.dot_num = dot_num; this.plus_minus = plus_minus; } public InputFilterMinMax(String min, String max) { this.min = Float.valueOf(min); this.max = Float.valueOf(max); } @Override public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) { try { //限制小数点位数 if (source.equals(".") && dest.toString().length() == 0) { return "0."; } //有负数的 if (plus_minus == 1) { if (source.equals("-") && start == 0) { return "-"; } } //不让首位置出现00 这种情况 if (source.equals("0") && start == 0 && dest.toString().equals("0")) { return ""; } if (dest.toString().contains(".")) { int index = dest.toString().indexOf("."); int mlength = dest.toString().substring(index).length(); if (mlength == dot_num + 1) { return ""; } } // if (plus_minus == 1) { // String old = String.valueOf(dest); // String newString = String.valueOf(source); // String str = old + newString; // int num_str = Integer.parseInt(str); // if (num_str % 0.25 == 0) { // // }else { // return null; // } // // // } //限制大小 float input = Float.valueOf(dest.toString() + source.toString()); if (isInRange(min, max, input)) return null; } catch (Exception nfe) { } return ""; } private boolean isInRange(float a, float b, float c) { return b > a ? c >= a && c <= b : c >= b && c <= a; } }
//自定义edittext
import android.content.Context; import android.util.AttributeSet; import android.widget.EditText; import androidx.appcompat.widget.AppCompatEditText; /** * 只能在最后输入数据的输入框 * <pre> * author : Fantasy * version : 1.0, 2018-08-11 * since : 1.0, 2018-08-11 * </pre> */ public class LastInputEditText extends androidx.appcompat.widget.AppCompatEditText { public LastInputEditText(Context context) { super(context); } public LastInputEditText(Context context, AttributeSet attrs) { super(context, attrs); } public LastInputEditText(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @Override protected void onSelectionChanged(int selStart, int selEnd) { super.onSelectionChanged(selStart, selEnd); if (selStart == selEnd) { // 防止不能多选 setSelection(getText().length()); // 保证光标始终在最后面 } } }