因为表情传到后台解析数据失败,在移动端做个限制,限制表情及颜文字
原有的过滤条件不全,还是有颜文字不能被过滤,查了一下资料
增加条件:“[\u2190-\u21FF]|[\u2600-\u26FF]|[\u2700-\u27BF]|[\u3000-\u303F]|[\u1F300-\u1F64F]|[\u1F680-\u1F6FF]
”
资料地址:https://www.codenong.com/22178349/
增加条件:“[`!@#$%^&*()+=|{}‘:;’,\[\].<>/?!@#¥%……&*()——+|{}【】‘;:”“’。,、?]”
资料地址:https://blog.csdn.net/qq_35644925/article/details/113591221
//给editText设置过滤器
InputFilter inputFilter = new InputFilter() {
//限制输入表情
Pattern emoji = Pattern.compile("[\ud83c\udc00-\ud83c\udfff]|[\ud83d\udc00-\ud83d\udfff]|[\u2600-\u27ff]|[\u2190-\u21FF]|[\u2600-\u26FF]|[\u2700-\u27BF]|[\u3000-\u303F]|[\u1F300-\u1F64F]|[\u1F680-\u1F6FF]", Pattern.UNICODE_CASE | Pattern.CASE_INSENSITIVE);
//限制输入特殊字符以及颜文字
String speChat = "[`~!@#$%^&*()+=|{}':;',\\[\\].<>/?~!@#¥%……&*()——+|{}【】‘;:”“’。,、?]";
@Override
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
Matcher emojiMatcher = emoji.matcher(source);
if (emojiMatcher.find()) {
ToastUtils.showShorts(FinishPatrolActivity.this, "不支持输入表情");
return "";
}
Pattern pattern = Pattern.compile(speChat);
Matcher matcher = pattern.matcher(source.toString());
if (matcher.find()) {
ToastUtils.showShorts(FinishPatrolActivity.this, "不支持输入特殊符号");
return "";
}
return null;
}
};
使用方式
//给editText设置filter过滤器//添加LengthFilter限制最大输入字符数为50
editText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(50),EditTextInputFilterUtils.inputFilter});