Android 台灣中文網

標題: android怎麼給文本中的電話號碼和網址自動添加鏈接 [打印本頁]

作者: 暗桌之光    時間: 2011-6-14 15:30
標題: android怎麼給文本中的電話號碼和網址自動添加鏈接
有三個方法:
一。
設置TextView的autoLink屬性:他有幾個值all、web、phone、email等。當文中有這幾種類型的文本值時,點擊它將進入網頁、打電話或者email的activity,這是最簡單的方法

二。
在文本值直接添加鏈接
1.例如在string.xml文件中:
  1. <string><a href=http://www.google.com>http://www.google.com</a> <a href="tel:18600000001">tel</a> </string>
複製代碼

同時設置TextView屬性
setMovementMethod(LinkMovementMethod.getInstance());
2.在代碼中使用Hteml.fromHtml構建文本
  1.        tv2.setText(
  2.                 Html.fromHtml("the google url: " +
  3.                 "<a href=\"http://www.google.com\">http://www.google.com</a><br/>" +
  4.                 "the telephone: " +
  5.                 "<a href=\"tel:18603045201\">18603045201</a>)"
  6.                 ));
  7.         tv2.setMovementMethod(LinkMovementMethod.getInstance());
複製代碼


三。
使用SpanableString指定某段字串為鏈接文本
  1.         TextView tv3=(TextView)findViewById(R.id.tv3);
  2.         SpannableString ss=
  3.             new SpannableString("the google url: http://www.google.com 18600000001");
  4.         ss.setSpan(new URLSpan("http://www.google.com"),
  5.                 16, 37, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
  6.         ss.setSpan(new URLSpan("tel:18603045201"),
  7.                 38, 49, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
  8.         tv3.setText(ss);
  9.         tv3.setMovementMethod(LinkMovementMethod.getInstance());
複製代碼





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