我在xml
文件中有一个段落。在那一段我有一些字符串作为如下链接:
which can be located at `<a href="https://www/tutorial.html">https://www.tutorial.com </a>.` this is
点击它时,它应该在浏览器中启动URL。
实施是否正确?
有两种方法可以做到这一点。
你需要打电话给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
中打开它而不使用外部浏览器。我希望它有所帮助。
你正在使用字符串中的html标签,如果你想要点击href标签,你需要将这个字符串加载到webView中,然后你就可以点击它并完全按照你想要的方式运行。但是如果你想在TextView中显示,那么只需将你的网址放在像https://www/tutorial.html这样的字符串中。
将其写入string.xml文件中
<string name="link"><a href="http://www.google.com">Google</a></string>
((TextView) findViewById(R.id.link)).setMovementMethod(LinkMovementMethod.getInstance());
((TextView) findViewById(R.id.link)).setText(Html.fromHtml(getResources().getString(R.string.link)));