简体   繁体   English

如何为ImageSpan添加单击操作

[英]How to add click action for the ImageSpan

I have a ImageSpan set in the TextEdit. 我在TextEdit中设置了ImageSpan。 And I want to add the action - when user click on the ImageSpan, it will popup a dialog and show the big image. 我想添加动作 - 当用户点击ImageSpan时,它会弹出一个对话框并显示大图像。

I checked the SDK and seems the ImageSpan doesn't support onclick. 我检查了SDK,似乎ImageSpan不支持onclick。 Is there anyway to enable the onclick for the ImageSpan or other Span that support Image? 反正是否支持ImageSpan或支持Image的其他Span的onclick?

I checked the code and found there is a URLSpan created with the ImageSpan because the input string is 我检查了代码,发现有一个用ImageSpan创建的URLSpan,因为输入字符串是

But seems the URLSpan doesn't work and there is no click action create for it. 但似乎URLSpan不起作用,并且没有为它创建单击操作。 Any idea? 任何的想法?

Thanks. 谢谢。

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. 要使图像可单击,您需要将ClickableSpan对象附加到与ImageSpan相同的图像范围。 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. 当您从Html.fromHtml()获取Spanned对象时,您可以浏览为其分配的ImageSpan对象集并附加其他ClickableSpan对象。

Like this: 像这样:

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

            }               

I have found the key point。 In order to response to click action, we not only set clickablespan , but also set edittext'setMovementMethod, the code is like this: 我找到了关键点。为了响应点击动作,我们不仅设置了clickablespan,还设置了edittext'setMovementMethod,代码如下:

EditText.setMovementMethod(LinkMovementMethod.getInstance());

Here is the problem. 这是问题所在。 If set setMovementMethod to LinkMovementMethod.getInstance() , the edittext's cursor will be gone. 如果将setMovementMethod设置为LinkMovementMethod.getInstance() ,则edittext的光标将消失。 I don't know why 我不知道为什么

following the miaohua1982's answer above, it is clear that cursor is getting disabled after setting the setmovementmethod to LinkMovementmethod. 按照上面miaohua1982的回答,很明显在将setmovement方法设置为LinkMovementmethod后光标被禁用。 I have faced similar problem in textview where action mode(which will appear on LongPress of textview) is cancelled and im not getting any action items.I have solved this problem by extending the LinkMovementMethod and overriding a method as below. 我在textview中遇到了类似的问题,其中动作模式(将出现在textview的LongPress上)被取消,我没有得到任何动作项。我通过扩展LinkMovementMethod并覆盖如下方法解决了这个问题。 I hope even in editext it solves the problem. 我希望即使在editext它解决了这个问题。

class MyMovementMethod extends LinkMovementMethod{

 @Override
  public boolean canSelectArbitrarily() {
       return true;
    }    

}

First, make the area clickable from the properties. 首先,使区域可以从属性中单击。
Next, add OnClickListner for the same. 接下来,添加OnClickListner。

Do your custom action onclick method. 执行自定义操作onclick方法。

You might want to check out using ClickableSpan and attach the TextView to a LinkMovementMethod and override its onTouchEvent etc.... 您可能想要使用ClickableSpan签出并将TextView附加到LinkMovementMethod并覆盖其onTouchEvent等....

Hope that helps 希望有所帮助

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

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