获取EditText组件
[java] view plaincopy
- EditText etWeiboContent = (EditText) findViewById(R.id.et_content);
将表情转换成文本
[java] view plaincopy
- EmoticonsUtil mEmoticons = new EmoticonsUtil(this);
- CharSequence emoticonsText = mEmoticons.replace(mEmoticons.getEmoticons()[position]);
获取光标所在位置
[java] view plaincopy
- int index = etWeiboContent.getSelectionStart();
获取EditText中原有的文本内容
[java] view plaincopy
- Editable editable = etWeiboContent.getEditableText();
在光标所在位置插入表情转换成文本
[java] view plaincopy
- if (index < 0 || index >= editable.length()) {
- editable.append(emoticonsText);
- } else {
- editable.insert(index, emoticonsText);
- }
完整的代码如下:
[java] view plaincopy
- EditText etWeiboContent = (EditText) findViewById(R.id.et_content);
- // 将表情转换成文本
- EmoticonsUtil mEmoticons = new EmoticonsUtil(this);
- CharSequence emoticonsText = mEmoticons.replace(mEmoticons.getEmoticons()[position]);
- // 获取光标所在位置
- int index = etWeiboContent.getSelectionStart();
- // 获取EditText中原有的文本内容
- Editable editable = etWeiboContent.getEditableText();
- // 在光标所在位置插入表情转换成文本
- if (index < 0 || index >= editable.length()) {
- editable.append(emoticonsText);
- } else {
- editable.insert(index, emoticonsText);
- }
有关将表情转换成文本的工具类源码如下:
[java] view plaincopy
- package com.easipass.sysframe.sc.home;
- import java.io.IOException;
- import java.io.InputStream;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- import android.content.Context;
- import android.content.res.AssetManager;
- import android.graphics.Bitmap;
- import android.graphics.BitmapFactory;
- import android.text.Spannable;
- import android.text.SpannableStringBuilder;
- import android.text.style.ImageSpan;
- /**
- * 功能描述:文字与表情图标的处理工具类
- * @author android_ls
- */
- public class EmoticonsUtil {
- private AssetManager assetManager;
- private String[] emoticons;
- public AssetManager getAssetManager() {
- return assetManager;
- }
- public String[] getEmoticons() {
- return emoticons;
- }
- public EmoticonsUtil(Context context) {
- try {
- assetManager = context.getResources().getAssets();
- emoticons = assetManager.list("emoticons");
- // LogUtil.d("emoticons = " + emoticons.length);
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- /**
- * 将文本中的表情符号替换为表情图片
- *
- * @param text
- * 需要转换的字符
- * @return 带有表情的字符
- */
- public CharSequence replace(String text) {
- // LogUtil.d("text = " + text);
- StringBuilder sb = new StringBuilder();
- sb.append(‘(‘);
- for (int i = 0; i < emoticons.length; i++) {
- sb.append(Pattern.quote(emoticons[i]));
- sb.append(‘|‘);
- }
- sb.replace(sb.length() - 1, sb.length(), ")");
- // LogUtil.d("sb = " + sb.toString());
- /* \Q 在non-word 字符前加上\,直到\E
- \E 结束\L,\E 和\Q*/
- // 竖线的转义:| ==> u007C
- // (\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)
- // 利用 \Q \E 使得正则表示式里的特殊字符全部 文字化了
- Pattern pattern = Pattern.compile(sb.toString());
- try {
- SpannableStringBuilder builder = new SpannableStringBuilder(text);
- Matcher matcher = pattern.matcher(text);
- while (matcher.find()) {
- // 返回匹配到的子字符串
- // LogUtil.d("matcher.group() = " + matcher.group());
- InputStream inStream = assetManager.open("emoticons/" + matcher.group());
- Bitmap bitmap = Bitmap.createScaledBitmap(BitmapFactory.decodeStream(inStream), 60, 60, true);
- ImageSpan span = new ImageSpan(bitmap);
- // 返回匹配到的子字符串在字符串中的索引位置.
- // LogUtil.d("matcher.start() = " + matcher.start());
- // 返回匹配到的子字符串的最后一个字符在字符串中的索引位置.
- // LogUtil.d("matcher.end() = " + matcher.end());
- builder.setSpan(span, matcher.start(), matcher.end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
- }
- return builder;
- } catch (Exception e) {
- return text;
- }
- /* 04-10 11:01:36.449: I/System.out(17881): text = face011.pngface016.pngface016.png
- 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)
- 04-10 11:01:36.459: I/System.out(17881): matcher.group() = face011.png
- 04-10 11:01:36.469: I/System.out(17881): matcher.start() = 0
- 04-10 11:01:36.469: I/System.out(17881): matcher.end() = 11
- 04-10 11:01:36.469: I/System.out(17881): matcher.group() = face016.png
- 04-10 11:01:36.479: I/System.out(17881): matcher.start() = 11
- 04-10 11:01:36.479: I/System.out(17881): matcher.end() = 22
- 04-10 11:01:36.479: I/System.out(17881): matcher.group() = face016.png
- 04-10 11:01:36.489: I/System.out(17881): matcher.start() = 22
- 04-10 11:01:36.489: I/System.out(17881): matcher.end() = 33*/
- }
- }
时间: 2024-10-19 06:05:33