2

I am facing a problem here. I want to add images along with text in an EditText, but after lots of searching I reached a conclusion that I couldnt add images with text. UPTILL I got this piece of code that is supposed to be adding images to text in editText.

ImageSpan imageSpan = new ImageSpan(getResources().getDrawable(R.drawable.ic_action_emo_angry));
            SpannableStringBuilder builder = new SpannableStringBuilder();
            builder.append(input.getText());
            String imgId = "[img=1]"; 
            int selStart = input.getSelectionStart();

            // current selection is replaceв with imageId
            builder.replace(input.getSelectionStart(), input.getSelectionEnd(), imgId);
            builder.setSpan(imageSpan, selStart, selStart + imgId.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            input.setText(builder);

but when I tried it, it didn't work also. so Is there a way to do it? Thanks.

4

1 回答 1

1

我设法修复它

这是我解决问题的方法:

ImageGetter imageGetter = new ImageGetter() {
                    @Override
                    public Drawable getDrawable(String source) {
                        Drawable d = getResources().getDrawable(
                                cslistemoji.images[x]);
                        d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
                        return d;
                    } 

                };

                cs = Html.fromHtml(
                        "<img src='"
                                + getResources()
                                        .getDrawable(cslistemoji.images[pos])
                                + "'/>", imageGetter, null);
                System.out.println("cs is:- " + cs);
                String s = status.getText().toString();
                status.append(cs);
于 2013-04-27T15:15:11.673 回答