在 Android 开发中,经常需要限制 EditText 的输入字符,以下是几种不同的实现方法。
使用 InputFilter
通过实现 InputFilter 接口来限制输入。例如限制输入长度为 10。
InputFilter[] filters = new InputFilter[1];
filters[0] = new InputFilter.LengthFilter(10);
editText.setFilters(filters);
- 1.
- 2.
- 3.
正则判断是否输入的是中文:
editText.setFilters(new InputFilter[]{
new InputFilter() {
@Override
public CharSequence filter(CharSequence charSequence, int i, int i1, Spanned spanned, int i2, int i3) {
String regex = "^[\u4E00-\u9FA5]+$";
boolean isChinese = Pattern.matches(regex, charSequence.toString());
if (!Character.isLetterOrDigit(charSequence.charAt(i)) || isChinese) {
return "";
}
return null;
}
}
});
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
使用 TextWatcher
TextWatcher 可以用于监听文本变化,并在输入时实施限制。例如检查输入是否包含某些特定字符,并删掉不是字母或数字的字符。
editText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
String editable = evPwd.getText().toString();
String regEx = "[^a-zA-Z0-9]"; //只能输入字母或数字
Pattern p = Pattern.compile(regEx);
Matcher m = p.matcher(editable);
String str = m.replaceAll("").trim(); //删掉不是字母或数字的字符
if(!editable.equals(str)){
evPwd.setText(str); //设置EditText的字符
evPwd.setSelection(str.length()); //因为删除了字符,要重写设置新的光标所在位置
}
}
@Override
public void afterTextChanged(Editable s) {
}
});
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
使用 XML 属性
在 XML 中使用属性来限制 EditText 输入字符,如 android:inputType 和 android:digits。
只能输入数字:
<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number"
android:digits="0123456789" />
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
只能输入0~9 小写a~z:
<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text"
android:digits="0123456789abcdefghijklmnopqrstuvwxyz" />
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
自定义 EditText
对于更复杂的需求,可以通过自定义 EditText 控件实现输入限制。
public class LimitEditText extends EditText {
public LimitEditText(Context context) {
super(context);
}
public LimitEditText(Context context, AttributeSet attrs) {
super(context, attrs);
}
public LimitEditText(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
return new InnerInputConnecttion(super.onCreateInputConnection(outAttrs), false);
}
class InnerInputConnecttion extends InputConnectionWrapper implements InputConnection {
public mInputConnecttion(InputConnection target, boolean mutable) {
super(target, mutable);
}
/**
* 对输入的内容进行拦截
*
* @param text
* @param newCursorPosition
* @return
*/
@Override
public boolean commitText(CharSequence text, int newCursorPosition) {
// 只能输入字母或者数字
if (!Character.isLetterOrDigit(charSequence.charAt(i)) || isChinese) {
return false;
}
return super.commitText(text, newCursorPosition);
}
@Override
public boolean sendKeyEvent(KeyEvent event) {
return super.sendKeyEvent(event);
}
@Override
public boolean setSelection(int start, int end) {
return super.setSelection(start, end);
}
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
- 51.
注意事项
- 当使用 TextWatcher 时,要小心不要创建无限循环。例如,如果在 onTextChanged 方法中直接修改 EditText 的文本,并且没有适当的检查来防止不必要的修改,那么可能会导致无限循环。
- 对于复杂的输入限制,考虑使用正则表达式来匹配和过滤文本。可以提供更强大和灵活的文本处理能力。