1

I have a textview which can contain links like https://www.google.com and hyper links with anchor tag Google

Now, I have added the below properties on this textview.

Linkify.addLinks(textview, Linkify.WEB_URLS);
textview.setMovementMethod(LinkMovementMethod.getInstance());

But the links like https://www.google.com these are coming fine in blue and redirecting to the page but anchor tags are not coming in blue and they are not redirecting it.

So, I want to make my textview to render both type of links: direct links and hyper links. How can I do this.

4

3 回答 3

1

Linkify(您调用它的方式)只知道转换实际上看起来像 Web URL 的东西(即它们以 http 或 https 开头,后跟冒号和两个斜杠等)。

如果您想将其他内容转换为链接,则必须添加更多参数以Linkify使其更智能地转换您想要的内容。您可以创建一个 MatchFilter 和一个 TransformFilter 然后调用Linkify.addLinks(TextView text, Pattern p, String scheme, Linkify.MatchFilter matchFilter, Linkify.TransformFilter transformFilter)

但是在我看来,您想使用“Google”之类的词并为“ https://www.google.com ”添加链接。那不是可以扫描的东西。为此,您需要使用SpannableStringBuilder. 您的代码可能如下所示:

    String text = "This is a line with Google in it.";
    Spannable spannable = new SpannableString(text);
    int start = text.indexOf("Google");
    int end = start + "Google".length();
    URLSpan urlSpan = new URLSpan("https://www.google.com");
    spannable.setSpan(urlSpan, start, end, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
    textView.setText(spannable);
于 2015-12-30T03:15:02.013 回答
0
//the string to add links to
val htmlString = "This has anchors and urls http://google.com also <a href=\"http://google.com\">Google</a>."

//Initial span from HtmlCompat will link anchor tags
val htmlSpan = HtmlCompat.fromHtml(htmlString, HtmlCompat.FROM_HTML_MODE_LEGACY) as Spannable

//save anchor links for later
val anchorTagSpans = htmlSpan.getSpans(0, htmlSpan.length, URLSpan::class.java)

//add first span to TextView
textView.text = htmlSpan

//Linkify will now make urls clickable but overwrite our anchor links
Linkify.addLinks(textView, Linkify.ALL)
textView.movementMethod = LinkMovementMethod.getInstance()
textView.linksClickable = true

//we will add back the anchor links here
val restoreAnchorsSpan = SpannableString(textView.text)
for (span in anchorTagSpans) {
    restoreAnchorsSpan.setSpan(span, htmlSpan.getSpanStart(span), htmlSpan.getSpanEnd(span), Spanned.SPAN_INCLUSIVE_INCLUSIVE)
}

//all done, set it to the textView
textView.text = restoreAnchorsSpan
于 2021-02-03T19:26:24.493 回答
0

它在javadocLinkify#addLinks(Spannable, Int)提到:

...如果掩码不为零,它还会删除附加到 Spannable 的任何现有 URLSpans,以避免在同一文本上重复调用它时出现问题。

虽然没有提到Linkify#addLinks(TextView, Int)您正在使用的位置,但似乎它们遵循相同的行为并且现有链接(即您问题中的“锚标签”)将在链接之前被删除。

要解决并保留现有链接(在您的情况下为“锚标签”),您需要备份现有跨度(即TextView#getText--> 转换为跨区--> 用于Spanned#getSpans列出现有链接--> 使用Spanned#getSpanStart和检索每个跨度的Spanned#getSpanEnd设置Spanned#getSpanFlags)

链接化后,重新添加跨度(即TextView#getText-->转换为Spannable-->用于Spannable#setSpan重新添加链接-->用 设置Spannable后面TextView#setText

根据您的情况,您可能还需要检查重叠的“锚标签”和“链接链接”并进行相应调整......

如您所见,这是相当乏味和复杂且容易出错的代码。为了简化事情,我只是将所有这些合并到Textoo库中以供重用和共享。使用 Textoo,您可以通过以下方式实现相同的目标:

TextView myTextView = Textoo
            .config((TextView) findViewById(R.id.view_location_disabled))
            .linkifyWebUrls()
            .apply();

Textoo 将保留现有链接并链接所有不重叠的网址。

于 2016-01-14T15:28:16.680 回答