android 发送屏蔽Emoji表情

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/c471961491/article/details/78652760

环境  cocos2dx-3.3  

输入组件使用  editorbox


修改文件  Cocos2dxHelper.java

文件路径  cocos2d-x\cocos\platform\android\java\src\org\cocos2dx\lib


final byte[] bytesUTF8 = pResult.getBytes("UTF8");  


改成
   
            String text = filterEmoji(pResult);  
            if (null == text || 0 == text.length()) {  
                  return;  
            }  
              
            final byte[] bytesUTF8 = text.getBytes("UTF8");


最后增加以下方法


// ==========================================================  
// Filter Emoji  
// ==========================================================  
private static boolean containsEmoji(String source) {  
if (null == source || 0 == source.length()) {  
return false;  
}  
  
int len = source.length();  
for (int i = 0; i < len; i++) {  
char codePoint = source.charAt(i);  
if (isEmojiCharacter(codePoint)) {  
return true;  
}  
}  
  
return false;  
}  
  
private 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)));  
}  
  
private static String filterEmoji(String source) {  
if (!containsEmoji(source)) {  
return source;// don't contain, just return  
}  
  
StringBuilder buf = null;  
int len = source.length();  
  
for (int i = 0; i < len; i++) {  
char codePoint = source.charAt(i);  
  
if (!isEmojiCharacter(codePoint)) {  
if (buf == null) {  
buf = new StringBuilder(source.length());  
}  
  
buf.append(codePoint);  
} else {  
}  
}  
  
if (buf == null) {  
return null;  
} else {  
if (buf.length() == len) {  
buf = null;  
return source;  
} else {  
return buf.toString();  
}  
}  



猜你喜欢

转载自blog.csdn.net/c471961491/article/details/78652760