如何在fromHTML中创建可单击的链接?(安卓)

时间:2022-01-07 07:30:39

This seems like a trivial problem, but is has me kind of stumped. I want to load an HTML string using Html.fromHtml(), and have any links in the string to be clickable and open in the browser.

这似乎是个微不足道的问题,但却让我陷入了困境。我想使用HTML . fromhtml()加载一个HTML字符串,并在该字符串中有任何链接可单击并在浏览器中打开。

Basic example:

基本的例子:

textView.setText(Html.fromHtml("<a href=\"http://www.google.com\">This is a link</a>"));

With this snippet, the text is formatted as if it were a link (blue, underlined), but it's not clickable. I tried Linkify, but it only seems to work with links that are not HTML-based.

使用这个片段,文本被格式化为一个链接(蓝色,下划线),但它不是可点击的。我尝试了Linkify,但它似乎只适用于非基于html的链接。

Any suggestions?

有什么建议吗?

5 个解决方案

#1


119  

As I assumed, the solution was trivial:

正如我所假设的,解决方案很简单:

textView.setText(Html.fromHtml("<a href=\"http://www.google.com\">This is a link</a>"));
textView.setMovementMethod(LinkMovementMethod.getInstance());

The second line somehow activates the link behavior, although I'm not quite sure how. The same question is addressed over at Google Code.

第二行以某种方式激活了链接行为,尽管我不太确定是怎么激活的。在谷歌代码中也解决了同样的问题。

#2


16  

As mentioned in other answers, a way forward is to use:

正如在其他答案中提到的,一种前进的方式是:

xtView.setText(Html.fromHtml("<a href=\"http://www.google.com\">This is a link</a>"));
textView.setMovementMethod(LinkMovementMethod.getInstance());

However, this won't work if you have ANY android:autoLink value set, not just 'web' as other comments seem to suggest. So that means you can use this solution to linkify URLs at the expense of having phone, email and maps being disabled/unlinked.

但是,如果你有android:autoLink值集,而不是像其他评论所说的“web”,这就行不通了。因此,这意味着您可以使用这个解决方案来链接url,以牺牲电话、电子邮件和地图被禁用/不链接的代价。

#3


5  

The javadoc of the LinkMovementMethod says that it

LinkMovementMethod的javadoc说

Supports clicking on links with DPad Center or Enter.

支持点击与DPad中心的链接或进入。

So it makes sense that works that way.

这样做是有道理的。

And confirmed, with 4.2.2 works like charm with just the

并证实,与4.2.2的作品一样,魅力与只是

textView.setMovementMethod(LinkMovementMethod.getInstance());

#4


3  

It should be this way:

应该是这样:

textView.setText(Html.fromHtml("<a href=\"http://www.google.com\">This is a link</a>"));
textView.setAutoLinkMask(Linkify.WEB_URLS);
textView.setLinksClickable(true);

in XML should be

在XML中应该

<TextView
    android:id="@+id/txtview"
    android:autoLink="web"
    android:linksClickable="true"
    />

#5


0  

String data="MyTest";

textView.setText(data);
textView.setText(Html.fromHtml(data));
textView.setMovementMethod(LinkMovementMethod.getInstance());
textView.setLinksClickable(true);

#1


119  

As I assumed, the solution was trivial:

正如我所假设的,解决方案很简单:

textView.setText(Html.fromHtml("<a href=\"http://www.google.com\">This is a link</a>"));
textView.setMovementMethod(LinkMovementMethod.getInstance());

The second line somehow activates the link behavior, although I'm not quite sure how. The same question is addressed over at Google Code.

第二行以某种方式激活了链接行为,尽管我不太确定是怎么激活的。在谷歌代码中也解决了同样的问题。

#2


16  

As mentioned in other answers, a way forward is to use:

正如在其他答案中提到的,一种前进的方式是:

xtView.setText(Html.fromHtml("<a href=\"http://www.google.com\">This is a link</a>"));
textView.setMovementMethod(LinkMovementMethod.getInstance());

However, this won't work if you have ANY android:autoLink value set, not just 'web' as other comments seem to suggest. So that means you can use this solution to linkify URLs at the expense of having phone, email and maps being disabled/unlinked.

但是,如果你有android:autoLink值集,而不是像其他评论所说的“web”,这就行不通了。因此,这意味着您可以使用这个解决方案来链接url,以牺牲电话、电子邮件和地图被禁用/不链接的代价。

#3


5  

The javadoc of the LinkMovementMethod says that it

LinkMovementMethod的javadoc说

Supports clicking on links with DPad Center or Enter.

支持点击与DPad中心的链接或进入。

So it makes sense that works that way.

这样做是有道理的。

And confirmed, with 4.2.2 works like charm with just the

并证实,与4.2.2的作品一样,魅力与只是

textView.setMovementMethod(LinkMovementMethod.getInstance());

#4


3  

It should be this way:

应该是这样:

textView.setText(Html.fromHtml("<a href=\"http://www.google.com\">This is a link</a>"));
textView.setAutoLinkMask(Linkify.WEB_URLS);
textView.setLinksClickable(true);

in XML should be

在XML中应该

<TextView
    android:id="@+id/txtview"
    android:autoLink="web"
    android:linksClickable="true"
    />

#5


0  

String data="MyTest";

textView.setText(data);
textView.setText(Html.fromHtml(data));
textView.setMovementMethod(LinkMovementMethod.getInstance());
textView.setLinksClickable(true);