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.pngE|Qface002.pngE|Qface003.pngE|Qface004.pngE|Qface005.pngE|Qface006.pngE|Qface007.pngE|Qface008.pngE|Qface009.pngE|Qface011.pngE|Qface012.pngE|Qface016.pngE|Qface018.pngE|Qface019.pngE|Qface020.pngE|Qface025.pngE|Qface026.pngE|Qface032.pngE|Qface036.pngE|Qface037.pngE|Qface040.pngE|Qface054.pngE|Qface055.pngE|Qface058.pngE|Qface059.pngE|Qface060.pngE|Qface061.pngE|Qface062.pngE|Qface063.pngE|Qface071.pngE|Qface072.pngE|Qface073.pngE|Qface081.pngE|Qface083.pngE|Qface085.pngE|Qface090.pngE|Qface098.pngE|Qface099.pngE|Qface100.pngE|Qface101.pngE|Qface102.pngE|Qface103.pngE|Qface104.pngE|Qface105.pngE|Qface106.pngE|Qface114.pngE|Qface121.pngE|Qface191.pngE|Qface194.pngE|Qface196.pngE|Qface198.pngE|Qface201.pngE|Qface202.pngE|Qface205.pngE|Qface208.pngE|Qface217.pngE|Qface218.pngE|Qface219.pngE|Qface220.pngE|Qface221.pngE|Qface222.pngE|Qface223.pngE|Qface224.pngE|Qface225.pngE|Qface226.pngE|Qface227.pngE|Qface228.pngE|Qface229.pngE|Qface231.pngE|Qface233.pngE|Qface234.pngE|Qface238.pngE|Qface239.pngE|Qface242.pngE|Qface247.pngE|Qface248.pngE|Qface251.pngE|Qface252.pngE|Qface253.pngE|Qface254.pngE|Qface255.pngE|Qface257.pngE|Qface258.pngE|Qface259.pngE|Qface260.pngE|Qface261.pngE|Qface263.pngE|Qface264.pngE|Qface265.pngE|Qface266.pngE|Qface268.pngE|Qface270.pngE|Qface271.pngE|Qface273.pngE|Qface274.pngE|Qface277.pngE|Qface278.pngE|Qface279.pngE|Qface280.pngE|Qface281.pngE|Qface285.pngE|Qface287.pngE|Qface288.pngE|Qface290.pngE)  
  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.pngE|Qface002.pngE|Qface003.pngE|Qface004.pngE|Qface005.pngE|Qface006.pngE|Qface007.pngE|Qface008.pngE|Qface009.pngE|Qface011.pngE|Qface012.pngE|Qface016.pngE|Qface018.pngE|Qface019.pngE|Qface020.pngE|Qface025.pngE|Qface026.pngE|Qface032.pngE|Qface036.pngE|Qface037.pngE|Qface040.pngE|Qface054.pngE|Qface055.pngE|Qface058.pngE|Qface059.pngE|Qface060.pngE|Qface061.pngE|Qface062.pngE|Qface063.pngE|Qface071.pngE|Qface072.pngE|Qface073.pngE|Qface081.pngE|Qface083.pngE|Qface085.pngE|Qface090.pngE|Qface098.pngE|Qface099.pngE|Qface100.pngE|Qface101.pngE|Qface102.pngE|Qface103.pngE|Qface104.pngE|Qface105.pngE|Qface106.pngE|Qface114.pngE|Qface121.pngE|Qface191.pngE|Qface194.pngE|Qface196.pngE|Qface198.pngE|Qface201.pngE|Qface202.pngE|Qface205.pngE|Qface208.pngE|Qface217.pngE|Qface218.pngE|Qface219.pngE|Qface220.pngE|Qface221.pngE|Qface222.pngE|Qface223.pngE|Qface224.pngE|Qface225.pngE|Qface226.pngE|Qface227.pngE|Qface228.pngE|Qface229.pngE|Qface231.pngE|Qface233.pngE|Qface234.pngE|Qface238.pngE|Qface239.pngE|Qface242.pngE|Qface247.pngE|Qface248.pngE|Qface251.pngE|Qface252.pngE|Qface253.pngE|Qface254.pngE|Qface255.pngE|Qface257.pngE|Qface258.pngE|Qface259.pngE|Qface260.pngE|Qface261.pngE|Qface263.pngE|Qface264.pngE|Qface265.pngE|Qface266.pngE|Qface268.pngE|Qface270.pngE|Qface271.pngE|Qface273.pngE|Qface274.pngE|Qface277.pngE|Qface278.pngE|Qface279.pngE|Qface280.pngE|Qface281.pngE|Qface285.pngE|Qface287.pngE|Qface288.pngE|Qface290.pngE) 
  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. }