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

 

 

转载于:https://www.cnblogs.com/jico/archive/2010/11/02/1867437.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值