在textview中显示笑脸,在android中显示edittext

时间:2012-02-17 12:40:12

标签: android chat android-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);
System.out.println("cs is:- " + cs);
edttxtemoji.setText(cs);

我发现这个代码,在这里它使用图像,这是可行的吗? 还是有其他解决方案? 请提前为我提供更好的解决方案

3 个答案:

答案 0 :(得分:8)

是的还有另一种方式可以在TextViewEditText内展示笑脸。使用Spannable构建ImageSpan文本,然后将Spannable的文本设置为TextViewEditText。这是同一个post

答案 1 :(得分:1)

在edittext中设置笑脸

int value=R.id.ic_launcher;
Drawable Smiley = getResources().getDrawable(value);
Smiley.setBounds(0, 0, 15, 15);
SpannableStringBuilder builder = new SpannableStringBuilder();
String imgValue = "["+value+"]";
builder.append(imgValue);
builder.setSpan(new ImageSpan(Smiley), builder.length()-imgValue.length(), builder.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
edit_text.getText().insert(txt.getSelectionStart(), builder);

现在在listview或textview中获取笑脸

textview.setText(addSmileySpans(context,edit_text.getText()));
public CharSequence addSmileySpans(Context context, CharSequence msg) {

    SpannableStringBuilder builder = new SpannableStringBuilder(your_recieved_message);
    Pattern pattern = Pattern.compile("\\[([^\\[\\]]+)\\]");
    if( pattern != null )
    {
        Matcher matcher = pattern.matcher( your_recieved_message );
        int matchesSoFar = 0;
        while( matcher.find() )
        {
            CharSequence cs =matcher.group().subSequence(1, matcher.group().length()-1);
            int value = Integer.parseInt(cs.toString());
            System.out.println("pattern is::"+matcher.group().subSequence(1, matcher.group().length()-1)); 
            int start = matcher.start() - (matchesSoFar * 2);
            int end = matcher.end() - (matchesSoFar * 2);
            Drawable Smiley = context.getResources().getDrawable(value);
            Smiley.setBounds(0, 0,15,15);
            builder.setSpan(new ImageSpan(Smiley), start + 1, end - 1, 0 );
            builder.delete(start, start + 1);
            builder.delete(end - 2, end -1);
            matchesSoFar++;

        }
    }
    return builder;
}

答案 2 :(得分:1)

我觉得有点晚了。

public void addSmily() {
    CharSequence text = myEditText.getText();
    int resource  = R.drawable.ic_menu_emoticons ;
    myEditText.setText(getSpannableText(text,resource));
}

private Spannable getSpannableText(CharSequence text, int smilyToAppend) {
    Spannable spannable = Factory.getInstance().newSpannable(text+" ");
    ImageSpan smilySpan = new ImageSpan(getApplicationContext(),smilyToAppend);
    spannable.setSpan(smilySpan, spannable.length()-1, spannable.length(), 0);
    return spannable;
}