我有以下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标签)对我来说是非常误导的…

其他回答

我使用自动链接来“自动下划线”文本,但我只是做了一个“onClick”来管理它(我自己遇到了这个问题)。

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginRight="10dp"
    android:textSize="18dp"
    android:autoLink="all"
    android:text="@string/twitter"
    android:onClick="twitter"/>

public void twitter (View view)
{
    try
    {
        Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://twitter.com/onaclovtech"));
        startActivity(browserIntent);
    }
    finally
    {
    }
}

它不需要任何权限,因为你将意图传递给管理这些资源的应用程序(即浏览器)。

这对我来说很管用。

这是一个非常单行的Android代码,使电话和URL可从textView中选择,无论字符串是什么,数据是什么。您不需要为此使用任何HTML标记。

TextView textView = (TextView)findViewById(R.id.textView1);
textView.setText("some URL is www.google.com phone 7504567890 another URL lkgndflg.com ");

// Makes the textView's Phone and URL (hyperlink) select and go.
Linkify.addLinks(textView, Linkify.WEB_URLS | Linkify.PHONE_NUMBERS);

在花了一段时间之后,我发现:

android:autoLink="web"工作,如果你有完整的链接在你的HTML。以下内容将以蓝色高亮显示,并可点击:

Some text <a href="http://www.google.com">http://www.google.com</a> . 一些文字http://www.google.com

view.setMovementMethod (LinkMovementMethod.getInstance ());将与以下工作(将突出显示和可点击):

Some text <a href="http://www.google.com">http://www.google.com</a> . 一些文字http://www.google.com 一些文本<a href="http://www.google.com">转到谷歌</a>

注意,第三个选项有一个超链接,但是链接的描述(标签之间的部分)本身并不是链接。android:autoLink="web"不工作与这样的链接。

android:autoLink="web"如果在XML中设置将覆盖view.setMovementMethod(LinkMovementMethod.getInstance());(即。第三类链接将被突出显示,但不能点击)。

这个故事的寓意是使用view.setMovementMethod(LinkMovementMethod.getInstance());在你的代码中,确保你没有android:autoLink="web"在你的XML布局中,如果你想要所有的链接都是可点击的。

我只使用android:autoLink=“web”,它工作得很好。单击该链接将打开浏览器并显示正确的页面。

我能猜到的一件事是,其他一些视图在链接的上方。透明的内容填充整个父元素,但不显示链接上方的任何内容。在这种情况下,点击指向这个视图而不是链接。

通过使用linkify:

Linkify获取一段文本和一个正则表达式,并将文本中的所有正则表达式匹配转换为可点击的链接:

TextView textView = (TextView) findViewById(R.id.textView);
textView.setText("http://example.com");
Linkify.addLinks(textView, Linkify.WEB_URLS);

别忘了

import android.widget.TextView;