文本视图中的 Hyperlink 未在 Android 中的文本周围显示 link

Hyperlink in textview not showing link around text in Android

我想在文本视图中为我在锚标记 android 中输入的地址显示 link 文本。到目前为止,我尝试使用以下代码进行输出。

文本视图XML布局:

<TextView
        android:id="@+id/test"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:autoLink="web"
        android:linksClickable="true"
        android:textSize="22sp" />

文本视图java代码:

    private String tweet="&lt;a href=http://www.google.co.in&gt;Google&lt;/a&gt;";
    test = (TextView) findViewById(R.id.test);
    test.setText(Html.fromHtml(tweet));
test.setMovementMethod(LinkMovementMethod.getInstance());

它应该只显示 Google,点击它应该会打开。

试试这个

private String tweet = "<a href=\"http://www.google.co.in\">Google</a>";

代替你的私人字符串推文

字符串 tweet 必须包含真正的 HTML 代码,而不是转义标签。此外,最好用引号将属性括起来:(需要转义)

所以这一行:

private String tweet="&lt;a href=http://www.google.co.in&gt;Google&lt;/a&gt;";

必须改为:

private String tweet="<a href=\"http://www.google.co.in\">Google</a>";

您还需要删除 android:autoLink="web"android:linksClickable="true":

<TextView
        android:id="@+id/test"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="22sp" />

我用过下面这样的:

  1. Xml

    <TextView
        android:id="@+id/tv_welcome_message"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"            
        android:textSize="16sp"/>
    
  2. 在activity..

     TextView   tv_welcome_message=(TextView) findViewById(R.id.tv_welcome_message);
     tv_welcome_message.setMovementMethod(LinkMovementMethod.getInstance());
     String text = " <a href='http://www.google.com'> Google </a>";
     tv_welcome_message.setText(Html.fromHtml(text));