由于编码原因 服务器端是不能识别大部分表情的 但是在手机端 用搜狗 或者百度输入法 是可以输入表情的 但是我们有不希望用户输入表情
不是当这些表情传入服务器端的时候 服务器会报错 这就需要我们android过滤了
下面是我写的EditText 输入过滤的
public class Test {
private EditText feed_advise;
//输入表情前EditText中的文本
private String tmp;
//是否重置了EditText的内容
private boolean resetText;
private TextWatcher watcher = new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if(!resetText && start<s.length()) {<span style="color:#808080;">//s<str.length()判断用户是否是删除了输入的数据</span>
char codePoint = s.charAt(start);
if(isEmojiCharacter(codePoint)){
resetText = true;
//是表情符号就将文本还原为输入表情符号之前的内容
feed_advise.setText(tmp);
feed_advise.setSelection(tmp.length());//设置EditText光标
<span style="background-color:#344134;">Toast</span>.makeText(Test .<span style="color:#cc7832;">this,</span><span style="color:#6a8759;">"不支持表情输入"</span><span style="color:#cc7832;">,</span><span style="background-color:#344134;">Toast</span>.<span style="color:#9876aa;font-style:italic;">LENGTH_SHORT</span>)<span style="color:#cc7832;">;</span>
}
}else{
resetText = false;
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
if(!resetText || s.toString().length()<=start) {<span style="color:#808080;">//s.toString().length()<=start 判断用户是否是删除了输入的数据</span>
tmp = s.toString();//存储这次输入后的文本 好在现场输入表情后还原文本
}
}
@Override
public void afterTextChanged(Editable s) {
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_feed_back);
feed_advise= (EditText) findViewById(R.id.feed_advise);//获取EditText控件
feed_advise.addTextChangedListener(watcher);//设置判断
}
/**
* 是否包含表情
*
* @param codePoint
* @return 如果不包含 返回false,包含 则返回true
*/
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)));
}
}
下面是一个用于过滤一个字符串的表情的代码 下面代码的思路是 用户输入完全部在去判断用户是否输入表情 如果有 就把表情去掉
public class EmojiFilter {
/**
* 是否包含表情
*
* @param codePoint
* @return 如果不包含 返回false,包含 则返回true
*/
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)));
}
/**
* 过滤emoji 或者 其他非文字类型的字符
*
* @param source
* @return
*/
public static String filterEmoji(String source) {
StringBuilder buf = null;
int len = source.length();
for (int i = 0; i < len; i++) {
char codePoint = source.charAt(i);
if (!isEmojiCharacter(codePoint)) {// 如果不包含 则将字符append
if (buf == null) {
buf = new StringBuilder(source.length());
}
buf.append(codePoint);
} else {
}
}
if (buf == null) {
return source;// 如果没有找到 emoji表情,则返回源字符串
} else {
if (buf.length() == len) {// 这里的意义在于尽可能少的toString,因为会重新生成字符串
buf = null;
return source;
} else {
return buf.toString();
}
}
}
}
本文有一部分引用于 http://download.csdn.net/detail/ycxyyzw/8550353 demo代码