所以,我很好奇如何使这项工作...我正在从一个 API 中提取,其中一个元素是 HTML 格式的。所以,我认为让这些链接可点击会很酷。但是,其中一些链接并不完整,因为它们直接绑定(bind)到我从中获取 API 数据的站点。
这是我所拥有的示例:
textSummary.setMovementMethod(LinkMovementMethod.getInstance());
textSummary.setText(Html.fromHtml(object.getDescription()));
现在,如果我单击以“http://”开头的链接,一切正常。它让我可以选择在哪个浏览器中打开它。但是,如果 href 看起来像这样:
/inxile-entertainment-inc/3010-3966
我显然收到了一个错误,因为 Intent 没有格式正确的 Url。我应该补充的是:
http://www.giantbomb.com
让部分 href 工作。
有没有办法做到这一点,最终结果是设备启动默认浏览器(或者通常是我的应用程序之外的浏览器)?
谢谢!
编辑:这里有更多信息。
这是有问题的 TextView。本质上,这来自一个 fragment ,该 fragment 根据从 API 引入的数据填充它。
<TextView android:id="@+id/detail_summary"
android:textColor="@color/colorAccent"
android:layout_marginTop="10dp"
android:paddingStart="3dp"
android:paddingEnd="3dp"
android:paddingBottom="3dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
错误:
android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=/inxile-entertainment-inc/3010-3966 (has extras) }
此链接会让我选择在哪个浏览器中打开它:
http://www.youtube.com/watch?v=Gd43NYBzHuk
最佳答案
你可以使用
android:autoLink="web"
<TextView android:id="@+id/detail_summary"
android:textColor="@color/colorAccent"
android:layout_marginTop="10dp"
android:paddingStart="3dp"
android:paddingEnd="3dp"
android:paddingBottom="3dp"
android:autoLink="web"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
这个TextView不需要onClickLinstner
关于java - TextView 中的 HTML 链接问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34998735/