本文实例讲述了android同时控制EditText输入字符个数和禁止特殊字符输入的方法。分享给大家供大家参考。具体分析如下:
这里总结了三种方法如下:
方法一:
1. 引用两个命名空间:
import android.text.TextWatcher; import android.text.Editable;
用于禁止特殊字符输入控制
定义EditText mEditText对象
2. 控制字符长度:
通过InputFilter过滤来实现字符长度控制,这样的好处是可以实现动态长度控制,而不是一个长度定值
int mMaxLenth = 20; InputFilter[] FilterArray = new InputFilter[1]; FilterArray[0] = new InputFilter() { @Override public CharSequence filter (CharSequence source, int start, int end, Spanned dest, int dstart, int dend){ boolean bInvlid = false; int sourceLen = getCharacterNum(source.toString()); int destLen = getCharacterNum(dest.toString()); if (sourceLen + destLen > mMaxLenth) { return ""; } return source; } }; mEditText.setFilters(FilterArray);
3. 禁止特殊字符输入:
mEditText.addTextChangedListener(new TextWatcher() { String tmp = ""; String digits = "/\\:*&#63;<>|\"\n\t"; @Override public void onTextChanged(CharSequence s, int start, int before, int count) { mEditText.setSelection(s.length()); } @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { tmp = s.toString(); } @Override public void afterTextChanged(Editable s) { String str = s.toString(); if (str.equals(tmp)) { return; } StringBuffer sb = new StringBuffer(); for (int i = 0; i
方法二:
1. 添加引用:
import java.util.regex.*;
2. 定义函数:
public static String StringFilter(String str)throws PatternSyntaxException{ String regEx = "[/\\:*&#63;<>|\"\n\t]"; //要过滤掉的字符 Pattern p = Pattern.compile(regEx); Matcher m = p.matcher(str); return m.replaceAll("").trim(); }
3. 添加监听事件:
mEditText.addTextChangedListener(new TextWatcher() { @Override public void onTextChanged(CharSequence s, int start, int before, int count) { String t = mEditText.getText().toString(); String editable = mEditText.getText().toString(); String str = stringFilter(editable.toString()); if(!editable.equals(str)){ mEditText.setText(str); mEditText.setSelection(str.length()); //光标置后 } } @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { } @Override public void afterTextChanged(Editable s) { } });
方法三:
int mMaxLenth = 200;//设置允许输入的字符长度 public static String stringFilter(String str)throws PatternSyntaxException{ String regEx = "[/\\:*&#63;<>|\"\n\t]"; Pattern p = Pattern.compile(regEx); Matcher m = p.matcher(str); return m.replaceAll(""); } mEditText.addTextChangedListener(new TextWatcher() { private int cou = 0; int selectiOnEnd= 0; @Override public void onTextChanged(CharSequence s, int start, int before, int count) { cou = before + count; String editable = mEditText.getText().toString(); String str = stringFilter(editable); //过滤特殊字符 if (!editable.equals(str)) { mEditText.setText(str); } mEditText.setSelection(mEditText.length()); cou = mEditText.length(); } @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { } @Override public void afterTextChanged(Editable s) { if (cou > mMaxLenth) { selectiOnEnd= mEditText.getSelectionEnd(); s.delete(mMaxLenth, selectionEnd); } } });
以上代码都经过测试,给大家共享!对于第一,二,两种方法在三星NOTE 2中会有个问题(其他手机没有问题),并且输入窗口会出现跳动;具体原因,感兴趣的朋友可以调试一下!
第三种方法没有问题。
希望本文所述对大家的Android程序设计有所帮助。