在另一项活动打开TextView的链接,而不是默认的浏览器 [英] Open TextView links at another activity, not default browser

查看:305
本文介绍了在另一项活动打开TextView的链接,而不是默认的浏览器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

经与 autoLinkMask TextView的设置为 Linkify.ALL ,我能打开的链接和浏览器显示网页。

Having the textView with autoLinkMask set to Linkify.ALL, i'm able to open the links and the browser shows the web-page.

我需要调用另一个活动,将做到这与它的web视图不离开应用程序。

I need to call another activity that will do it with it's webView not leaving the application.

重要提示:

  • 改变TextView的内容是不是一种选择,我需要有显示的联系,因为它们是与他们的方案,
  • 有大量的文字在TextView的,不仅是链接。

我看了看,通过 movementMethod IntentFilters ,可能会错过一些东西,但看起来它也没有办法。

I looked through movementMethod and IntentFilters, might miss something but looks like it can't help.

因此​​,任何选项拦截在触及链接的TextView 来用它做什么不打开浏览器?

So, any option to intercept the touched link in the TextView to do something with it not opening the browser ?

如果你想提<一个href="http://stackoverflow.com/questions/1697084/handle-textview-link-click-in-my-android-app">this SO问题,请给一些争论,为什么因为它似乎并没有解决同样的问题,因为我有。

If you want to mention this SO question, please give some arguments why cause it doesn't seem to solve the same problem as I have.

推荐答案

第1步:创建您自己的 ClickableSpan 的子类,你想要做什么在其的onClick()方法(例如,名为 YourCustomClickableSpan

Step #1: Create your own subclass of ClickableSpan that does what you want in its onClick() method (e.g., called YourCustomClickableSpan)

第2步:运行批量转换所有 URLSpan 对象的是 YourCustomClickableSpan 的对象。我有一个实用工具类这样的:

Step #2: Run a bulk conversion of all of the URLSpan objects to be YourCustomClickableSpan objects. I have a utility class for this:

public class RichTextUtils {
    public static <A extends CharacterStyle, B extends CharacterStyle> Spannable replaceAll(Spanned original,
    Class<A> sourceType,
    SpanConverter<A, B> converter) {
        SpannableString result=new SpannableString(original);
        A[] spans=result.getSpans(0, result.length(), sourceType);

        for (A span : spans) {
            int start=result.getSpanStart(span);
            int end=result.getSpanEnd(span);
            int flags=result.getSpanFlags(span);

            result.removeSpan(span);
            result.setSpan(converter.convert(span), start, end, flags);
        }

        return(result);
    }

    public interface SpanConverter<A extends CharacterStyle, B extends CharacterStyle> {
        B convert(A span);
    }
}

您会使用这样的:

yourTextView.setText(RichTextUtils.replaceAll((Spanned)yourTextView.getText(),
                                             URLSpan.class,
                                             new URLSpanConverter()));

用自定义的 URLSpanConverter 是这样的:

class URLSpanConverter
      implements
      RichTextUtils.SpanConverter<URLSpan, YourCustomClickableSpan> {
    @Override
    public URLSpan convert(URLSpan span) {
      return(new YourCustomClickableSpan(span.getURL()));
    }
  }

将所有 URLSpan 反对 YourCustomClickableSpan 的对象。

这篇关于在另一项活动打开TextView的链接,而不是默认的浏览器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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