//设置不支持输入表情符
ed_text.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
int index = ed_text.getSelectionStart() - 1;
if (index > 0) {
if (isEmojiCharacter(s.charAt(index))) {
Editable edit = ed_text.getText();
edit.delete(s.length() - 2, s.length());
Toast.makeText(MainActivity.this, "不支持输入表情符号",Toast.LENGTH_SHORT).show();
}
}
}
});
//设置
public static void setEditTextInhibitInputSpeChat(EditText editText){
InputFilter filter=new InputFilter() {
@Override
public CharSequence filter(CharSequence source, int start, int end,
Spanned dest, int dstart, int dend) {
// TODO Auto-generated method stub
// 判断特殊字符
String speChat="[`~!@#$%^&*()+=|{}':;',\\[\\].<>/?~!@#¥%……&*()——+|{}【】‘;:”“’。,、?]";
Pattern pattern = Pattern.compile(speChat);
Matcher matcher = pattern.matcher(source.toString());
// 字符长度 现在是限制20个字符
int sourceLen = getCharacterNum(source.toString());
int destLen = getCharacterNum(dest.toString());
if(matcher.find()||source.equals(" ")||sourceLen + destLen > 20){
return "";
}else{
return null;
}
}
};
editText.setFilters(new InputFilter[]{filter});
}
/**
* 获取一段字符串的字符个数(包含中英文,一个中文算2个字符)
*/
public static boolean isEmojiCharacter(char codePoint) {
return !((codePoint == 0x0) || (codePoint == 0x9) || (codePoint == 0xA) || (codePoint == 0xD) || ((codePoint >= 0x20) && codePoint <= 0xD7FF)) || ((codePoint >= 0xE000) && (codePoint <= 0xFFFD)) || ((codePoint >= 0x10000) && (codePoint <= 0x10FFFF));
}
/**
* 获取一段字符串的字符个数(包含中英文,一个中文算2个字符)
*/
public static int getCharacterNum(final String content) {
if (null == content || "".equals(content)) {
return 0;
}else {
return (content.length() + getChineseNum(content)); //默认一个中文2个字符,如果要设置成3个字符 return (content.length() + getChineseNum(content)*2);
}
}
/**
* 返回字符串里中文字或者全角字符的个数
*/
public static int getChineseNum(String s) {
int num = 0;
char[] myChar = s.toCharArray();
for (int i = 0; i < myChar.length; i++) {
if ((char)(byte)myChar[i] != myChar[i]) {
num++;
}
}
return num;
}