Android TextView添加超链接的方法示例

 更新时间:2016年10月26日 11:09:13   作者:Wallace  
这篇文章主要介绍了Android TextView添加超链接的方法,结合实例形式分析了TextView控件添加HTML超链接的实现技巧,需要的朋友可以参考下

本文实例讲述了Android TextView添加超链接的方法。分享给大家供大家参考,具体如下:

public class Link extends Activity {
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.link);
    // text1 shows the android:autoLink property, which
    // automatically linkifies things like URLs and phone numbers
    // found in the text. No java code is needed to make this
    // work.
    // text2 has links specified by putting <a> tags in the string
    // resource. By default these links will appear but not
    // respond to user input. To make them active, you need to
    // call setMovementMethod() on the TextView object.
    TextView t2 = (TextView) findViewById(R.id.text2);
    t2.setMovementMethod(LinkMovementMethod.getInstance());
    // text3 shows creating text with links from HTML in the Java
    // code, rather than from a string resource. Note that for a
    // fixed string, using a (localizable) resource as shown above
    // is usually a better way to go; this example is intended to
    // illustrate how you might display text that came from a
    // dynamic source (eg, the network).
    TextView t3 = (TextView) findViewById(R.id.text3);
    t3.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."));
    t3.setMovementMethod(LinkMovementMethod.getInstance());
    // text4 illustrates constructing a styled string containing a
    // link without using HTML at all. Again, for a fixed string
    // you should probably be using a string resource, not a
    // hardcoded value.
    SpannableString ss = new SpannableString(
      "text4: 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 t4 = (TextView) findViewById(R.id.text4);
    t4.setText(ss);
    t4.setMovementMethod(LinkMovementMethod.getInstance());
  }
}

更多关于Android相关内容感兴趣的读者可查看本站专题:《Android视图View技巧总结》、《Android布局layout技巧总结》、《Android开发入门与进阶教程》、《Android调试技巧与常见问题解决方法汇总》、《Android多媒体操作技巧汇总(音频,视频,录音等)》、《Android基本组件用法总结》及《Android控件用法总结

希望本文所述对大家Android程序设计有所帮助。

相关文章

  • android中图形图像处理之drawable用法分析

    android中图形图像处理之drawable用法分析

    这篇文章主要介绍了android中图形图像处理之drawable用法,较为详细的分析了Android中绘图所涉及的各种常用技巧与方法,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-10-10
  • Android网络访问之Retrofit使用教程

    Android网络访问之Retrofit使用教程

    Retrofit 是一个 RESTful 的 HTTP 网络请求框架的封装,网络请求的工作本质上是 OkHttp 完成,而 Retrofit 仅负责 网络请求接口的封装
    2022-12-12
  • Andriod事件分发事件由来初识

    Andriod事件分发事件由来初识

    这篇文章主要为大家讲解了Andriod事件分发事件由来的初步认识,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-03-03
  • Android Textview实现颜色渐变滚动效果

    Android Textview实现颜色渐变滚动效果

    这篇文章主要为大家详细介绍了Android Textview实现颜色渐变滚动效果,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-10-10
  • Android开发-之五大布局详解

    Android开发-之五大布局详解

    这篇文章主要介绍了Android开发-之五大布局详解,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2016-11-11
  • 用Kotlin打造一个Router的示例代码

    用Kotlin打造一个Router的示例代码

    本篇文章主要介绍了用Kotlin打造一个Router的示例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-05-05
  • android开发教程之卸载sd卡对MediaServer的处理

    android开发教程之卸载sd卡对MediaServer的处理

    Android中如果MediaServer访问SD卡上的音频文件,卸载SD卡的时候,就会kill掉MediaServer,卸载SD卡上必要条件就是没有进程访问SD卡上的资源文件。Kill掉MediaServer的进程后,MediaServer会重新启动。
    2014-02-02
  • Android GridView实现横向列表水平滚动

    Android GridView实现横向列表水平滚动

    这篇文章主要为大家详细介绍了Android GridView实现横向列表水平滚动,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-07-07
  • 浅谈Android串口通讯SerialPort原理

    浅谈Android串口通讯SerialPort原理

    这篇文章主要介绍了浅谈Android串口通讯SerialPort原理,文章围绕主题展开详细的内容介绍,具有一定的参考价值,需要的小伙伴可以参考一下
    2022-09-09
  • Android框架组件Lifecycle的使用详解

    Android框架组件Lifecycle的使用详解

    这篇文章主要介绍了Android框架组件Lifecycle的使用详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-07-07

最新评论