简体   繁体   English

在android中处理SpannableStringBuilder图像

[英]Handle SpannableStringBuilder image in android

I am developing an application in which selected contacts are added to EditText with a close mark image and when I click on that close mark image the contact should be removed. 我正在开发一个应用程序,其中选定的联系人添加到EditText与一个关闭标记图像 ,当我点击该关闭标记图像时 ,应删除该联系人。 I have completed code upto showing close mark image but I don't know how to handle those close mark images . 我已完成代码显示关闭标记图像,但我不知道如何处理那些关闭标记图像 Please suggest me how. 请建议我如何。

在此输入图像描述

My code: 我的代码:

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 is what you want: 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

Do not forget the last line 不要忘记最后一行

The best way to do what you are trying to do is to use the AOSP* "official" Chips library. 做你想做的最好的方法是使用AOSP *“官方” 芯片库。

For instance, when you start entering a number in the default sms app, this will show a list of possible matching contacts. 例如,当您开始在默认短信应用程序中输入数字时,这将显示可能匹配的联系人列表。 Once you select a contact or the number matches one, then it will turn into a "chip", with an 'x' button to remove it from the receiver list. 一旦你选择一个联系人或号码匹配一个,那么它将变成一个“芯片”,带有一个“x”按钮将其从接收者列表中删除。

To obtain this behaviour, use this library, directly from the AOSP: https://android.googlesource.com/platform/frameworks/opt/chips/+/master 要获得此行为,请直接从AOSP使用此库: https//android.googlesource.com/platform/frameworks/opt/chips/+/master

A brief explanation can be found here, by Roman Nurik, an Android Developer Advocate working at Google: https://plus.google.com/+RomanNurik/posts/WUd7GrfZfiZ 这里有一个简短的解释,由在谷歌工作的Android开发者倡导者Roman Nurik提供: https//plus.google.com/+RomanNurik/posts/WUd7GrfZfiZ

*AOSP stands for Android Open Source Project * AOSP代表Android开源项目

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM