海燕啊,你可长点心吧,属耗子,落爪就忘,还是顺便多记记吧
1.自定义EditText
final EditText managerEditText = new EditText(Activity.this);
2.自定义EditText的自定义限制,还有很多自定义的内容,希望以后可以整理到一起,写一个通用类
1)接口形式,仿照网易云通信
public interface InputFilter { public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend); /** * This filter will capitalize all the lower case letters that are added * through edits. */ public static class AllCaps implements InputFilter { public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) { for (int i = start; i < end; i++) { if (Character.isLowerCase(source.charAt(i))) { char[] v = new char[end - start]; TextUtils.getChars(source, start, end, v, 0); String s = new String(v).toUpperCase(); if (source instanceof Spanned) { SpannableString sp = new SpannableString(s); TextUtils.copySpansFrom((Spanned) source, start, end, null, sp, 0); return sp; } else { return s; } } } return null; // keep original } } /** * This filter will constrain edits not to make the length of the text * greater than the specified length. */ public static class LengthFilter implements InputFilter { private final int mMax; public LengthFilter(int max) { mMax = max; } public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) { int keep = mMax - (dest.length() - (dend - dstart)); if (keep <= 0) { return ""; } else if (keep >= end - start) { return null; // keep original } else { keep += start; if (Character.isHighSurrogate(source.charAt(keep - 1))) { --keep; if (keep == start) { return ""; } } return source.subSequence(start, keep); } } /** * @return the maximum length enforced by this input filter */ public int getMax() { return mMax; } } }
调用方式:
EditText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(23)});
2)自定义类 重写InputFilter
https://blog.csdn.net/qq_35936174/article/details/93885088 这个博客地址
public class InputFilterMinMax implements InputFilter{
private float min, max;
public InputFilterMinMax(float min, float max) {
this.min = min;
this.max = max;
}
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 (dest.toString().contains(".")) {
int index = dest.toString().indexOf(".");
int mlength = dest.toString().substring(index).length();
if (mlength == 3) {
return "";
}
}
//限制大小
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;
}
}
添加监听
edit.setFilters(new InputFilter[]{new InputFilterMinMax(0,100)});
需要设置类型
两种方法:(XML布局)android:inputType="numberDecimal"
或者
edit.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL|InputType.TYPE_CLASS_NUMBER);