轉自:http://ghostfromheaven.iteye.com/blog/752181
Android 的實現TextView中文字鏈接的方式有很多種。
總結起來大概有4種:
1.當文字中出現URL、E-mail、電話號碼等的時候,可以將TextView的android:autoLink屬性設置為相應的的值,如果是所有的類型都出來就是android:autoLink="all"。當然也可以在java代碼里做,textView01.setAutoLinkMask(Linkify.ALL);
2.將要處理的文字寫到一個資源文件,如string.xml,然后的java代碼里引用(直接寫在代碼了是不可行的,會直接把文字都顯示處理)
3.用Html類的fromHtml()方法格式化要放到TextView里的文字
4.用Spannable或實現它的類,如SpannableString來格式,部分字符串。
當然以上各種方法,不要在java代碼里加上textView03.setMovementMethod(LinkMovementMethod.getInstance());這樣的代碼,否則無效。
java代碼
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.method.ScrollingMovementMethod; import android.text.style.StyleSpan; import android.text.style.URLSpan; import android.text.util.Linkify; import android.widget.TextView; public class LinkTest extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); TextView textView01 = (TextView) findViewById(R.id.textView01); textView01.setAutoLinkMask(Linkify.ALL); String autoLinkText = "http://student.csdn.net/?232885我的CSDN博客 "; textView01.setText(autoLinkText); TextView textView02 = (TextView) findViewById(R.id.textView02); //String aLinkText = "<a href=/"http://student.csdn.net/?232885/">我的CSDN博客 </a>" // + "<a href=/"tel:4155551212/">and my phone number</a>"; //textView02.setText(aLinkText); textView02.setMovementMethod(LinkMovementMethod.getInstance()); TextView textView03 = (TextView) findViewById(R.id.textView03); String htmlLinkText = "<a href=/"http://student.csdn.net/?232885/"><u>我的CSDN博客 </u></a>"; textView03.setText(Html.fromHtml(htmlLinkText)); textView03.setMovementMethod(LinkMovementMethod.getInstance()); TextView textView04 = (TextView) findViewById(R.id.textView04); SpannableString ss = new SpannableString("call: 4155551212."); ss.setSpan(new StyleSpan(Typeface.BOLD), 0, 5, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); ss.setSpan(new URLSpan("tel:4155551212"), 6, 16, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); textView04.setText(ss); textView04.setMovementMethod(LinkMovementMethod.getInstance()); } }
string.xml文件
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="hello">Hello World, LinkTest!</string> <string name="app_name">LinkTest</string> <string name="aLinkText"> <a href="http://student.csdn.net/?232885">我的CSDN博客 </a> </string> </resources>
main.xml文件
<?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" android:autoLink="all" > <TextView android:id="@+id/textView01" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/textView02" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/aLinkText" /> <TextView android:id="@+id/textView03" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/textView04" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>