如何添加点击动作的ImageSpan [英] How to add click action for the ImageSpan

查看:390
本文介绍了如何添加点击动作的ImageSpan的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ImageSpan集的文本编辑。我想补充的动作 - 当上了ImageSpan用户点击,就会弹出一个对话框并显示大图

I have a ImageSpan set in the TextEdit. And I want to add the action - when user click on the ImageSpan, it will popup a dialog and show the big image.

我检查了SDK和似乎ImageSpan不支持的onclick。反正是有使的onclick用于支持图片的ImageSpan或其他跨度?

I checked the SDK and seems the ImageSpan doesn't support onclick. Is there anyway to enable the onclick for the ImageSpan or other Span that support Image?

我查了code,发现有一个URLSpan创建与ImageSpan因为输入字符串是

I checked the code and found there is a URLSpan created with the ImageSpan because the input string is

但似乎URLSpan不工作,也没有点击操作创建它。你知道吗?

But seems the URLSpan doesn't work and there is no click action create for it. Any idea?

感谢。

推荐答案

我一直想在今天解决同样的问题,并找到解决方案。 为了使图像点击你需要一个ClickableSpan对象附加到同一范围ImageSpan为你的形象。 当你从Html.fromHtml(您跨区对象),你可以通过一组分配给它ImageSpan对象,并附加额外ClickableSpan对象。

I've been trying to solve the same problem today and find the solution. To make the image clickable you need to attach a ClickableSpan object to the same range as ImageSpan for your image. When you get your Spanned object from Html.fromHtml() you can go through the set of ImageSpan objects assigned for it and attach additional ClickableSpan object.

这样的:

            ImageSpan[] image_spans = s.getSpans(0, s.length(), ImageSpan.class);

            for (ImageSpan span : image_spans) {

                final String image_src = span.getSource();
                final int start = s.getSpanStart(span);
                final int end = s.getSpanEnd(span);

                ClickableSpan click_span = new ClickableSpan() {

                    @Override
                    public void onClick(View widget) {

                        Toast.makeText(HtmlImagesTestActivity.this,
                                "Image Clicked " + image_src,
                                Toast.LENGTH_SHORT).show();

                    }

                };

                ClickableSpan[] click_spans = s.getSpans(start, end, ClickableSpan.class);

                if(click_spans.length != 0) {

                    // remove all click spans

                    for(ClickableSpan c_span : click_spans) {
                        s.removeSpan(c_span);
                    }


                }


                s.setSpan(click_span, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

            }               

这篇关于如何添加点击动作的ImageSpan的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆