【问题标题】:How to add clickable link in string xml file in Kotlin?如何在 Kotlin 的字符串 xml 文件中添加可点击的链接?
【发布时间】:2021-11-28 09:14:06
【问题描述】:
如何使字符串资源中的链接可点击并在用户点击时打开网页?
<string name="about" translatable="false">
Click <annotation font="lato_bold">.
<a href="https://google.com/">here</a></annotation>
for more information
</string>
我可以用TextView 做到这一点,但我不知道如何用字符串资源做到这一点。
谢谢。
【问题讨论】:
标签:
android
xml
kotlin
hyperlink
【解决方案1】:
改变
<string name="about" translatable="false">
Click <annotation font="lato_bold">.
<a href="https://google.com/">here</a></annotation>
for more information
</string>
到
<string name="about">
<a href="https://google.com/">here</a>
</string>
你可以直接在字符串标签中使用anchor tag
【解决方案2】:
你可以通过编码做到这一点:
科特林:
var result:Textview = findViewById(R.id.myTextview)
result!!.movementMethod = LinkMovementMethod.getInstance()
val text = "<a href='http://www.google.com'>Google.com</a>"
result!!.text = Html.fromHtml(text)