TextView 添加链接
2015-11-18 11:58
477 查看
TextView 添加链接
有时我们需要在文字中添加链接供用户点击,如 email, phone, web, map 等。笔者此前的一个项目中就有此需求——当用户选择不同的提现方式时显示不同的提示文字,其中支付宝提现需要添加链接供用户点击查看,链接跳转至支付宝官方说明。这样做的好处是,说明来自官方,更加权威、可信、一致和简洁。下面介绍两种常用的给 TextView 添加链接的方法。1. android:autoLink 属性
autoLink 属性可应用于 TextView 及其子类如 Button 等,但并非所有子类均有效,如 Button 中有效但在 EditText 中链接虽然变色却不可点击,其对应的 xml 属性和方法如下:xml 属性 | 对应的方法 |
---|---|
android:autoLink | setAutoLinkMask() |
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 属性可接受的参数及说明如下:
常量 | 值 | 说明 |
---|---|---|
none | 0x00 | 默认值,不匹配任何地址。 |
web | 0x01 | 匹配 URLs |
0x02 | 匹配 email | |
phone | 0x04 | 匹配电话号码 |
map | 0x08 | 匹配地图地址 |
all | 0x0f | 匹配所有(相当于 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 字符串添加链接,本文不作过多介绍,有兴趣可自行搜索查看。相关文章推荐
- .NET导出Excel遇到的错误的解决方法
- UIImageView图片视图的基本概念和使用方法
- python--自动删除文件
- mms
- PostgreSQL与MySQL比较(转)
- docker centos7 httpd的小坑
- 点击TextField禁止弹出键盘
- log4j配置详解
- ASPxGridView实现点击行则自动启用编辑状态
- 漂亮的checkbox
- 划分树
- mysql limit查询优化
- jQuery-1.9.1源码分析系列(十) 事件系统——事件体系结构
- linux下DNS解析(nslookup、dig、host)
- 安装完CentOS 7 后必做的七件事
- win7 win10 win8系统文件夹重命名要刷新下文件名才会改变,桌面也不会自动刷新
- 下拉框
- Windows Store 应用获得设备 ID 的几种方案
- 安卓手机获取基站信息
- SQL 日期类型的处理转换