EditText插入表情(字符串)到光标所在位置

获取EditText组件

[java] view plaincopy
 
  1. EditText etWeiboContent = (EditText) findViewById(R.id.et_content);  

将表情转换成文本

[java] view plaincopy
 
  1. EmoticonsUtil mEmoticons = new EmoticonsUtil(this);  
  2. CharSequence emoticonsText = mEmoticons.replace(mEmoticons.getEmoticons()[position]);  

获取光标所在位置

[java] view plaincopy
 
  1. int index = etWeiboContent.getSelectionStart();  

获取EditText中原有的文本内容

[java] view plaincopy
 
  1. Editable editable = etWeiboContent.getEditableText();  

在光标所在位置插入表情转换成文本

[java] view plaincopy
 
  1. if (index < 0 || index >= editable.length()) {  
  2.      editable.append(emoticonsText);  
  3.  } else {  
  4.      editable.insert(index, emoticonsText);  
  5.  }  

完整的代码如下:

[java] view plaincopy
 
  1. EditText etWeiboContent = (EditText) findViewById(R.id.et_content);  
  2.      
  3.    // 将表情转换成文本  
  4.    EmoticonsUtil mEmoticons = new EmoticonsUtil(this);  
  5.    CharSequence emoticonsText = mEmoticons.replace(mEmoticons.getEmoticons()[position]);  
  6.      
  7.    // 获取光标所在位置  
  8.    int index = etWeiboContent.getSelectionStart();  
  9.    // 获取EditText中原有的文本内容  
  10.    Editable editable = etWeiboContent.getEditableText();  
  11.    // 在光标所在位置插入表情转换成文本  
  12.    if (index < 0 || index >= editable.length()) {  
  13.        editable.append(emoticonsText);  
  14.    } else {  
  15.        editable.insert(index, emoticonsText);  
  16.    }  

有关将表情转换成文本的工具类源码如下:

[java] view plaincopy
 
  1. package com.easipass.sysframe.sc.home;  
  2.   
  3. import java.io.IOException;  
  4. import java.io.InputStream;  
  5. import java.util.regex.Matcher;  
  6. import java.util.regex.Pattern;  
  7.   
  8. import android.content.Context;  
  9. import android.content.res.AssetManager;  
  10. import android.graphics.Bitmap;  
  11. import android.graphics.BitmapFactory;  
  12. import android.text.Spannable;  
  13. import android.text.SpannableStringBuilder;  
  14. import android.text.style.ImageSpan;  
  15.   
  16. /** 
  17.  * 功能描述:文字与表情图标的处理工具类 
  18.  * @author android_ls 
  19.  */  
  20. public class EmoticonsUtil {  
  21.   
  22.     private AssetManager assetManager;  
  23.   
  24.     private String[] emoticons;  
  25.   
  26.     public AssetManager getAssetManager() {  
  27.         return assetManager;  
  28.     }  
  29.   
  30.     public String[] getEmoticons() {  
  31.         return emoticons;  
  32.     }  
  33.       
  34.     public EmoticonsUtil(Context context) {  
  35.         try {  
  36.             assetManager = context.getResources().getAssets();  
  37.             emoticons = assetManager.list("emoticons");  
  38.             // LogUtil.d("emoticons = " + emoticons.length);  
  39.         } catch (IOException e) {  
  40.             // TODO Auto-generated catch block  
  41.             e.printStackTrace();  
  42.         }  
  43.     }  
  44.   
  45.     /** 
  46.      * 将文本中的表情符号替换为表情图片 
  47.      *  
  48.      * @param text 
  49.      *            需要转换的字符 
  50.      * @return 带有表情的字符 
  51.      */  
  52.     public CharSequence replace(String text) {  
  53.   
  54.         // LogUtil.d("text = " + text);  
  55.   
  56.         StringBuilder sb = new StringBuilder();  
  57.         sb.append('(');  
  58.   
  59.         for (int i = 0; i < emoticons.length; i++) {  
  60.             sb.append(Pattern.quote(emoticons[i]));  
  61.             sb.append('|');  
  62.         }  
  63.   
  64.         sb.replace(sb.length() - 1, sb.length(), ")");  
  65.         // LogUtil.d("sb = " + sb.toString());  
  66.   
  67.         /* \Q    在non-word 字符前加上\,直到\E 
  68.          \E    结束\L,\E 和\Q*/  
  69.         // 竖线的转义:| ==> u007C  
  70.   
  71.         // (\Qface001.png\E|\Qface002.png\E|\Qface003.png\E|\Qface004.png\E|\Qface005.png\E|\Qface006.png\E|\Qface007.png\E|\Qface008.png\E|\Qface009.png\E|\Qface011.png\E|\Qface012.png\E|\Qface016.png\E|\Qface018.png\E|\Qface019.png\E|\Qface020.png\E|\Qface025.png\E|\Qface026.png\E|\Qface032.png\E|\Qface036.png\E|\Qface037.png\E|\Qface040.png\E|\Qface054.png\E|\Qface055.png\E|\Qface058.png\E|\Qface059.png\E|\Qface060.png\E|\Qface061.png\E|\Qface062.png\E|\Qface063.png\E|\Qface071.png\E|\Qface072.png\E|\Qface073.png\E|\Qface081.png\E|\Qface083.png\E|\Qface085.png\E|\Qface090.png\E|\Qface098.png\E|\Qface099.png\E|\Qface100.png\E|\Qface101.png\E|\Qface102.png\E|\Qface103.png\E|\Qface104.png\E|\Qface105.png\E|\Qface106.png\E|\Qface114.png\E|\Qface121.png\E|\Qface191.png\E|\Qface194.png\E|\Qface196.png\E|\Qface198.png\E|\Qface201.png\E|\Qface202.png\E|\Qface205.png\E|\Qface208.png\E|\Qface217.png\E|\Qface218.png\E|\Qface219.png\E|\Qface220.png\E|\Qface221.png\E|\Qface222.png\E|\Qface223.png\E|\Qface224.png\E|\Qface225.png\E|\Qface226.png\E|\Qface227.png\E|\Qface228.png\E|\Qface229.png\E|\Qface231.png\E|\Qface233.png\E|\Qface234.png\E|\Qface238.png\E|\Qface239.png\E|\Qface242.png\E|\Qface247.png\E|\Qface248.png\E|\Qface251.png\E|\Qface252.png\E|\Qface253.png\E|\Qface254.png\E|\Qface255.png\E|\Qface257.png\E|\Qface258.png\E|\Qface259.png\E|\Qface260.png\E|\Qface261.png\E|\Qface263.png\E|\Qface264.png\E|\Qface265.png\E|\Qface266.png\E|\Qface268.png\E|\Qface270.png\E|\Qface271.png\E|\Qface273.png\E|\Qface274.png\E|\Qface277.png\E|\Qface278.png\E|\Qface279.png\E|\Qface280.png\E|\Qface281.png\E|\Qface285.png\E|\Qface287.png\E|\Qface288.png\E|\Qface290.png\E)  
  72.   
  73.         // 利用 \Q \E 使得正则表示式里的特殊字符全部 文字化了   
  74.         Pattern pattern = Pattern.compile(sb.toString());  
  75.   
  76.         try {  
  77.             SpannableStringBuilder builder = new SpannableStringBuilder(text);  
  78.             Matcher matcher = pattern.matcher(text);  
  79.             while (matcher.find()) {  
  80.   
  81.                 // 返回匹配到的子字符串   
  82.                 // LogUtil.d("matcher.group() = " + matcher.group());  
  83.   
  84.                 InputStream inStream = assetManager.open("emoticons/" + matcher.group());  
  85.                 Bitmap bitmap = Bitmap.createScaledBitmap(BitmapFactory.decodeStream(inStream), 60, 60, true);  
  86.                 ImageSpan span = new ImageSpan(bitmap);  
  87.   
  88.                 // 返回匹配到的子字符串在字符串中的索引位置.  
  89.                 // LogUtil.d("matcher.start() = " + matcher.start());  
  90.                 // 返回匹配到的子字符串的最后一个字符在字符串中的索引位置.   
  91.                 // LogUtil.d("matcher.end() = " + matcher.end());  
  92.   
  93.                 builder.setSpan(span, matcher.start(), matcher.end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);  
  94.             }  
  95.             return builder;  
  96.         } catch (Exception e) {  
  97.             return text;  
  98.         }  
  99.   
  100.         /* 04-10 11:01:36.449: I/System.out(17881): text = face011.pngface016.pngface016.png 
  101.          04-10 11:01:36.449: I/System.out(17881): sb = (\Qface001.png\E|\Qface002.png\E|\Qface003.png\E|\Qface004.png\E|\Qface005.png\E|\Qface006.png\E|\Qface007.png\E|\Qface008.png\E|\Qface009.png\E|\Qface011.png\E|\Qface012.png\E|\Qface016.png\E|\Qface018.png\E|\Qface019.png\E|\Qface020.png\E|\Qface025.png\E|\Qface026.png\E|\Qface032.png\E|\Qface036.png\E|\Qface037.png\E|\Qface040.png\E|\Qface054.png\E|\Qface055.png\E|\Qface058.png\E|\Qface059.png\E|\Qface060.png\E|\Qface061.png\E|\Qface062.png\E|\Qface063.png\E|\Qface071.png\E|\Qface072.png\E|\Qface073.png\E|\Qface081.png\E|\Qface083.png\E|\Qface085.png\E|\Qface090.png\E|\Qface098.png\E|\Qface099.png\E|\Qface100.png\E|\Qface101.png\E|\Qface102.png\E|\Qface103.png\E|\Qface104.png\E|\Qface105.png\E|\Qface106.png\E|\Qface114.png\E|\Qface121.png\E|\Qface191.png\E|\Qface194.png\E|\Qface196.png\E|\Qface198.png\E|\Qface201.png\E|\Qface202.png\E|\Qface205.png\E|\Qface208.png\E|\Qface217.png\E|\Qface218.png\E|\Qface219.png\E|\Qface220.png\E|\Qface221.png\E|\Qface222.png\E|\Qface223.png\E|\Qface224.png\E|\Qface225.png\E|\Qface226.png\E|\Qface227.png\E|\Qface228.png\E|\Qface229.png\E|\Qface231.png\E|\Qface233.png\E|\Qface234.png\E|\Qface238.png\E|\Qface239.png\E|\Qface242.png\E|\Qface247.png\E|\Qface248.png\E|\Qface251.png\E|\Qface252.png\E|\Qface253.png\E|\Qface254.png\E|\Qface255.png\E|\Qface257.png\E|\Qface258.png\E|\Qface259.png\E|\Qface260.png\E|\Qface261.png\E|\Qface263.png\E|\Qface264.png\E|\Qface265.png\E|\Qface266.png\E|\Qface268.png\E|\Qface270.png\E|\Qface271.png\E|\Qface273.png\E|\Qface274.png\E|\Qface277.png\E|\Qface278.png\E|\Qface279.png\E|\Qface280.png\E|\Qface281.png\E|\Qface285.png\E|\Qface287.png\E|\Qface288.png\E|\Qface290.png\E) 
  102.          04-10 11:01:36.459: I/System.out(17881): matcher.group() = face011.png 
  103.          04-10 11:01:36.469: I/System.out(17881): matcher.start() = 0 
  104.          04-10 11:01:36.469: I/System.out(17881): matcher.end() = 11 
  105.          04-10 11:01:36.469: I/System.out(17881): matcher.group() = face016.png 
  106.          04-10 11:01:36.479: I/System.out(17881): matcher.start() = 11 
  107.          04-10 11:01:36.479: I/System.out(17881): matcher.end() = 22 
  108.          04-10 11:01:36.479: I/System.out(17881): matcher.group() = face016.png 
  109.          04-10 11:01:36.489: I/System.out(17881): matcher.start() = 22 
  110.          04-10 11:01:36.489: I/System.out(17881): matcher.end() = 33*/  
  111.     }  
  112.   
  113. }  

posted on 2015-12-21 15:29  taoboy  阅读(351)  评论(0编辑  收藏  举报

导航