a b c d e f g h i j k l m n o p q r s t u v w x y z

android---Text中电话号码、网址自动链接

假若TextView文本中有电话号码或者网址,我想通过点击电话号码或者网址就能实现打电话或者打开网页,android中已经为我们提供这样的属性和方法进行设置,大体可以分为三种:

1、设置TextView的autoLink属性:他有几个值all、web、phone、email等。当文中有这几种类型的文本值时,点击它将进入网页、打电话或者email的activity,这是最简单的方法

2、在文本值直接添加链接

    (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());

3、使用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());

 

 

posted @ 2010-11-02 18:28  莴笋炒肉  阅读(6571)  评论(0编辑  收藏  举报