如何在android中添加表情符号聊天

时间:2014-03-18 09:52:10

标签: java android

我正在开发一个聊天应用程序,我想在其中添加笑脸支持。但我无法使用它。
我已经完成了许多代码,但符号并没有改变为表情符号 这是我的代码......

private static final Factory spannableFactory = Spannable.Factory
        .getInstance();

private static final Map<Pattern, Integer> emoticons = new HashMap<Pattern, Integer>();

static {
    addPattern(emoticons, ":)", R.drawable.emo_im_happy);
    addPattern(emoticons, ":-)", R.drawable.emo_im_happy);
    // ...
}

private static void addPattern(Map<Pattern, Integer> map, String smile,
        int resource) {
    map.put(Pattern.compile(Pattern.quote(smile)), resource);
}
    protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

    setContentView(R.layout.messaging_screen); // messaging_screen);

    messageHistoryText = (EditText) findViewById(R.id.messageHistory);

    messageText = (EditText) findViewById(R.id.message);

    messageText.requestFocus();

    sendMessageButton = (Button) findViewById(R.id.sendMessageButton);
}

public void appendToMessageHistory(String paramString1, String paramString2) {
        if ((paramString1 != null) && (paramString2 != null)) {
            if (paramString1.toString().equals(this.friend.userName.toString())) {
                appendColoredText(this.messageHistoryText,paramString1 + ":\n", Layout.Alignment.ALIGN_NORMAL,-16711936);
                appendColoredText(this.messageHistoryText, getSmiledText(this, paramString2) + "\n",    Layout.Alignment.ALIGN_NORMAL, -16711936);


            } else {
                appendColoredText(this.messageHistoryText, "you:\n",Layout.Alignment.ALIGN_OPPOSITE, -65536);
                appendColoredText(this.messageHistoryText, getSmiledText(this, paramString2) + "\n",Layout.Alignment.ALIGN_OPPOSITE, -65536);


            }
        }
    }

    public static boolean addSmiles(Context context, Spannable spannable) {
        boolean hasChanges = false;
        for (Entry<Pattern, Integer> entry : emoticons.entrySet()) {
            Matcher matcher = entry.getKey().matcher(spannable);
            while (matcher.find()) {
                boolean set = true;
                for (ImageSpan span : spannable.getSpans(matcher.start(),
                        matcher.end(), ImageSpan.class))
                    if (spannable.getSpanStart(span) >= matcher.start()
                            && spannable.getSpanEnd(span) <= matcher.end())
                        spannable.removeSpan(span);
                    else {
                        set = false;
                        break;
                    }
                if (set) {
                    hasChanges = true;
                    spannable.setSpan(new ImageSpan(context, entry.getValue()), matcher.start(), matcher.end(),Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                }
            }
        }
        return hasChanges;
    }


    public static Spannable getSmiledText(Context context, CharSequence text) {
        Spannable spannable = spannableFactory.newSpannable(text);
        addSmiles(context, spannable);
        return spannable;
    }


    public static void appendColoredText(EditText paramEditText,String paramString, Layout.Alignment paramAlignment, int paramInt) {
        int i = paramEditText.getText().length();
        paramEditText.append(paramString);
        int j = paramEditText.getText().length();
        Editable localEditable = paramEditText.getText();
        localEditable.setSpan(new ForegroundColorSpan(paramInt), i, j, 0);
        localEditable.setSpan(new AlignmentSpan.Standard(paramAlignment), i, j, 0);
        //localEditable.setSpan(new BackgroundColorSpan(back), i, j, 0);

    }

我在静态块中使用此代码,以便显示表情符号,但不显示它们。那么我该怎么办呢?

谢谢

0 个答案:

没有答案