如何使用InputFilter限制Android中的EditText中的字符?我只想把字符限制在0-9,a-z,A-Z和空格。设置inputtype,我可以将其限制为数字,但我无法确定Inputfilter查看文档的方法。
3 回答
尚方宝剑之说
TA贡献1788条经验 获得超4个赞
InputFilter filter = new 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.isLetterOrDigit(source.charAt(i))) { return ""; } } return null; }};edit.setFilters(new InputFilter[] { filter });
MMTTMM
TA贡献1869条经验 获得超4个赞
source
new InputFilter() { @Override public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) { if (source instanceof SpannableStringBuilder) { SpannableStringBuilder sourceAsSpannableBuilder = (SpannableStringBuilder)source; for (int i = end - 1; i >= start; i--) { char currentChar = source.charAt(i); if (!Character.isLetterOrDigit(currentChar) && !Character.isSpaceChar(currentChar)) { sourceAsSpannableBuilder.delete(i, i+1); } } return source; } else { StringBuilder filteredStringBuilder = new StringBuilder(); for (int i = start; i < end; i++) { char currentChar = source.charAt(i); if (Character.isLetterOrDigit(currentChar) || Character.isSpaceChar(currentChar)) { filteredStringBuilder.append(currentChar); } } return filteredStringBuilder.toString(); } }}
牛魔王的故事
TA贡献1830条经验 获得超3个赞
<EditText android:inputType="text" android:digits="0,1,2,3,4,5,6,7,8,9,*,qwertzuiopasdfghjklyxcvbnm" />
- 3 回答
- 0 关注
- 785 浏览
添加回答
举报
0/150
提交
取消