四种实现Textview超链接的方式

来源:互联网 发布:淘宝买花苗 编辑:程序博客网 时间:2024/05/15 15:18

1、通过android:autoLink属性来实现对TextView中文本相应类型的链接进行自动识别。

例如:android:autoLink= all 可以自动识别TextView文本中的网络地址,邮件地址,电话号码,地图位置等,并进行链接。

android:autoLink所有支持的链接属性取值如下:

常量

描述

none

0x00

不进行自动识别(默认).

web

0x01

自动识别网络地址

email

0x02

自动识别邮件地址

phone

0x04

自动识别电话号码

map

0x08

自动识别地图位置

all

0x0f

自动识别以上四种链接属性(相当于web|email|phone|map).

注:可以通过“|”符号连接多个属性值来支持多种类型的链接自动识别。例如,

android:autoLink=web|email|phone支持对网络地址,邮件地址,电话号码的自动识别,并进行链接。
这是在XML文件中进行属性设置来识别链接的方式,还有一种在Java代码中进行属性设置的方式,同样可以实现类似功能。例如TextView对象mTextView1,我们可以通过mTextView1.setAutoLinkMask(intmask)来实现对TextView中文本相应类型的链接进行自动识别。其中mask所有取值如下:

常量

int

ALL

自动识别邮件地址,网络地址,地图位置和电话号码

 

int

EMAIL_ADDRESSES

自动识别邮件地址

 

int

MAP_ADDRESSES

自动识别地图位置

 

int

PHONE_NUMBERS

自动识别电话号码

 

int

WEB_URLS

自动识别网络地址

 

注:使用时请在常量前面加上Linkify.字样,例如:mTextView1.setAutoLinkMask(Linkify.ALL)

2、将含有HTML链接标记的文本写在Android资源文件中,如string.xml,然后在Java代码中直接引用。

3、通过Html类的fromHtmlStringsource)方法来对含有HTML链接标记的文本进行格式化处理。

4、通过Spannable或继承它的类,如SpannableString来格式化部分字符串。关于SpannableString的详细用法,请参考:http://blog.csdn.net/yang_hui1986527/article/details/6776629

注:默认情况下,第2,3,4种方法可以显示链接,但是无法响应用户的点击输入。如果需要激活该响应,需要调用TextView对象的以下方法:setMovementMethod(LinkMovementMethod.getInstance())

下面我们进行实例代码解析:

 res-value-string.xml

[html] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.     <string name="link_text_auto"><b>text1:</b> This is some text.  In  
  4.       this text are some things that are actionable.  For instance,  
  5.       you can click on http://www.google.com and it will launch the  
  6.       web browser.  You can click on google.com too.  And, if you  
  7.       click on (415) 555-1212 it should dial the phone.  
  8.     </string>  
  9.     <string name="link_text_manual"><b>text2:</b> This is some other  
  10.       text, with a <a href="http://www.google.com">link</a> specified  
  11.       via an <a> tag.  Use a \"tel:\" URL  
  12.       to <a href="tel:4155551212">dial a phone number</a>.  
  13.     </string>  
  14. </resources>  

res-layout-link.xml

[html] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2.   
  3. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  4.               android:orientation="vertical"  
  5.               android:layout_width="match_parent"  
  6.               android:layout_height="wrap_content">  
  7.   
  8.   <!-- 四个TextView控件, 每个控件都显示包含链接的文本。 -->  
  9.   
  10.   <!-- text1 自动识别文本链接,例如URL网络地址和电话号码等。 -->  
  11.   <TextView xmlns:android="http://schemas.android.com/apk/res/android"  
  12.             android:id="@+id/text1"  
  13.             android:layout_width="match_parent"  
  14.             android:layout_height="match_parent"  
  15.             android:autoLink="all"  
  16.             android:text="@string/link_text_auto"  
  17.             />  
  18.   
  19.   <!-- text2 使用包含用<a>等显式HTML标记来指定链接的文本资源。 -->  
  20.   <TextView xmlns:android="http://schemas.android.com/apk/res/android"  
  21.             android:id="@+id/text2"  
  22.             android:layout_width="match_parent"  
  23.             android:layout_height="match_parent"  
  24.             android:text="@string/link_text_manual"  
  25.             />  
  26.   
  27.   <!-- text3 在Java代码中使用HTML类来构造包含链接的文本。 -->  
  28.   <TextView xmlns:android="http://schemas.android.com/apk/res/android"  
  29.             android:id="@+id/text3"  
  30.             android:layout_width="match_parent"  
  31.             android:layout_height="match_parent"  
  32.             />  
  33.   
  34.   <!-- text4 在Java代码中不使用HTML类来构造包含链接的文本。  -->  
  35.   <TextView xmlns:android="http://schemas.android.com/apk/res/android"  
  36.             android:id="@+id/text4"  
  37.             android:layout_width="match_parent"  
  38.             android:layout_height="match_parent"  
  39.             />  
  40.   
  41. </LinearLayout>  

src-com.example.android.apis.text-Link.java

[java] view plaincopy
  1. package com.example.android.apis.text;  
  2.   
  3. import com.example.android.apis.R;  
  4.   
  5. import android.app.Activity;  
  6. import android.graphics.Typeface;  
  7. import android.os.Bundle;  
  8. import android.text.Html;  
  9. import android.text.SpannableString;  
  10. import android.text.Spanned;  
  11. import android.text.method.LinkMovementMethod;  
  12. import android.text.style.StyleSpan;  
  13. import android.text.style.URLSpan;  
  14. import android.widget.TextView;  
  15.   
  16. public class Link extends Activity {  
  17.     @Override  
  18.     protected void onCreate(Bundle savedInstanceState) {  
  19.        //super.onCreate(savedInstanceState)是调用父类的onCreate构造函数  
  20.         //savedInstanceState是保存当前Activity的状态信息  
  21.         super.onCreate(savedInstanceState);  
  22.   
  23.         //将link布局文件渲染出一个View对象,并作为Activity的默认View  
  24.         setContentView(R.layout.link);  
  25.   
  26.         // text1 通过 android:autoLink 属性自动识别文本中的链接,例如URL网络地址和电话号码等。  
  27.          // 不需要任何java代码来使之起作用。  
  28.   
  29.         // text2 含有由<a>等HTML标记指定的文本链接。默认情况下,这些链接可以显示但不会响应用户输入。  
  30.         //要想这些链接响应用户的点击输入,你需要调用TextView的setMovementMethod()方法 。  
  31.           
  32.         TextView t2 = (TextView) findViewById(R.id.text2);  
  33.         t2.setMovementMethod(LinkMovementMethod.getInstance());  
  34.   
  35.         // text3 显示在java代码中通过HTML类来创建包含文本链接的文本,而不是从文本资源中创建。  
  36.         //请注意,对于一个固定长度文本,最好像上面的例子一样,从文本资源中创建。  
  37.         // 这个例子仅仅说明您怎样去显示来自动态来源(例如,网络)的文本。  
  38.   
  39.         TextView t3 = (TextView) findViewById(R.id.text3);  
  40.         t3.setText(  
  41.             Html.fromHtml(  
  42.                 "<b>text3:</b>  Text with a " +  
  43.                 "<a href=\"http://www.google.com\">link</a> " +  
  44.                 "created in the Java source code using HTML."));  
  45.         t3.setMovementMethod(LinkMovementMethod.getInstance());  
  46.   
  47.         // text4 举例说明完全不通过HTML标记来构建一个包含链接的格式化文本。  
  48.         // 对于固定长度的文本,你最好使用string资源文本(即在string.xml中指定),而不是硬编码值(即在java代码中指定)。  
  49.   
  50.         //构建一个SpannableString  
  51.         SpannableString ss = new SpannableString(  
  52.             "text4: Click here to dial the phone.");  
  53.   
  54.         //设置粗体  
  55.         ss.setSpan(new StyleSpan(Typeface.BOLD), 06,  
  56.                    Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);  
  57.         //设置电话号码的链接  
  58.         ss.setSpan(new URLSpan("tel:4155551212"), 1317,  
  59.                    Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);  
  60.   
  61.         TextView t4 = (TextView) findViewById(R.id.text4);  
  62.         t4.setText(ss);  
  63.         t4.setMovementMethod(LinkMovementMethod.getInstance());  
  64.     }  
  65. }  
0 0
原创粉丝点击