Android 台灣中文網
標題:
android怎麼給文本中的電話號碼和網址自動添加鏈接
[打印本頁]
作者:
暗桌之光
時間:
2011-6-14 15:30
標題:
android怎麼給文本中的電話號碼和網址自動添加鏈接
有三個方法:
一。
設置TextView的autoLink屬性:他有幾個值all、web、phone、email等。當文中有這幾種類型的文本值時,點擊它將進入網頁、打電話或者email的activity,這是最簡單的方法
二。
在文本值直接添加鏈接
1.例如在string.xml文件中:
<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構建文本
tv2.setText(
Html.fromHtml("the google url: " +
"<a href=\"http://www.google.com\">http://www.google.com</a><br/>" +
"the telephone: " +
"<a href=\"tel:18603045201\">18603045201</a>)"
));
tv2.setMovementMethod(LinkMovementMethod.getInstance());
複製代碼
三。
使用SpanableString指定某段字串為鏈接文本
TextView tv3=(TextView)findViewById(R.id.tv3);
SpannableString ss=
new SpannableString("the google url: http://www.google.com 18600000001");
ss.setSpan(new URLSpan("http://www.google.com"),
16, 37, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
ss.setSpan(new URLSpan("tel:18603045201"),
38, 49, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
tv3.setText(ss);
tv3.setMovementMethod(LinkMovementMethod.getInstance());
複製代碼
歡迎光臨 Android 台灣中文網 (https://apk.tw/)
Powered by Discuz! X3.1