我正试图在我的TextView文本中添加到一个网站的链接。
我使用Html.fromHtml()方法来设置文本,链接看起来和预期的一样,但是当我单击它时,它没有做任何事情。我尝试将movementMethod设置为LinkMovementMethod.getInstance(),但这只是去掉了格式化,仍然无法工作。
agreementText.text = HtmlCompat.fromHtml("I'm familiar with <a href=\"https://www.sololearn.com/terms-of-use\">terms of use</a> of this website.", HtmlCompat.FROM_HTML_MODE_LEGACY)
agreementText.movementMethod = LinkMovementMethod.getInstance()
我的TextView:
<TextView
android:id="@+id/agreement_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="12sp"
android:linksClickable="true"
android:autoLink="all"/>
发布于 2022-11-13 13:46:29
我实际上在这篇文章的评论中找到了答案:Android: textview hyperlink注释是:“最正确的答案是直接在strings.xml中键入HTML锚标记,确保没有设置TextView的autolink属性,然后使用setMovementMethod设置LinkMovementMethod”。所以,如果我没有设置autoLink或linksClickable,并且设置了LinkMovementMethod,那么它就正常工作了。
https://stackoverflow.com/questions/74421502
复制相似问题