如何使 fromHTML 中的链接可点击?(安卓) [英] How can I make links in fromHTML clickable? (Android)

查看:23
本文介绍了如何使 fromHTML 中的链接可点击?(安卓)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这似乎是一个微不足道的问题,但让我有点困惑.我想使用 Html.fromHtml() 加载一个 HTML 字符串,并且字符串中的任何链接都可以点击并在浏览器中打开.

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.

基本示例:

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

使用此代码段,文本的格式就好像它是一个链接(蓝色,下划线),但它是不可点击的.我尝试过 Linkify,但它似乎只适用于非基于 HTML 的链接.

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.

有什么建议吗?

推荐答案

正如我所料,解决方案很简单:

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

第二行以某种方式激活链接行为,虽然我不太确定如何.Google 代码.

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

这篇关于如何使 fromHTML 中的链接可点击?(安卓)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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