自定义EditText禁止输入表情
2016-04-27 15:27
267 查看
import android.content.Context; import android.text.Editable; import android.text.Selection; import android.text.Spannable; import android.text.TextWatcher; import android.util.AttributeSet; import android.widget.EditText; import android.widget.Toast; public class ContainsEmojiEditText extends EditText { //输入表情前的光标位置 private int cursorPos; //输入表情前EditText中的文本 private String inputAfterText; //是否重置了EditText的内容 private boolean resetText; private Context mContext; public ContainsEmojiEditText(Context context) { super(context); this.mContext = context; initEditText(); } public ContainsEmojiEditText(Context context, AttributeSet attrs) { super(context, attrs); this.mContext = context; initEditText(); } public ContainsEmojiEditText(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); this.mContext = context; initEditText(); } // 初始化edittext 控件 private void initEditText() { cursorPos = getSelectionEnd(); addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence s, int start, int before, int count) { if (!resetText) { cursorPos = getSelectionEnd(); //这里用s.toString()而不直接用s是因为如果用s, // 那么,inputAfterText和s在内存中指向的是同一个地址,s改变了, // inputAfterText也就改变了,那么表情过滤就失败了 inputAfterText = s.toString(); } } @Override public void onTextChanged(CharSequence s, int start, int before, int count) { if (!resetText) { if(before != 0){ return; } if (count >= 2) {//表情符号的字符长度最小为2 CharSequence input = s.subSequence(cursorPos, cursorPos + count ); if (containsEmoji(input.toString())) { resetText = true; Toast.makeText(mContext, "不支持输入Emoji表情符号", Toast.LENGTH_SHORT).show(); //是表情符号就将文本还原为输入表情符号之前的内容 setText(inputAfterText); CharSequence text = getText(); if (text instanceof Spannable) { Spannable spanText = (Spannable) text; Selection.setSelection(spanText, text.length()); } } } } else { resetText = false; } // cursorPos = getSelectionEnd(); } @Override public void afterTextChanged(Editable s) { } }); } /** * 检测是否有emoji表情 * * @param source * @return */ public static boolean containsEmoji(String source) { int len = source.length(); for (int i = 0; i < len; i++) { char codePoint = source.charAt(i); if (!isEmojiCharacter(codePoint)) { //如果不能匹配,则该字符是Emoji表情 return true; } } return false; } /** * 判断是否是Emoji * * @param codePoint 比较的单个字符 * @return */ 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)); } }转载自:http://www.cnblogs.com/guangliangchen/p/4864017.html
相关文章推荐
- c中函数指针的使用
- could not open hibernate session for transaction; nested exception is org.hibernate.exception.generi
- 机器学习之——归一化
- ACM刷题之ZOJ————Defuse the Bomb
- 利用Multipeer Connectivity框架进行WiFi传输
- WS架构:webapp数据交互与实时数据
- 2016-4-27知识整理
- 使用sed替换替换指定行的日期部分
- PHP 时间戳转化为日期格式
- UML类图
- ubuntu 解压命令集合
- PHPStorm8 常用快捷键
- JAVA设计模式之单例模式
- Oracle kill inactive 线程
- Tensorflow学习笔记(7)——CNN识别mnist编程实现
- 文档去重算法:SimHash和MinHash
- 搜索后再分页
- Exercise(8):单色汉诺塔
- ViewPager 详解(五)-----使用Fragment实现ViewPager滑动
- Java内存泄露原因详解