Java 如何在android中添加笑脸聊天

Java 如何在android中添加笑脸聊天,java,android,Java,Android,我正在开发一个聊天应用程序,我想在其中添加对笑脸的支持。但是我不能使用它。 我已经阅读了许多代码,但符号没有变为笑脸。 这是我的密码 private static final Factory spannableFactory = Spannable.Factory .getInstance(); private static final Map<Pattern, Integer> emoticons = new HashMap<Pattern, Integer

我正在开发一个聊天应用程序,我想在其中添加对笑脸的支持。但是我不能使用它。
我已经阅读了许多代码,但符号没有变为笑脸。
这是我的密码

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);

    }
私有静态最终工厂spannableFactory=Spannable.Factory
.getInstance();
private static final Map emoticons=new HashMap();
静止的{
添加图案(表情符号“:”,R.drawable.emo_im_happy);
addPattern(表情符号“:-”,R.drawable.emo_im_happy);
// ...
}
私有静态void addPattern(映射映射、字符串微笑、,
int资源){
map.put(Pattern.compile(Pattern.quote(smile)),resource);
}
创建时受保护的void(Bundle savedInstanceState){
//TODO自动生成的方法存根
super.onCreate(savedInstanceState);
setContentView(R.layout.messaging_屏幕);//messaging_屏幕);
messageHistoryText=(EditText)findViewById(R.id.messageHistory);
messageText=(EditText)findViewById(R.id.message);
messageText.requestFocus();
sendMessageButton=(按钮)findViewById(R.id.sendMessageButton);
}
public void appendToMessageHistory(字符串paramString1,字符串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,GetSmileText(this,paramString2)+“\n”,Layout.Alignment.ALIGN_NORMAL,-16711936);
}否则{
appendColoredText(this.messageHistoryText,“you:\n”,Layout.Alignment.ALIGN_相反,-65536);
appendColoredText(this.messageHistoryText,getSmileText(this,paramString2)+“\n”,Layout.Alignment.ALIGN_相反,-65536);
}
}
}
公共静态布尔addSmiles(上下文上下文,可扩展){
布尔hasChanges=false;
for(条目:emoticons.entrySet()){
Matcher Matcher=entry.getKey().Matcher(spannable);
while(matcher.find()){
布尔集合=真;
对于(ImageSpan:spannable.getspan)(matcher.start(),
matcher.end(),ImageSpan.class))
if(spannable.getSpanStart(span)>=matcher.start()

&&spannable.getSpanEnd(span)可能重复是的,但我无法在我的代码中使用它,因此请为我提供解决方案@karanthakkar,而不仅仅是添加我使用过的注释,它对我有效。如果你不想让你的文本自动更改:)所以我想你需要一些EditText上的监听器。是吗?或者当你调用getSmiledText时?