我有以下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。


当前回答

由于数据绑定是出来的,我想分享我的解决方案数据绑定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());
    }
}

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

其他回答

理查德,下次,你应该在布局XML的TextView下添加这段代码。

android:autoLink="all"

应该是这样的。

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

您不需要使用此代码(t2.setMovementMethod(LinkMovementMethod.getInstance());)来使链接可单击。

另外,这里有一个事实:只要你设置了autoLink和linksClickable,不要忘记在String.xml文件中添加这个,这样可点击的链接就可以工作了。

<string name="txtCredits"><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());
    }
}

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

这就是我如何解决TextView中可点击和可见的链接(通过代码)

private void setAsLink(TextView view, String url){
    Pattern pattern = Pattern.compile(url);
    Linkify.addLinks(view, pattern, "http://");
    view.setText(Html.fromHtml("<a href='http://" + url + "'>http://" + url + "</a>"));
}

你只需要在XML的文本视图中添加这个:

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

在API演示中,我找到了问题的解决方案:

文件Link.java:

    // text2 has links specified by putting <a> tags in the string
    // resource.  By default these links will appear but not
    // respond to user input.  To make them active, you need to
    // call setMovementMethod() on the TextView object.

    TextView t2 = (TextView) findViewById(R.id.text2);
    t2.setMovementMethod(LinkMovementMethod.getInstance());

我删除了我的TextView上的大部分属性,以匹配演示中的内容。

<TextView
    android:id="@+id/text2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/txtCredits"/>

这就解决了问题。它很难发现和修复。

重要:不要忘记删除autoLink="web"如果你正在调用setMovementMethod()。