原谅链接:http://www.cnblogs.com/qingblog/archive/2012/05/24/2516148.html
当文字中出现URL、E-mail、电话号码等的时候,还可以为TextView设置链接。总结起来,一共有4种方法来为TextView实现链接。
(1)在xml里添加android:autoLink属性。如果写为android:autoLink=”all”,则为所有种类添加链接。当然,同样的也可以在Java代码中完成,用法为tv.setAutoLinkMask(Linkify.ALL)。
(2)将显示内容写到资源文件,一般为String.xml中,并且用<a>标签来声明链接,但是这还不够,要激活这个链接,需要在Java代码中使用setMovementMethod()方法设置TextView可点击。
(3)用Html类的fromHtml()方法格式化要放到TextView里的文字。
(4)用Spannable或实现它的类,如SpannableString。与其他方法不同的是,Spannable对象可以为个别字符设置链接(当然也可以为个别字符设置颜色、字体等,实现某些字符高亮显示的效果等)。
看一个实例:
package com.pms.tvdemo; import android.app.Activity; import android.graphics.Color; import android.graphics.Typeface; import android.os.Bundle; import android.text.Html; import android.text.Spannable; import android.text.SpannableString; import android.text.Spanned; import android.text.method.LinkMovementMethod; import android.text.style.BackgroundColorSpan; import android.text.style.StyleSpan; import android.text.style.URLSpan; import android.widget.TextView; publicclass MyTextView extends Activity { /** Called when the activity is first created. */ @Override publicvoid onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); TextView t2 = (TextView) findViewById(R.id.tv02); t2.setMovementMethod(LinkMovementMethod.getInstance()); //设置TextView可点击,第二种方式实现 TextView t3 = (TextView) findViewById(R.id.tv03); t3.setText( Html.fromHtml( "<b>text3:</b> " + "<a href=http://www.cnblogs.com/noTice520/archive/2011/11/24//"http://www.google.com\">链接到google</a> " + "使用HTML在java代码中实现")); t3.setMovementMethod(LinkMovementMethod.getInstance()); //第三种方式实现 //创建一个 SpannableString对象 SpannableString ss = new SpannableString( "text4: 点击这里拨打电话,点击这里链接到google"); ss.setSpan(new StyleSpan(Typeface.BOLD), 0, 6, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); //设置0~6字符为粗体 ss.setSpan(new URLSpan("tel:4155551212"), 9, 11, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); //设置9~11字符为拨号链接 ss.setSpan(new URLSpan("http://www.google.com"), 18, 20, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);//设置18~20为网站链接 ss.setSpan(new BackgroundColorSpan(Color.RED), 23 ,29, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); //设置 23~29字符为红色高亮 TextView t4 = (TextView) findViewById(R.id.tv04); t4.setText(ss);//SpannableString对象设置给TextView t4.setMovementMethod(LinkMovementMethod.getInstance());//第四种实现方式 } }
一共定义了4种实现方法,其中第二种方法一定要将文字内容写在资源文件中,写在Java代码中是无法实现的。
main.xml:
<?xmlversion="1.0"encoding="utf-8"?> <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <!-- text1 通过xml属性设置自动实现超链接 --> <TextView android:id="@+id/tv01" android:layout_width="fill_parent" android:layout_height="wrap_content" android:autoLink="all" android:text="@string/first_link" /> <!-- text2 通过含有<a>标签的string资源文件实现超链接 --> <TextView android:id="@+id/tv02" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/second_link" /> <!-- text3 通过在java代码中使用html来实现超链接 --> <TextView android:id="@+id/tv03" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <!-- text4 通过java代码实现。但是不用html--> <TextView android:id="@+id/tv04" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout>
可以发现,第一种方式在TextView标签中加入android:auto=“all”属性来实现了链接。
String.xml:
<?xmlversion="1.0"encoding="utf-8"?> <resources> <stringname="hello">Hello World, MyTextView!</string> <stringname="app_name">TextView Demo</string> <stringname="first_link"><b>text1:</b>这是用第一种方式实现的超链接。你只要点击<!-- <b>标签设置粗体 -->
</string> <stringname="second_link"><b>text2:</b>这是第二种方式实现的超链接,用一个<a>标签来声明。<ahref="http://www.google.com">链接到google</a><!-- <a>用了尖括号的转义字符 --> 用”tel“就可以链接到拨号<ahref="tel:123456789">拨打电话</a>. </string> </resources>
点击http://www.google.com ,就会进入浏览器打开该网页,点击google.com也行。点击15145625689就会拨打电话。
运行效果如图4-1所示。