在android中处理SpannableStringBuilder图像

时间:2014-01-06 10:24:37

标签: android android-edittext drawable spannablestring spannable

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

enter image description here

我的代码:

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;
    }

2 个答案:

答案 0 :(得分:6)

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

不要忘记最后一行

答案 1 :(得分:1)

您要做的事情的最佳方式是使用AOSP *“官方”芯片库。

例如,当您开始在默认短信应用中输入数字时,这将显示可能匹配的联系人列表。一旦你选择了一个联系人或号码匹配一个,那么它将变成一个“芯片”,用“x”按钮将其从接收者列表中删除。

要获得此行为,请直接从AOSP使用此库: https://android.googlesource.com/platform/frameworks/opt/chips/+/master

在这里可以找到一个简短的解释,由在谷歌工作的Android开发者倡导者Roman Nurik提供: https://plus.google.com/+RomanNurik/posts/WUd7GrfZfiZ

* AOSP代表Android开源项目