【问题标题】:Adding a clickable web link within Android TextView在 Android TextView 中添加可点击的 Web 链接
【发布时间】:2022-11-13 22:05:45
【问题描述】:

我正在尝试在我的 TextView 的文本中添加指向网站的链接。

我正在使用 Html.fromHtml() 方法来设置文本,并且链接看起来像预期的那样,但是当我单击它时,它什么也没做。我尝试将 motionMethod 设置为 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
    android:id="@+id/agreement_text"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textSize="12sp"
    android:linksClickable="true"
    android:autoLink="all"/>

【问题讨论】:

    标签: android hyperlink textview


    【解决方案1】:

    我实际上在这篇文章的评论中找到了答案:Android: textview hyperlink 评论是:“最正确的答案是直接在 strings.xml 中键入 HTML 锚标记,确保未设置 TextView 的自动链接属性,然后使用 setMovementMethod 设置 LinkMovementMethod”。因此,如果我没有设置 autoLink 或 linksClickable,而我设置了 LinkMovementMethod,它就可以正常工作。

    【讨论】:

      猜你喜欢
      • 2016-04-07
      • 1970-01-01
      • 2012-03-13
      • 1970-01-01
      • 2015-04-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多