如何将编辑文本中的文字和笑脸转为字符串?
使用以下代码,我在 Edittext 中添加笑脸/表情符号,但如何将文本/笑脸从 edittext 转换为字符串格式。
ImageGetter imageGetter = new ImageGetter() {
public Drawable getDrawable(String source) {
Drawable d = getResources().getDrawable(
R.drawable.happy);
d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
return d;
}
};
cs = Html.fromHtml(
"<img src='"
+ getResources()
.getDrawable(R.drawable.happy)
+ "'/>", imageGetter, null);
edttxtemoji.setText(cs);
最佳答案
请使用以下函数。
public static Spannable getSmiledText(Context context, String text) {
SpannableStringBuilder builder = new SpannableStringBuilder(text);
int index;for (index = 0; index < builder.length(); index++) {
for (Entry<String, Integer> entry : emoticons.entrySet()) {
int length = entry.getKey().length();
if (index + length > builder.length())
continue;
if (builder.subSequence(index, index + length).toString().equals(entry.getKey())) {
builder.setSpan(new ImageSpan(context, entry.getValue()), index, index + length,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
index += length - 1;
break;
}
}
}
return builder;
}
下面是情绪代码...
private static final HashMap<String, Integer> emoticons = new HashMap<String, Integer>();
static {
emoticons.put("8-)", R.drawable.s1);
emoticons.put(":-&", R.drawable.s2);
emoticons.put(">:-)", R.drawable.s3).....};
并使用 settext
tv_msg_send.setText(getSmiledText(getApplicationContext(), edt_msg.getText().toString()));
关于android - 如何将Edittext中的文字和笑脸转为字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7200310/