如何用EditText中的图像替换String? [英] How to replace String with an image inside an EditText?
本文介绍了如何用EditText中的图像替换String?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我目前正在开发一个简单的聊天应用程序。我想在这个应用程序中添加表情符号功能。
我已经有了一个从其他用户接收字符串的函数。我想要的是 - 如果用户发送的文本包含CharSequence,如:D或:p,我希望将它们更改为表情符号。我该如何实现这个功能?
I am currently working on a simple chat application. I want to add emoticons features in this app. I already have a function to receive string from other users. What I want is- If the text that user sent contains CharSequence like ":D" or ":p", I want them to be changed into the emoticons. How can I implement this function?
我想要一个这样的函数:
I want a function like this:
public void updateMessage(){
if (receivedMessage.contains(":D")){
receivedMessage.replace(":D",image);
}
messageLog.append(receivedMessage);
}
如果可能,请帮助我。如果还有其他方法,请提及。
Please help me if this is possible. If there are other ways, please mention them.
推荐答案
你可以这样尝试
if (rosan.contains(":p")){
int span = abc.indexOf(":p");
res = getResources().getDrawable(R.drawable.myImage);
res.setBounds(0, 0, res.getIntrinsicWidth(), res.getIntrinsicHeight());
span = new ImageSpan(res, ImageSpan.ALIGN_BASELINE);
ss.setSpan(span, startSpan, startSpan+2, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
rosan = rosan.replaceFirst(":D"," "); //replace with two blank spaces.
}
这篇关于如何用EditText中的图像替换String?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文