Android開發(fā)實(shí)現(xiàn)TextView超鏈接5種方式源碼實(shí)例
Android實(shí)現(xiàn)TextView超鏈接一共有五種方式:推薦第四種、第五種
1. 直接在xml文件中配置autoLink屬性(簡單易用,效果單一)
autoLink屬性一共有六個值,分別是none(正常),web(將文本識別為一個網(wǎng)址),phone(將文本識別為一個電話號碼),mail(將文本識別為一個郵件地址),map(這個,呃,該怎么表述呢?會打開地圖應(yīng)用),all(根據(jù)文本自動識別)。一般情況下我們設(shè)置為all即可,我們看看,這個時候它就會自動將TextView中的電話號碼、郵件地址、網(wǎng)頁鏈接等識別出來,這中方式是最簡單的一種。如:
<TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:autoLink="all" android:text=" android:textSize="16dp" />
2. 使用HTML語言
我們知道TextView可以直接顯示轉(zhuǎn)換后的HTML,那么借助H5開發(fā)經(jīng)驗(yàn),我們知道網(wǎng)頁中的超鏈接也可以在TextView中打開,如下:
只要我們寫好協(xié)議,這個其實(shí)也很簡單。
tv1.setText(Html.fromHtml("<a href='tel:18565554482'>打電話</a>,<a href='smsto:18565554482'>發(fā)短信</a>,<a href='mailto:584991843@qq.com'>發(fā)郵件</a>,<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'>發(fā)短信</a>,<a href='mailto:584991843@qq.com'>發(fā)郵件</a>,<a >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中設(shè)置該TextView為可點(diǎn)擊狀態(tài)即可:
tv4.setMovementMethod(LinkMovementMethod.getInstance());
4. 使用SpannableString實(shí)現(xiàn)超鏈接(效果多樣)
關(guān)于SpannableString的更多使用,參見另一篇:
SpannableString ss = new SpannableString("打電話,發(fā)短信,發(fā)郵件,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:584991843@qq.com"), 8, 11, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); ss.setSpan(new URLSpan("http://www.baidu.com"), 12, 16, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); //SpannableString對象設(shè)置給TextView tv3.setText(ss); //設(shè)置TextView可點(diǎn)擊 tv3.setMovementMethod(LinkMovementMethod.getInstance());
5. 使用SpannableTextView實(shí)現(xiàn)(效果多樣)
設(shè)置單一效果:
// 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);
設(shè)置多重效果疊加:
// 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);
實(shí)現(xiàn)無下劃線超鏈接:
自定義的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); } }
本文主要為大家介紹了5種方式實(shí)現(xiàn)Android TextView超鏈接源碼實(shí)例,更多關(guān)于Android實(shí)現(xiàn)TextView超鏈接的文章請查看下面的相關(guān)鏈接
- Android文本視圖TextView實(shí)現(xiàn)跑馬燈效果
- Android文本視圖TextView實(shí)現(xiàn)聊天室效果
- Android使用TypeFace設(shè)置TextView的文字字體
- Flutter中嵌入Android 原生TextView實(shí)例教程
- android使用TextView實(shí)現(xiàn)跑馬燈效果
- android TextView中識別多個url并分別點(diǎn)擊跳轉(zhuǎn)方法詳解
- Android開發(fā)中TextView各種常見使用方法小結(jié)
- Android開發(fā)之TextView使用intent傳遞信息,實(shí)現(xiàn)注冊界面功能示例
- Android使用AutoCompleteTextView實(shí)現(xiàn)自動填充功能的案例
- Android為textView設(shè)置setText的時候報錯的講解方案
- 詳解Android TextView屬性ellipsize多行失效的解決思路
- 在Android TextView中顯示圖片的4種方式詳解
- Android實(shí)現(xiàn)梯形TextView效果
相關(guān)文章
Android FlowLayout流式布局實(shí)現(xiàn)詳解
這篇文章主要為大家詳細(xì)介紹了Android FlowLayout流式布局的實(shí)現(xiàn)方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2020-09-09Recyclerview添加頭布局和尾布局、item點(diǎn)擊事件詳解
這篇文章主要為大家詳細(xì)介紹了Recyclerview添加頭布局和尾布局、item點(diǎn)擊事件的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-08-08Android開發(fā)筆記之:返回鍵的復(fù)寫onBackPressed()介紹
本篇文章是對Android中返回鍵的復(fù)寫onBackPressed()進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05Android App仿QQ制作Material Design風(fēng)格沉浸式狀態(tài)欄
這篇文章主要介紹了Android App仿QQ制作Material Design風(fēng)格沉浸式狀態(tài)欄的實(shí)例,同時也給出了4.4版本下實(shí)現(xiàn)效果與5.0的對比,需要的朋友可以參考下2016-04-04詳解Android中提示對話框(ProgressDialog和DatePickerDialog和TimePickerDi
這篇文章主要介紹了詳解Android中提示對話框(ProgressDialog和DatePickerDialog和TimePickerDialog&PopupWindow)的相關(guān)資料,需要的朋友可以參考下2016-01-01android studio xml文件實(shí)現(xiàn)添加注釋
這篇文章主要介紹了android studio xml文件實(shí)現(xiàn)添加注釋,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03

Android 簡單服務(wù)定位器模式實(shí)現(xiàn)

使用Timer實(shí)現(xiàn)網(wǎng)頁勻速加載的進(jìn)度條樣式