【问题标题】:Use string replace to change text color of URL link使用字符串替换更改 URL 链接的文本颜色
【发布时间】:2017-03-12 21:04:31
【问题描述】:

我的意思是,是否可以在单个字符串中将 文本 URL 链接 更改为绿色?

例如,当我有一个包含多个 URL 的字符串时,例如 below 示例。 (第 1 部分)是否可以使用 字符串替换 将“http://information.com/qokp076wulpw”的文本颜色设置为绿色,这样就可以像第 2 部分一样?

第 1 部分:

<a class="postlink" href="http://test.site/i7xt1.htm">http://test.site/i7xt1.htm<br/></a><br/>Mirror:<br/><a class="postlink" href="http://information.com/qokp076wulpw">http://information.com/qokp076wulpw<br/></a>

第 2 部分:

<a class="postlink" href="http://information.com/qokp076wulpw"><font color='#1AB053'>http://information.com/qokp076wulpw</font><br/></a>

【问题讨论】:

  • 您的链接已失效。

标签: java android string replace textcolor


【解决方案1】:

您可以查看this 解决方案,以在 HTML 内容中启用 TextView 中的链接,并且要设置链接颜色,您可以在 XML 布局中设置 android:textColorLink 属性以指定链接的颜色。

如果您只想将文本 URL 转换为链接,则不需要 Java 代码。您可以简单地将其添加到您的 TextView 中:

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/txt_no_html_tags"
    android:textColorLink="#00FF00"
    android:autoLink="web"
    android:linksClickable="true/>

【讨论】:

  • 嗨 dsdebastiani,这不是我要找的,完整的字符串确实已加载到 CustomeTextView 但并非所有 URL 链接都只需要将某些链接涂成绿色,其余链接应该保持正常颜色。这就是为什么我尝试使用字符串 replce 来实现这一点,然后在必要的地方注入 "" 以获得该特定 URL 的绿色。
  • 所以你可以使用SpannableString 来做到这一点。此解决方案可以帮助您:stackoverflow.com/a/7364716/3851246
  • 不能像函数“setTextFromHtml”一样工作,该函数不能用于此解决方案。我有一个自定义文本视图。我的职能。公共无效 setTextFromHtml(字符串文本){ this.setText(Html.fromHtml(Utils.parseSmiles(修剪(文本)))); this.setMovementMethod(LinkMovementMethod.getInstance());我需要将文本设置为 holder.text.setTextFromHtml(item);
  • stackoverflow.com/questions/7364119/… 有效!我需要更改一些代码,但它可以工作。非常感谢!
猜你喜欢
  • 2014-02-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-26
相关资源
最近更新 更多