Android EditText 里面屏蔽第三方输入法的表情
第三方的输入法的表情可能会给开发带来一定的麻烦,一般地像服务器上传的字符都是普通的字符,但是测试,就是狠狠得拿出搜狗输入法,点几个表情,
大问题来了,要么是显示异常,要么是直接crash,但是有很少的部分机型,会支持,可能是第三方的ROM对Android 的TextView 做了一定的处理,所以,我们只能干掉该死的表情了 具体的方法如下:
首先是在EditTextView 里面加入TextWatcher
在判断字符的变化时,就判断字符的类型,是否为字符,否则删除原来的字符,重新输入;
1 mEditTextView.addTextChangedListener(new TextWatcher() { 2 3 @Override 4 public void afterTextChanged(Editable editable) { 5 int index = contentText.getSelectionStart() - 1; 6 if (index > 0) { 7 if (isEmojiCharacter(editable.charAt(index))) { 8 Editable edit = contentText.getText(); 9 edit.delete(index, index + 1); 10 } 11 } 12 } 13 14 @Override 15 public void beforeTextChanged(CharSequence s, int start, int count, int after) { 16 17 } 18 19 @Override 20 public void onTextChanged(CharSequence s, int start, int before, int count) { 21 22 } 23 });
这里就是判断字符是否为表情;
1 private static boolean isEmojiCharacter(char codePoint) { 2 return !((codePoint == 0x0) || (codePoint == 0x9) || (codePoint == 0xA) || (codePoint == 0xD) || ((codePoint >= 0x20) && odePoint <= 0xD7FF))|| ((codePoint >= 0xE000) && (codePoint <= 0xFFFD)) || ((codePoint >= 0x10000) && (codePoint <= 0x10FFFF))); 3 }
文章来自:http://www.cnblogs.com/sssss/p/5029511.html