其他分享
首页 > 其他分享> > 在android中处理SpannableStringBuilder图像

在android中处理SpannableStringBuilder图像

作者:互联网

我正在开发一个应用程序,其中选定的联系人添加到EditText与aclose标记图像,当我点击该关闭标记图像时,应删除该联系人.我已完成代码显示关闭标记图像,但我不知道如何处理那些关闭标记图像.请建议我如何.

我的代码:

for (int i = 0; i < selectedItems.size(); i++) {
                    String na = selectedItems.get(i);
                    TextView tv = createContactTextView(na);
                    BitmapDrawable bd = (BitmapDrawable) convertViewToDrawable(tv);
                    bd.setBounds(0, 0, bd.getIntrinsicWidth(),
                            bd.getIntrinsicHeight());
                    sb.append(na + ",");
                    sb.setSpan(new ImageSpan(bd), sb.length()
                            - (na.length() + 1), sb.length() - 1,
                            Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                }

                txt.setText(sb);

private Object convertViewToDrawable(TextView view) {
        int spec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
        view.measure(spec, spec);
        view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
        Bitmap b = Bitmap.createBitmap(view.getMeasuredWidth(),
                view.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
        Canvas c = new Canvas(b);
        c.translate(-view.getScrollX(), -view.getScrollY());
        view.draw(c);
        view.setDrawingCacheEnabled(true);
        Bitmap cacheBmp = view.getDrawingCache();
        Bitmap viewBmp = cacheBmp.copy(Bitmap.Config.ARGB_8888, true);
        view.destroyDrawingCache();
        return new BitmapDrawable(viewBmp);
    }

    private TextView createContactTextView(String text) {
        TextView tv = new TextView(this);
        tv.setText(text);
        tv.setTextSize(25);
        tv.setBackgroundResource(R.drawable.oval_small);
        tv.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.close, 0);
        return tv;
    }

解决方法:

ClickableSpan就是你想要的:

for (int i = 0; i < selectedItems.size(); i++) {
     String na = selectedItems.get(i);
     TextView tv = createContactTextView(na);
     BitmapDrawable bd = (BitmapDrawable) convertViewToDrawable(tv);
     bd.setBounds(0, 0, bd.getIntrinsicWidth(),bd.getIntrinsicHeight());
     sb.append(na + ",");
     sb.setSpan(new ImageSpan(bd), sb.length()
          - (na.length() + 1), sb.length() - 1,
          Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

     final int index = i;  
     sb.setSpan(new ClickableSpan() {
                @Override
                public void onClick(View widget) {
                    // here add your code
                    // delete your selectedItems[index]
                    // recreate your SpannedString and set to txt
                }
            }, sb.length()
                    - (na.length() + 1), sb.length() - 1,
                    Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
 }

 txt.setText(sb);
 txt.setMovementMethod(LinkMovementMethod.getInstance());  // important

不要忘记最后一行

标签:android,android-edittext,drawable,spannablestring,spannable
来源: https://codeday.me/bug/20190612/1226742.html