在android中的TextView内自动链接 [英] Autolink inside a TextView in android

查看:17
本文介绍了在android中的TextView内自动链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何为 textview 的某些部分提供自动链接?例如:我在 TextView 中的文字是请点击此处打开此网页".我只想显示此处"文本的链接.我应该在点击文本here"时打开该网页,而不是在 TextView 的任何地方.

How to give autolink for some part of textview ? For example : My text inside TextView is "Please click here to open this webpage". I want to show link for only the text "here". And I should open that webpage onclick of the text "here" but not on the anywhere of TextView.

推荐答案

在string.xml中放一个字符串

Put a String in string.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="txtCredits">Support: <a href="http://www.stackoverflow.com">click here</a></string>
</resources>

你可以像这样在 textView 中使用它:

And you can use it in textView like this:

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

编辑

由于某种原因,上面的代码不能正常工作.所以,也添加下面的代码,

For some reason above code does not work properly. So, add below code also,

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

这篇关于在android中的TextView内自动链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆