使用Linkify + 正则式区分微博文本链接及跳转处理
如同新浪微博Android版的应用上,当我们点击微博文本上的链接会自动跳转界面,或网站链接跳转网页浏览,或邮箱链接跳转邮箱服务,或电话号码链接跳转拨号界面。Android 帮我们设计了一个类:Linkify Linkify是一个辅助类,通过RegEx样式匹配,自动地在TextView类(和继承的类)中创建超链接。符合特定的RegEx样式的文本会被转变成可点击的超链接,这些超链接隐式地调用startActivity(new Intent(Intent.ACTION_VIEW,uri)),符合的文本会作为目标URI。 你可以指定任意的字符串样式为链接,Linkify类提供了预置的通用内容类型(如电话号码和e-mail、web地址)。 Java代码:
TextView textView
=
(TextView)findViewById(R.id.myTextView);
Linkify.addLinks(textView,Linkify.WEB_URLS | Linkify.EMAIL_ADDRESSES); 可以在layout资源里使用android:autoLink特性来为View制作链接。它支持一个或多个(用|分割)自定义的值:none、web、email、phone或all。接下来的XML片段显示了如何为电话号码和e-mail地址添加超链接:
<
TextView
android:id ="@+id/txt" android:layout_width ="fill_parent" android:layout_height ="wrap_content" android:text ="@string/hello" android:textColor ="#FFFFFF" android:textColorLink ="#0082CB" android:autoLink ="web|phone|email" > </ TextView > 创建自定义的链接字符串: 为了定义自己的链接字符串,你需要创建一个RegEx样式来匹配文本,进而显示成超链接。和本地类型一样,通过调用Linkify.addLinks来指定目标View,但这次,传入的是新的RegEx样式。你还可以传入一个前缀,当链接点击时,它会添加到目标URI上。
int
flags
=
Pattern.CASE_INSENSITIVE;
Pattern p = Pattern.compile(“bquake[ 0 - 9 ] * b”,flags); Linkify.addLinks(myTextView,p,“content: // com.paad.earthquake/earthquakes/”); 关于Linkify的相关介绍到此结束,现在开始实现使用Linkify + 正则式实现类似微博链接界面跳转,一步一步简单做一个DEMO。 第一步:新建一个Android工程命名为LinkTest; 第二步:在Res/values下新建一个strings.xml资源文件,存储微博文本数据;
<?
xml version="1.0" encoding="utf-8"
?>
resources string name ="hello" > 【官员辱骂采访记者被微博曝光后遭通报批评】5月4日,新疆人民广播电台@新疆新闻广播孙建忠 在喀什地区建设局采访时,遭行政办公室主任霍敏辱骂,后被微博曝光,霍敏当天被建设局通报批评,扣罚一个月工资并取消年终评优资格。孙建忠称已接到霍敏电话说要当面道歉。http://t.cn/hg3PxV string ="app_name" > LinkTest >
第三步:修改main.xml布局文件,此处仅添加了一个TextView 作为微博文本的载体;
<?
xml version="1.0" encoding="utf-8"
?>
< LinearLayout xmlns:android ="http://schemas.android.com/apk/res/android" android:orientation ="vertical" android:layout_width ="fill_parent" android:layout_height ="fill_parent" > TextView android:id ="@+id/txt" ="wrap_content" android:text ="@string/hello" android:textColor ="#FFFFFF" android:textColorLink ="#0082CB" android:autoLink ="web|phone|email" /> </ LinearLayout > 第四步:定义一个公共类Defs.java,用来存放自定义链接字符串; |