本文介绍了如何让文字和笑脸从EDITTEXT成字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何从文字的EditText和笑脸转换成String?
根据下列code我加入笑脸/ Emojis在EDITTEXT,但如何从文字的EditText /笑脸成字符串格式。
ImageGetter imageGetter =新ImageGetter(){
公众可绘制getDrawable(字符串源){
绘制对象D = getResources()。getDrawable(
R.drawable.happy);
d.setBounds(0,0,d.getIntrinsicWidth(),d.getIntrinsicHeight());
返回D组;
}
};
CS = Html.fromHtml(
&所述; IMG SRC ='
+ getResources()
.getDrawable(R.drawable.happy)
+'/>中,imageGetter,空);
edttxtemoji.setText(CS);
解决方案
请使用以下的功能。
公共静态Spannable getSmiledText(上下文的背景下,文本字符串){
SpannableStringBuilder建设者=新SpannableStringBuilder(文本);
INT指数;对于(指数= 0;指数< builder.length();指数++){
对于(进入<字符串,整数>输入:emoticons.entrySet()){
INT长度= entry.getKey()长度()。
如果(指数+长度GT; builder.length())
继续;
如果(builder.subSequence(索引,索引+长度)的ToString()。等于(entry.getKey())){
builder.setSpan(新ImageSpan(上下文,entry.getValue()),指数,指数+长度,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
指数+ =长度 - 1;
打破;
}
}
}
返回建设者;
}
下面的情绪code ...
私有静态最后的HashMap<字符串,整数>图释=新的HashMap<字符串,整数>();
静态{
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()));
How to Get text and smiley from edittext into String?
Using Following Code I was Add Smiley/Emojis in Edittext but how to get text/smiley from edittext into String Format.
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);
解决方案
Please use following function.
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;
}
following for emotions code...
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).....};
and settext using
tv_msg_send.setText(getSmiledText(getApplicationContext(), edt_msg.getText().toString()));
这篇关于如何让文字和笑脸从EDITTEXT成字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!