只支持输入汉字,字母、数字的编辑框,不支持输入特殊字符和空格的编辑框
汉字过滤:[\u4e00-\u9fa5]+
数字和字幕过滤:[a-zA-Z0-9 /]+
/**
* @Description 只支持输入汉字,字母、数字的编辑框,不支持输入特殊字符和空格
* @Author FTD
* @Date 2020/5/12
* @Version 1.0
*/
public class CustomEditText extends androidx.appcompat.widget.AppCompatEditText {
public CustomEditText(Context context) {
super(context);
}
public CustomEditText(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomEditText(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
return new mInputConnection(super.onCreateInputConnection(outAttrs), false);
}
class mInputConnection extends InputConnectionWrapper implements InputConnection {
public mInputConnection(InputConnection target, boolean mutable) {
super(target, mutable);
}
//拦截内容
@Override
public boolean commitText(CharSequence text, int newCursorPosition) {
// 只能输入汉字或者英文
if (!text.toString().matches("[\u4e00-\u9fa5]+") && !text.toString().matches("[a-zA-Z0-9 /]+")) {
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);
}
}
}
网友评论