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

问题描述 投票:1回答:3

我在xml文件中有一个段落。在那一段我有一些字符串作为如下链接:

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

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

实施是否正确?

android android-webview android-xml
3个回答
4
投票

有两种方法可以做到这一点。

你需要打电话给setMovementMethod

textView.setMovementMethod(LinkMovementMethod.getInstance());

第2

textView.setLinksClickable(true);

在XML中应该是

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

确保使用Html.fromHtml以这种方式设置文本,它将html锚标签作为锚点读取,点击它将打开浏览器。

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


0
投票

你正在使用字符串中的html标签,如果你想要点击href标签,你需要将这个字符串加载到webView中,然后你就可以点击它并完全按照你想要的方式运行。但是如果你想在TextView中显示,那么只需将你的网址放在像https://www/tutorial.html这样的字符串中。


0
投票

将其写入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)));
© www.soinside.com 2019 - 2024. All rights reserved.