Android TextView实现带链接文字事件监听的三种常用方式示例
作者:迟做总比不做强 时间:2021-10-12 23:59:35
本文实例讲述了Android TextView实现带链接文字事件监听的三种常用方式。分享给大家供大家参考,具体如下:
/**
* TextView实现文字链接跳转功能
* @description:
* @author ldm
* @date 2016-4-21 下午4:34:05
*/
public class TextViewLinkAct extends Activity {
private TextView tv_3;
private TextView tv_4;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.link);
setTextViewLink();
}
/**
* 通过不同方式实现TextView中文字点击链接跳转功能
*
* @description:
* @author ldm
* @date 2016-4-21 下午4:24:13
*/
private void setTextViewLink() {
// 以Html格式href链接方式实现跳转
tv_3 = (TextView) findViewById(R.id.text3);
tv_3.setText(Html
.fromHtml("<b>text3: Constructed from HTML programmatically.</b> Text with a "
+ "<a href=\"http://www.google.com\">link</a> "
+ "created in the Java source code using HTML."));
tv_3.setMovementMethod(LinkMovementMethod.getInstance());
// 通过SpannableString的setMovementMethod方法实现链接效果
SpannableString ss = new SpannableString(
"text4: Manually created spans. Click here to dial the phone.");
ss.setSpan(new StyleSpan(Typeface.BOLD), 0, 30,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
ss.setSpan(new URLSpan("tel:4155551212"), 31 + 6, 31 + 10,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
tv_4 = (TextView) findViewById(R.id.text4);
tv_4.setText(ss);
tv_4.setMovementMethod(LinkMovementMethod.getInstance());
}
}
布局文件
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:divider="?android:attr/listDivider"
android:orientation="vertical"
android:showDividers="middle" >
<!-- 通过在布局中autoLink属性设置TextView的链接功能. -->
<TextView
android:id="@+id/text1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:autoLink="all"
android:paddingBottom="8dp"
android:text="<b>text1: Various kinds
of data that will be auto-linked.</b> In
this text are some things that are actionable. For instance,
you can click on http://www.google.com and it will launch the
web browser. You can click on google.com too. If you
click on (415) 555-1212 it should dial the phone. Or just write
foobar@example.com for an e-mail link. If you have a URI like
http://www.example.com/lala/foobar@example.com you should get the
full link not the e-mail address. Or you can put a location
like 1600 Amphitheatre Parkway, Mountain View, CA 94043. To summarize:
https://www.google.com, or 650-253-0000, somebody@example.com,
or 9606 North MoPac Expressway, Suite 400, Austin, TX 78759."
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/text3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="8dp"
android:paddingTop="8dp"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/text4"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="8dp"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
</ScrollView>
其中通过在而已代码中android:autoLink属性的选项目有:none(无链接效果),web(网页链接),email(发邮件),phone(打电话),map(定位)及all(默认全都自动链接)。
希望本文所述对大家Android程序设计有所帮助。
来源:http://blog.csdn.net/true100/article/details/51210422
标签:Android,TextView,链接,事件监听
0
投稿
猜你喜欢
Android图片框架Glide原理深入探索
2021-07-22 05:08:42
java与JSON数据的转换实例详解
2022-07-03 22:48:25
使用递归实现数组求和示例分享
2023-04-24 02:17:40
Java实现人机猜拳游戏
2023-10-16 08:47:56
一文带你搞懂Java中Get和Post的使用
2023-04-16 23:57:24
Android实现多级树形菜单并支持多选功能
2023-08-29 20:01:04
C#中Html.RenderPartial与Html.RenderAction的区别分析
2022-03-17 01:27:59
如何使用mybatis-generator自动生成代码
2022-08-17 07:12:19
Java回调函数实例代码详解
2023-11-23 18:22:26
Android Alertdialog(实现警告对话框)
2022-03-02 10:57:54
eclipse下搭建hibernate5.0环境的步骤(图文)
2022-09-26 02:48:57
Android实现背景图滑动变大松开回弹效果
2022-10-15 10:45:15
C#实现GZip压缩和解压缩入门实例
2021-05-29 08:20:45
C#实现登录窗口(不用隐藏)
2021-11-21 09:30:24
Java实现的RSA加密解密算法示例
2022-05-03 09:20:38
Java中JavaBean对象和Map的互相转换方法实例
2021-07-12 04:46:05
Android实现控件拖动效果
2021-11-05 09:02:31
C#线程同步的几种方法总结
2022-01-31 19:49:06
C#异步编程Task的创建方式
2023-07-23 06:22:43
C#中的不可变数据类型介绍(不可变对象、不可变集合)
2022-06-13 19:08:33