我有以下TextView定义:

<TextView 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" android:text="@string/txtCredits"
    android:autoLink="web" android:id="@+id/infoTxtCredits"
    android:layout_centerInParent="true"
    android:linksClickable="true"/>

其中@string/txtCredits是一个字符串资源,包含<a href="some site">链接文本</a>。

Android正在突出显示TextView中的链接,但它们不响应单击。我做错了什么?我必须在我的活动中为TextView设置一个onClickListener像这样简单吗?

它看起来和我定义字符串资源的方式有关。

这行不通:

<string name="txtCredits"><a href="http://www.google.com">Google</a></string>

但这确实是:

<string name="txtCredits">www.google.com</string>

这是一个遗憾,因为我宁愿显示一个文本链接,而不是显示完整的URL。


当前回答

如果你想添加一个类似html的链接,你所需要做的就是:

添加一个类似html的资源字符串: <字符串名称= "链接" > < a href = " https://www.google.pl/ " >谷歌< / > < /字符串> 将你的视图添加到布局中,完全没有链接特定的配置: < TextView android: id =“@ + id /链接” android:文本= " @string /链接" / > ' 添加适当的MovementMethod编程到你的TextView: mLink = (TextView) findViewById(R.id.link); if (mLink != null) { mLink.setMovementMethod (LinkMovementMethod.getInstance ()); }

就是这样!是的,像“autoLink”和“linksClickable”这样的选项只在显式链接上工作(没有包装成HTML标签)对我来说是非常误导的…

其他回答

将CDATA添加到字符串资源中

Strings.xml

<string name="txtCredits"><![CDATA[<a href=\"http://www.google.com\">Google</a>]]></string>

由于数据绑定是出来的,我想分享我的解决方案数据绑定TextViews支持HTML标签与可点击的链接。

为了避免检索每个textview并使用From.html为他们提供html支持,我们扩展了textview并将逻辑放在setText()中

public class HtmlTextView extends TextView {

    public HtmlTextView(Context context) {
        super(context);
    }

    public HtmlTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public HtmlTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public void setText(CharSequence text, BufferType type) {
        super.setText(Html.fromHtml(text.toString()), type);
        this.setMovementMethod(LinkMovementMethod.getInstance());
    }
}

我已经做了一个主旨,也显示了使用这个实体和视图的例子。

我注意到使用android:autoLink="web"因此

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content" 
    android:autoLink="web"/>

对于url来说工作得很好,但是因为我有一个电子邮件地址和电话号码,我想要链接,我最终使用这一行android:autoLink="all"像这样

<TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content" 
        android:autoLink="all"/>

而且效果很好。

您遇到这个问题的原因是它只试图匹配“裸”地址。比如“www.google.com”或“http://www.google.com”。

通过Html.fromHtml()运行文本应该可以做到这一点。你必须通过编程来实现,但这是可行的。

我不得不在几个地方寻找这个,但我最终得到了这个版本的代码。

文件strings.xml:

<string name="name1">&lt;a href="http://www.google.com">link text1&lt;/a></string>
<string name="name2">&lt;a href="http://www.google.com">link text2&lt;/a></string>

文件myactivity.xml:

<TextView
    android:id="@+id/textview1"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:layout_marginTop="5dp" />

<TextView
    android:id="@+id/textview2"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:layout_marginTop="5dp" />

文件myactivity .java(在onCreate()):

TextView tv1 = (TextView)findViewById(R.id.textview1);
TextView tv2 = (TextView)findViewById(R.id.textview2);

tv1.setText(Html.fromHtml(getResources().getString(R.string.name1)));
tv2.setText(Html.fromHtml(getResources().getString(R.string.name2)));
tv1.setMovementMethod(LinkMovementMethod.getInstance());
tv2.setMovementMethod(LinkMovementMethod.getInstance());

这将创建两个可点击的超链接,文本链接text1和链接text2将用户重定向到谷歌。