溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

怎么在Android中利用TextView實現(xiàn)超鏈接

發(fā)布時間:2021-04-16 16:54:44 來源:億速云 閱讀:433 作者:Leah 欄目:移動開發(fā)

今天就跟大家聊聊有關(guān)怎么在Android中利用TextView實現(xiàn)超鏈接,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。

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)驗,我們知道網(wǎng)頁中的超鏈接也可以在TextView中打開,如下:

只要我們寫好協(xié)議,這個其實也很簡單。

 tv1.setText(Html.fromHtml("<a href='tel:18565554482'>打電話</a>,<a href='smsto:18565554482'>發(fā)短信</a>,<a href='mailto:584991843@qq.com'>發(fā)郵件</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'>發(fā)短信</a>,<a href='mailto:584991843@qq.com'>發(fā)郵件</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中設(shè)置該TextView為可點擊狀態(tài)即可:

tv4.setMovementMethod(LinkMovementMethod.getInstance()); 

4. 使用SpannableString實現(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可點擊 
tv3.setMovementMethod(LinkMovementMethod.getInstance());

5. 使用SpannableTextView實現(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);

實現(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);
    }
  }

看完上述內(nèi)容,你們對怎么在Android中利用TextView實現(xiàn)超鏈接有進(jìn)一步的了解嗎?如果還想了解更多知識或者相關(guān)內(nèi)容,請關(guān)注億速云行業(yè)資訊頻道,感謝大家的支持。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI