您的位置:首页 > 其它

TextView 添加链接

2015-11-18 11:58 477 查看

TextView 添加链接

有时我们需要在文字中添加链接供用户点击,如 email, phone, web, map 等。笔者此前的一个项目中就有此需求——当用户选择不同的提现方式时显示不同的提示文字,其中支付宝提现需要添加链接供用户点击查看,链接跳转至支付宝官方说明。这样做的好处是,说明来自官方,更加权威、可信、一致和简洁。下面介绍两种常用的给 TextView 添加链接的方法。

1. android:autoLink 属性

autoLink 属性可应用于 TextView 及其子类如 Button 等,但并非所有子类均有效,如 Button 中有效但在 EditText 中链接虽然变色却不可点击,其对应的 xml 属性和方法如下:

xml 属性对应的方法
android:autoLinksetAutoLinkMask()
Google 官方文档对其说明:

Controls whether links such as urls and email addresses are automatically found and converted to clickable links. The default value is “none”, disabling this feature.

控制是否将 urls, email 等地址自动转换为可点击的链接,默认属性为 “none” 以禁用这一特性。

autoLink 属性可接受的参数及说明如下:

常量说明
none0x00默认值,不匹配任何地址。
web0x01匹配 URLs
email0x02匹配 email
phone0x04匹配电话号码
map0x08匹配地图地址
all0x0f匹配所有(相当于 web | email | phone | map)
举个例子,布局文件如下:

[code]<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/tv_link"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:autoLink="all"
        android:text="@string/my_info"/>
</LinearLayout>


其中 my_info 对应的 string 如下:

[code]<string name="my_info">我的电话: 10000\n我的博客: http://blog.csdn.net/mrchx\n我的邮箱: 10000@qq.com</string>


运行后显示效果如下:



以上在 xml 中设置 autoLink 属性,也可在代码中设定,如下:

[code]TextView linkTV = (TextView) findViewById(R.id.tv_link);
linkTV.setAutoLinkMask(Linkify.ALL); // 要在 setText() 之前, 否则无效.
linkTV.setText(R.string.my_info);


在代码中设定时需要注意 setAutoLinkMask() 方法一定要在设置显示内容之前执行,否则无效。这也意味着,如果你仅在 xml 中设置要显示的内容,然后在代码中执行 setAutoLinkMask() 也是无效的。

2. setMovementMethod()

以上介绍的可满足大部分需求,但如果我们只想展示描述性的文字并不显式的显示地址,在点击描述性的文字时跳转,此时以上方法就不适用了。如本文开头提到的支付宝提现说明,直接展示链接地址不如展示描术性的文字友好,效果如下:

MrChx 的 CSDN 博客

这个链接并不显示地址而仅显示描述文字但仍可点击,这在网页中极其常见,TextView 中也可做到。再举个例子,布局文件如下:

[code]<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/tv_link"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/my_info" />
</LinearLayout>


其中 my_info 对应的 string 如下:

[code]<string name="my_info"><a href="http://blog.csdn.net/mrchx">MrChx 的 CSDN 博客</a></string>


主要代码如下:

[code]TextView linkTV = (TextView) findViewById(R.id.tv_link);
linkTV.setMovementMethod(LinkMovementMethod.getInstance());


这样就可以做到只显示“MrChx 的 CSDN 博客”并可点击跳转的效果了,纯代码实现如下:

[code]TextView linkTV = (TextView) findViewById(R.id.tv_link);
linkTV.setMovementMethod(LinkMovementMethod.getInstance());
String text = "<a href=\"http://blog.csdn.net/mrchx\">MrChx 的 CSDN 博客</a>";
linkTV.setText(Html.fromHtml(text));


总结

Html 还可定义更多属性以显示更多的文字效果,基本上可以满足各种需求。此外还可通过创建 SpanableString 字符串添加链接,本文不作过多介绍,有兴趣可自行搜索查看。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: