过滤输入法的表情

编程入门 行业动态 更新时间:2024-10-24 20:14:44

过滤<a href=https://www.elefans.com/category/jswz/34/1767193.html style=输入法的表情"/>

过滤输入法的表情

所以为了防止用户胡乱输入表情、同时限制用户只能输入应用自带的表情。编写了一个自定义控件来禁止输入的表情。

代码如下:


[java] view plain copy
  1. package com.qd.widget;  
  2.   
  3. import android.content.Context;  
  4. import android.text.InputFilter;  
  5. import android.text.SpannableString;  
  6. import android.text.Spanned;  
  7. import android.text.TextUtils;  
  8. import android.util.AttributeSet;  
  9. import android.widget.EditText;  
  10.   
  11. /** 
  12.  * 过滤搜狗输入法或其他输入法 当中的图片或其他非法字符 
  13.  *  
  14.  * 暂时仅过滤了部分常用的表情字符 
  15.  *  
  16.  * @author QD 
  17.  *  
  18.  */  
  19.   
  20. public class MyEditText extends EditText {  
  21.   
  22.     int maxLength = -1;  
  23.   
  24.     public MyEditText(Context context, AttributeSet attrs, int defStyle) {  
  25.         super(context, attrs, defStyle);  
  26.         addListener(attrs);  
  27.     }  
  28.   
  29.     public MyEditText(Context context, AttributeSet attrs) {  
  30.         super(context, attrs);  
  31.         addListener(attrs);  
  32.     }  
  33.   
  34.     public MyEditText(Context context) {  
  35.         super(context);  
  36.         addListener(null);  
  37.     }  
  38.   
  39.     private void addListener(AttributeSet attrs) {  
  40.         if (attrs != null)  
  41.             maxLength = attrs.getAttributeIntValue("", "maxLength", -1);  
  42.         // 过滤输入法表情  
  43.         InputFilter filter = new InputFilter() {  
  44.             @Override  
  45.             public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {  
  46.                 StringBuffer buffer = new StringBuffer();  
  47.                 for (int i = start; i < end; i++) {  
  48.                     char c = source.charAt(i);  
  49.                     // 第一个字符为以下时,过滤掉  
  50.                     if (c == 55356 || c == 55357 || c == 10060 || c == 9749 || c == 9917 || c == 10067 || c == 10024  
  51.                             || c == 11088 || c == 9889 || c == 9729 || c == 11093 || c == 9924) {  
  52.                         i++;  
  53.                         continue;  
  54.                     } else {  
  55.                         buffer.append(c);  
  56.                     }  
  57.                 }  
  58.                 if (source instanceof Spanned) {  
  59.                     SpannableString sp = new SpannableString(buffer);  
  60.                     TextUtils.copySpansFrom((Spanned) source, start, end, null, sp, 0);  
  61.                     return sp;  
  62.                 } else {  
  63.                     return buffer;  
  64.                 }  
  65.             }  
  66.         };  
  67.         // 输入框长度限制  
  68.         if (maxLength > 0)  
  69.             setFilters(new InputFilter[] { filter, new InputFilter.LengthFilter(maxLength) });  
  70.         else  
  71.             setFilters(new InputFilter[] { filter });  
  72.     }  

更多推荐

过滤输入法的表情

本文发布于:2024-02-12 23:15:24,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1689771.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:输入法   表情

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!