关于TextView中怎么添加超链接的问题

在TextView中使用超级连接有几种方式:
  1.在属性中设置:
  < TextView
  android:id="@+id/testweb"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android: //是将文本的web网址解释成超链接
  android:text="@string/link_text_auto"
  />
  autoLink:一共有几种值:web,phone,map,email.all.none.
  分别是url连接。电话号码提取拨号,地图地址。电子邮件,全部解释就是能支持的超级连接全部起作用,
  none就是默认情况,没有超链接。

  2.使用html文本:
  例如:
  < ?xml version="1.0" encoding="utf-8"?>
  < resources>
  < string name="link_text_manual">
  a < a href="http://www.google.com">link< /a> specified
  via an < a> tag. Use a \"tel:\" URL
  to < a href="tel:4155551212">dial a phone number< /a>.
  < /string>
  < /resources>
  < TextView
  android:id="@+id/testemail"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:text="@string/link_text_manual"
  />

  3.在java代码中添加超级连接:
  TextView t = (TextView) findViewById(R.id.text3);
  t.setText(
  Html.fromHtml(
  "< b>text3:< /b> Text with a " +
  "< a href=\"http://www.google.com\">link< /a> " +
  "created in the Java source code using HTML."));
  t.setMovementMethod(LinkMovementMethod.getInstance());

  4.使用:SpannableString
  SpannableString ss = new SpannableString("Click here to dial the phone.");
  ss.setSpan(new StyleSpan(Typeface.BOLD), 0, 6, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
  ss.setSpan(new URLSpan("tel:4155551212"), 13, 17, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
  TextView t = (TextView) findViewById(R.id.text4);
  t.setText(ss);
  t.setMovementMethod(LinkMovementMethod.getInstance());

 

 

转载自:http://blog.sina.com.cn/s/blog_68119c8b0100vdam.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值