如果需要把链接变成直接可以点击的,有以下方法
1. 设置TextView 类的autoLink属性
2. 设置TextView 类的 setMovementMethod方法
3. 用 SpannableString 类
实例
package demo.link;
import android.app.Activity;
import android.graphics.Typeface;
import android.os.Bundle;
import android.text.Html;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.method.LinkMovementMethod;
import android.text.style.StyleSpan;
import android.text.style.URLSpan;
import android.widget.TextView;
public class TestLinkActivity extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView tv2 = (TextView) findViewById(R.id.tv2);
tv2.setMovementMethod(LinkMovementMethod.getInstance());
TextView tv3 = (TextView) findViewById(R.id.tv3);
tv3.setText(Html.fromHtml("<b>测试3: <br/></b> 欢迎访问"
+ "<a href=\"http://www.google.com\">google</a> "
+ "或致电 <a href='tel:4155551212'>客服热线</a>"));
tv3.setMovementMethod(LinkMovementMethod.getInstance());
SpannableString ss = new SpannableString(
"text4: Click here to dial the phone.");
ss.setSpan(new StyleSpan(Typeface.BOLD), 0, 6,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
ss.setSpan(new URLSpan("tel:4155551212"), 13, 17,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
TextView tv4 = (TextView) findViewById(R.id.tv4);
tv4.setText(ss);
tv4.setMovementMethod(LinkMovementMethod.getInstance());
}
}
布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="测试链接" />
<TextView
android:id="@+id/tv1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/text1"
android:autoLink="all"
/>
/
<TextView
android:id="@+id/tv2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/link_text_manual"
/>
<TextView
android:id="@+id/tv3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/tv4"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, TestLinkActivity!</string>
<string name="app_name">TestLink</string>
<string name="text1">
测试1:\n
如果有任何疑问,请登录 http://www.google.com.cn 。或致电 010-10000
</string>
<string name="link_text_manual"><b>测试2:\n</b>请访问
<a href="http://www.google.com">google</a> 查询详细信息。
或致电 <a href="tel:4155551212">客服热线</a>.
</string>
</resources>