一、实现EditText 输入限制公司需求需要实现输入框只能输入0-24的数字 首先在布局页面设置
> android:maxLength="2"
>
>android:inputType="number"
package com.mansion.smart_pad.utils;
import android.text.InputFilter;
import android.text.Spanned;
public class IPSectionFilter implements InputFilter {
@Override
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
String sourceText = source.toString();
String destText = dest.toString();
if (dstart == 0 && "0".equals(source)) {
return "";
}
StringBuilder totalText = new StringBuilder();
totalText.append(destText.substring(0, dstart))
.append(sourceText)
.append(destText.substring(dstart, destText.length()));
try {
if (Integer.parseInt(totalText.toString()) > 24) {
return "";
} else if (Integer.parseInt(totalText.toString()) == 0) {
return "";
}
} catch (Exception e) {
return "";
}
if ("".equals(source.toString())) {
return "";
}
return "" + Integer.parseInt(source.toString());
}
}
二、在这边直接调用
EditText editText = holder.getView(R.id.et_item_box);
editText.setFilters(new InputFilter[]{new IPSectionFilter()});
|