我正在做一个小项目,发送带有情感图标的消息。我使用以下功能将图像转换为spannble字符串以发送情感图标作为文本但问题是接收器无法在他的消息上看到情感图标而不是情感图标接收器见[h] ????
public SpannableStringBuilder addSmily(int position){
Drawable happySmileys = mContext.getResources().getDrawable(mThumbIds[position]);
happySmileys .setBounds(0, 0, happySmileys.getIntrinsicWidth(), happySmileys.getIntrinsicHeight());
/*Drawable sadSmiley = this.getResources().getDrawable(R.drawable.progress_4);
sadSmiley .setBounds(0, 0, sadSmiley.getIntrinsicWidth(), sadSmiley.getIntrinsicHeight());*/
SpannableStringBuilder builder = new SpannableStringBuilder();
builder.append("[h]");
builder.setSpan(new ImageSpan(happySmileys), builder.length()-"[h]".length(), builder.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
return builder;
}
感谢您阅读我的问题。任何建议?
答案 0 :(得分:1)
您要求做的是不支持所有设备,管理用户电话消息的应用程序不知道如何操作。如果你想让它工作,你需要编写自己的应用程序来阅读消息(顺便说一句,这并不难)。然后,当您向安装了应用程序的人发送消息时,您可以做任何事情......
答案 1 :(得分:0)
感谢Babibu的概念。经过一番研究,我找到了解决方案 这是我的实施方式。
我的应用程序发送消息,例如“我很高兴”为“我很高兴:-)”然后当我收到消息时,我检查模式:-)然后我将其替换为{{0} }。
这是我如何替换
public static CharSequence addSmileySpans(Context context, CharSequence your_recieved_message) {
HashMap<Integer, String> smilyRegexMap = new HashMap<Integer, String>();
smilyRegexMap.put(R.drawable.android_smile, ":-\\)");
SpannableStringBuilder builder = new SpannableStringBuilder(your_recieved_message);
@SuppressWarnings("rawtypes")
Iterator it = smilyRegexMap.entrySet().iterator();
while (it.hasNext()) {
@SuppressWarnings("rawtypes")
Map.Entry pairs = (Map.Entry) it.next();
Pattern mPattern = Pattern.compile((String) pairs.getValue(),
Pattern.CASE_INSENSITIVE);
Matcher matcher = mPattern.matcher(your_recieved_message);
while (matcher.find()) {
Bitmap smiley = BitmapFactory.decodeResource(
context.getResources(), ((Integer) pairs.getKey()));
builder.setSpan(new ImageSpan(smiley), matcher.start(),
matcher.end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
}
return builder;
}
然后在TextView上设置函数的返回值,如
messageTextView.setText(addSmileySpans(context,"i iam happy :-)"))