Android 台灣中文網

標題: android在文本中添加超鏈接 [打印本頁]

作者: 暗桌之光    時間: 2011-6-23 15:43
標題: android在文本中添加超鏈接
在TextView中使用超級連接有幾種方式:
1.在屬性中設置:
  1. <TextView
  2. android:id="@+id/testweb"
  3.     android:layout_width="fill_parent"
  4.     android:layout_height="wrap_content"
  5.     android:autoLink="web" //是將文本的web網址解釋成超鏈接
  6.     android:text="@string/link_text_auto"
  7.     />
複製代碼
autoLink:一共有幾種值:web,phone,map,email.all.none.

分別是url連接。電話號碼提取撥號,地圖地址。電子郵件,全部解釋就是能支持的超級連接全部起作用,
none就是默認 情況。沒有超鏈接,
2.使用html文本:
例如:
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <resources>
  3.     <string name="link_text_manual">
  4.     a <a href="http://www.google.com">link</a> specified
  5.       via an <a> tag.  Use a \"tel:\" URL
  6.       to <a href="tel:4155551212">dial a phone number</a>.
  7.     </string>
  8. </resources>


  9. <TextView
  10. android:id="@+id/testemail"
  11.     android:layout_width="fill_parent"
  12.     android:layout_height="wrap_content"
  13.     android:text="@string/link_text_manual"
  14.     />
複製代碼
3.在java代碼中添加超級連接:
  1. TextView t = (TextView) findViewById(R.id.text3);
  2. t.setText(
  3. Html.fromHtml(
  4.   "<b>text3:</b>  Text with a " +
  5.   "<a href=\"http://www.google.com\">link</a> " +
  6.   "created in the Java source code using HTML."));
  7. t.setMovementMethod(LinkMovementMethod.getInstance());
複製代碼
使用:SpannableString
  1. SpannableString ss = new SpannableString("Click here to dial the phone.");
  2. ss.setSpan(new StyleSpan(Typeface.BOLD), 0, 6, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
  3. ss.setSpan(new URLSpan("tel:4155551212"), 13, 17, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
  4. TextView t = (TextView) findViewById(R.id.text4);
  5. t.setText(ss);
  6. t.setMovementMethod(LinkMovementMethod.getInstance());
複製代碼





歡迎光臨 Android 台灣中文網 (https://apk.tw/) Powered by Discuz! X3.1