首页 > 代码库 > android EditText 限制输入大小写,大写转小写
android EditText 限制输入大小写,大写转小写
在写登录验证的时候,需要将用户输入的大写字符转化为小写。看了下API,只有InputFilter.AllCaps是将EditText 的小写英文字符转化为大写。好吧。依赖AllCaps在写个类似的InputFilter吧。基本上Copy AllCaps的代码,只改了两行。大家看代码吧
import android.text.InputFilter;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextUtils;
//与InputFilter.AllCaps相反
public class LowInputFilter implements InputFilter {
@Override
public CharSequence filter(CharSequence source, int start, int end,
Spanned dest, int dstart, int dend) {
for (int i = start; i < end; i++) {
if (Character.isUpperCase(source.charAt(i))) {
char[] v = new char[end - start];
TextUtils.getChars(source, start, end, v, 0);
String s = new String(v).toLowerCase();
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
}
}