使用Linkify.addLinks结合了Html.fromHtml [英] Using Linkify.addLinks combine with Html.fromHtml

查看:872
本文介绍了使用Linkify.addLinks结合了Html.fromHtml的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个的TextView ,获取其数据通过调用此设置:

I have a TextView that gets it's data set by calling this:

tv.setText(Html.fromHtml(myText));

字符串会将myText 包含部分格式化的HTML数据。例如,它可能有字体标签,但没有使用&LT格式化的URL链接; A HREF = ...> 标记。我希望能使用 Linkify.addLinks(...)来做到这一点,因为我的文字可能包括其他类型的Linkify会为我相应地转换链接。所以我写了我的code看起来是这样的:

The string myText contains partially formatted html data. For example, it might have font tags, but not have any url links formatted using <a href=...> tags. I was hoping to use the Linkify.addLinks(...) to do that since my text could include other types of links that Linkify would convert for me appropriately. So I wrote my code to look like this:

String myText = "<font color=\"red\">Red text</font> and Url: www.google.com";
tv.setText(Html.fromHtml(myText));
Linkify.addLinks(tv, Linkify.ALL);
tv.setMovementMethod(LinkMovementMethod.getInstance());

这不能正常工作。这意味着它处理的字体标签,但Linkify并不URL转换为 UrlSpan 正常。

This does not work properly. Meaning that it processes the font tags but Linkify does not convert urls to UrlSpan properly.

另外,如果我叫的setText()未经Html.fromHtml(..),Linkify工作,但后来我失去了从HTML字体标签格式的所有文本。不知怎的,他们都似乎是相互矛盾的,我只能有一个或其他。

Alternatively, if I call setText() without the Html.fromHtml(..), Linkify works but then I lose all the text formatted from the html font tags. Somehow they both seem to be conflicting and I can have only one or the other.

现在这里是我不明白最有趣的部分。如果我删除从Java Linkify code和去我的布局xml和摆在那里以下行,一切似乎是工作(Linkify和fromHtml双方最终打起来好看......不知)

Now here's the interesting part that I dont understand. If I remove the Linkify code from java and go to my layout xml and put the following lines in there, all seems to be working (Linkify and fromHtml both end up playing nice together... somehow)

<TextView
    ... 
    android:autoLink="all"
    android:linksClickable="true"
    ...
/>

有人能向我解释为什么这样做使一切工作??

Can someone explain to me why that would make everything work??

我看着源$ C ​​$ C为TextView中的 setMovementMethod()键,它最终结束呼叫:

I looked into the source code for TextView's setMovementMethod() and it eventually ends up calling:

setFocusable(true);
setClickable(true);
setLongClickable(true);

这理论上应该使一切工作和行为相同的XML布局code。我试图调用切换的顺序 Linkify.addLinks(..)的setText(Html.fromHtml(..))在java code,但并没有发挥作用。

That should theoretically make everything work and behave the same as the xml layout code. I tried switching the order of calling Linkify.addLinks(..) before setText(Html.fromHtml(..)) in the java code, but that didn't make a difference.

任何想法,为什么结合Linkify.addLinks()和Html.fromHtml()在Java中会导致此行为......但不是在XML布局?

Any ideas as to why combining Linkify.addLinks() and Html.fromHtml() in java would cause this behavior... but not in the xml layout?

推荐答案

这是因为 Html.fromHtml Linkify.addLinks 删除previous处理文本之前跨越。

It's because Html.fromHtml and Linkify.addLinks removes previous spans before processing the text.

使用此code得到它的工作:

Use this code to get it work:

public static Spannable linkifyHtml(String html, int linkifyMask) {
    Spanned text = Html.fromHtml(html);
    URLSpan[] currentSpans = text.getSpans(0, text.length(), URLSpan.class);

    SpannableString buffer = new SpannableString(text);
    Linkify.addLinks(buffer, linkifyMask);

    for (URLSpan span : currentSpans) {
        int end = text.getSpanEnd(span);
        int start = text.getSpanStart(span);
        buffer.setSpan(span, start, end, 0);
    }
    return buffer;
}

这篇关于使用Linkify.addLinks结合了Html.fromHtml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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