来自 xml 的字符串,这是一个锚标记,单击它时不显示浏览器列表

String from xml which is an anchor tag not showing list of browser when clicking it

我在 xml 文件中有一个 段落 。在那个段落中,我有一些字符串作为link,如下所示:

which can be located at `<a href="https://www/tutorial.html">https://www.tutorial.com </a>.` this is 

点击它时,它应该会在浏览器中启动 URL。

实现是否正确?

有两种方法。

您需要致电setMovementMethod:

textView.setMovementMethod(LinkMovementMethod.getInstance());

第二

textView.setLinksClickable(true);

在XML中应该是

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

确保您使用 Html.fromHtml 设置文本,这样它会将 html 锚标记读取为锚,单击它会打开浏览器。

您还可以创建一个 WebView 并将 link 的字符串作为 URL 传递,以便在 Application 中打开它而不使用外部浏览器。 希望对你有帮助。

您在字符串中使用了 html 标签,如果您希望 href 标签被点击,您需要将此字符串加载到 webView 中,然后它就可以点击并按照您想要的方式运行。 但是,如果您想在 TextView 中显示,则只需将 url 放入字符串中,例如 https://www/tutorial.html

将此写入您的 string.xml 文件

<string name="link">&lt;a href="http://www.google.com">Google&lt;/a></string>

((TextView) findViewById(R.id.link)).setMovementMethod(LinkMovementMethod.getInstance());
((TextView) findViewById(R.id.link)).setText(Html.fromHtml(getResources().getString(R.string.link)));