一共有五種方式:推薦第四種、第五種
1. 直接在xml文件中配置autoLink屬性(簡單易用,效果單一)
autoLink屬性一共有六個值,分別是none(正常),web(將文本識別爲一個網址),phone(將文本識別爲一個電話號碼),mail(將文本識別爲一個郵件地址),map(這個,呃,該怎麼表述呢?會打開地圖應用),all(根據文本自動識別)。一般情況下我們設置爲all即可,我們看看,這個時候它就會自動將TextView中的電話號碼、郵件地址、網頁鏈接等識別出來,這中方式是最簡單的一種。如:
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:autoLink="all"
android:text="
android:textSize="16dp" />
2. 使用HTML語言
我們知道TextView可以直接顯示轉換後的HTML,那麼藉助H5開發經驗,我們知道網頁中的超鏈接也可以在TextView中打開,如下:
只要我們寫好協議,這個其實也很簡單。
tv1.setText(Html.fromHtml("<a href='tel:18565554482'>打電話</a>,<a href='smsto:18565554482'>發短信</a>,<a href='mailto:[email protected]'>發郵件</a>,<a href='http://www.baidu.com'>Go百度</a>"));
tv1.setMovementMethod(LinkMovementMethod.getInstance());
3. 在strings.xml中直接寫HTML,然後在TextView的xml中直接引用即可(跟第二種換湯不換藥)
strings.xml中的定義如下:
<string name="tv4"><a href='tel:18565554482'>打電話</a>,<a href='smsto:18565554482'>發短信</a>,<a href='mailto:[email protected]'>發郵件</a>,<a href='http://www.baidu.com'>Go百度</a></string>
TextView的XML定義如下:
<TextView
android:id="@+id/tv4"
android:layout_width="match_parent"
android:layout_height="48dp"
android:gravity="center"
android:text="@string/tv4"
android:textSize="24sp" >
</TextView>
然後只需要在Activity中設置該TextView爲可點擊狀態即可:tv4.setMovementMethod(LinkMovementMethod.getInstance());
4. 使用SpannableString實現超鏈接(效果多樣)
關於SpannableString的更多使用,參見另一篇:
SpannableString ss = new SpannableString("打電話,發短信,發郵件,Go百度");
ss.setSpan(new URLSpan("tel:18565554482"), 0, 3,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
ss.setSpan(new URLSpan("smsto:18565554482"), 4, 7,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
ss.setSpan(new URLSpan("mailto:[email protected]"), 8, 11,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
ss.setSpan(new URLSpan("http://www.baidu.com"), 12, 16,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
//SpannableString對象設置給TextView
tv3.setText(ss);
//設置TextView可點擊
tv3.setMovementMethod(LinkMovementMethod.getInstance());
5. 使用SpannableTextView實現(效果多樣)
設置單一效果:
// Setup single span
SpannableTextView tv1 = (SpannableTextView) view.findViewById(R.id.tv1);
Span span1 =
new Span.Builder("ForegroundSpan, BackgroundSpan, and CustomTypefaceSpan")
.foregroundColor(R.color.purple_500)
.backgroundColor(R.color.green_500)
.typeface(mItalicFont)
.build();
tv1.setFormattedText(span1);
設置多重效果疊加:
// Setup multiple spans
SpannableTextView tv2 = (SpannableTextView) view.findViewById(R.id.tv2);
List<Span> spans1 = new ArrayList<>();
spans1.add(new Span.Builder("ForegroundSpan")
.foregroundColor(R.color.red_500)
.build());
spans1.add(new Span.Builder("BackgroundSpan")
.backgroundColor(R.color.yellow_500)
.build());
spans1.add(new Span.Builder("ForegroundSpan and BackgroundSpan")
.foregroundColor(R.color.orange_500)
.backgroundColor(R.color.blue_500)
.build());
spans1.add(new Span.Builder("ForegroundSpan, BackgroundSpan, and CustomTypefaceSpan")
.foregroundColor(R.color.green_500)
.backgroundColor(R.color.indigo_500)
.typeface(mRegularFont)
.build());
tv2.setFormattedText(spans1);
實現無下劃線超鏈接:
自定義的urlspan 繼承URLSpan 去掉下劃線 //自定義urlspan 去掉下劃線
public class URLSpanNoUnderline extends URLSpan {
public URLSpanNoUnderline(String url) {
super(url);
}
@Override
public void updateDrawState(TextPaint ds) {
super.updateDrawState(ds);
ds.setUnderlineText(false);
ds.setColor(Color.BLACK);
}
}