如何给textview的部分文本添加自动链接?例如:我在TextView的文本是“请点击这里打开这个网页”。我想只在文本“这里”显示链接。当我点击“这里”这两个字,就能打开那个网页,点击其它文本不能链接。
Textviews可以显示HTML,就解决了你的问题。在hyperlink中实现你想要的点击链接
String html = "My link is <a href=\"http://google.com\">here</a>";
myTextView.setText(Html.fromHtml(html));
在string.xml中加入字符串:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="txtCredits">Support: <a href="http://www.stackoverflow.com">click here</a></string>
</resources>
然后在textView中这样使用:
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:autoLink="web"
android:gravity="center"
android:linksClickable="true"
android:text="@string/txtCredits" />
在strings.xml使用HTML语法:
<string name="test">Click <a href="http://vtuhtan.info">here</a></string>
设置TextView属性来获得点击链接和自动链接。
TextView tv = findViewById(R.id.textView);
tv.setText(Html.fromHtml(getResources().getString(R.string.test)));