使用Spannable上的文本视图显示表情符号视图、表情符号、文本、Spannable

2023-09-07 09:15:56 作者:污力满满

我需要证明我的表情符号的文本视图。

 私人的HashMap<字符串,整数>图释=新的HashMap<字符串,整数>();

emoticons.put(:-),R.drawable.f01);
emoticons.put(:P,R.drawable.f02);
emoticons.put(:D,R.drawable.f03);
 

现在假设我有一个的EditText ,我输入: - ),在它的点击一按钮,我想发送给的TextView 它会显示相应的资源。在这种情况下的f01

我如何做到这一点?

解决方案

  //试试这个
textview.setText(getSmiledText(text.toString()));

公共Spannable getSmiledText(字符串文本){
        SpannableStringBuilder建设者=新SpannableStringBuilder(文本);
        如果(emoticons.size()大于0){
            INT指数;
            对于(指数= 0;指数< builder.length();指数++){
                如果(Character.toString(builder.charAt(索引))等于(:)){
                    对于(Map.Entry的<字符串,整数>输入:emoticons.entrySet()){
                        INT长度= entry.getKey()长度()。
                        如果(指数+长度GT; builder.length())
                            继续;
                        如果(builder.subSequence(索引,索引+长度)的ToString()。等于(entry.getKey())){
                            builder.setSpan(新ImageSpan(的getContext(),entry.getValue()),指数,指数+长度,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                            指数+ =长度 -  1;
                            打破;
                        }
                    }
                }
            }
        }
        返回建设者;
    }
 

ssm框架实现的后台管理系统

I need to show my emoticon on a text view.

private HashMap<String, Integer> emoticons = new HashMap<String, Integer>();

emoticons.put(":-)", R.drawable.f01);
emoticons.put(":P", R.drawable.f02);
emoticons.put(":D", R.drawable.f03);

Now suppose I have a EditText and I am typing ":-)" in it on click on a button I want to send this to a TextView where it will show the corresponding resource. In this case f01.

How do I do this ?

解决方案

// try this
textview.setText(getSmiledText(text.toString()));

public Spannable getSmiledText(String text) {
        SpannableStringBuilder builder = new SpannableStringBuilder(text);
        if (emoticons.size() > 0) {
            int index;
            for (index = 0; index < builder.length(); index++) {
                if (Character.toString(builder.charAt(index)).equals(":")) {
                    for (Map.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(getContext(), entry.getValue()), index, index + length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                            index += length - 1;
                            break;
                        }
                    }
                }
            }
        }
        return builder;
    }