1、如题,代码如下
mBinding.etContent.setInputType(EditorInfo.TYPE_CLASS_TEXT | EditorInfo.TYPE_TEXT_FLAG_MULTI_LINE
| EditorInfo.TYPE_TEXT_VARIATION_WEB_EMAIL_ADDRESS);
另附中英文过滤Filter
public abstract class EditFilterFactory {
abstract InputFilter createInputFilter(EditText editText, int maxWord, String notMatchToast);
public static class SampleEmptyFilter extends EditFilterFactory {
@Override
InputFilter createInputFilter(EditText editText, int maxWord, String notMatchToast) {
return new InputFilter() {
@Override
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
return source;
}
};
}
}
public static class SampleEnFilter extends EditFilterFactory {
@Override
InputFilter createInputFilter(EditText editText, int maxWord, String notMatchToast) {
return new InputFilter() {
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
if(TextUtils.isEmpty(source)){
return source;
}
if (maxWord > 0 && editText.getText().toString().split("[ !?.]").length >= maxWord) {
ToastUtils.show(String.format("仅支持%s字,已达上限", maxWord));
return "";
}
String regex = "[a-zA-Z0-9,.!?' ]+";
String unRegex = "[^a-zA-Z0-9,.!?' ]+";
if (source.toString().matches(regex)) {
return source;
} else {
if (!TextUtils.isEmpty(notMatchToast)) {
ToastUtils.show(notMatchToast);
}
return source.toString().replaceAll(unRegex, "");
}
}
};
}
}
public static class SampleCnFilter extends EditFilterFactory {
@Override
InputFilter createInputFilter(EditText editText, int maxWord, String notMatchToast) {
return new InputFilter() {
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
if(TextUtils.isEmpty(source)){
return source;
}
if (maxWord > 0 && editText.getText().toString().length() >= maxWord) {
ToastUtils.show(String.format("仅支持%s字,已达上限", maxWord));
return "";
}
String regex = "[\u4e00-\u9fa5,。!? ]+";
String unRegex = "[^\u4e00-\u9fa5,。!? ]";
if (source.toString().matches(regex)) {
return source;
} else {
if (!TextUtils.isEmpty(notMatchToast)) {
ToastUtils.show(notMatchToast);
}
return source.toString().replaceAll(unRegex, "");
}
}
};
}
}
}
网友评论