Android TextView实现带链接文字事件监听的三种常用方式示例

所属分类: 软件编程 / Android 阅读数: 29
收藏 0 赞 0 分享

本文实例讲述了Android TextView实现带链接文字事件监听的三种常用方式。分享给大家供大家参考,具体如下:

/**
 * TextView实现文字链接跳转功能
 * @description:
 * @author ldm
 * @date 2016-4-21 下午4:34:05
 */
public class TextViewLinkAct extends Activity {
  private TextView tv_3;
  private TextView tv_4;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.link);
    setTextViewLink();
  }
  /**
   * 通过不同方式实现TextView中文字点击链接跳转功能
   *
   * @description:
   * @author ldm
   * @date 2016-4-21 下午4:24:13
   */
  private void setTextViewLink() {
    // 以Html格式href链接方式实现跳转
    tv_3 = (TextView) findViewById(R.id.text3);
    tv_3.setText(Html
        .fromHtml("<b>text3: Constructed from HTML programmatically.</b> Text with a "
            + "<a href=\"http://www.google.com\">link</a> "
            + "created in the Java source code using HTML."));
    tv_3.setMovementMethod(LinkMovementMethod.getInstance());
    // 通过SpannableString的setMovementMethod方法实现链接效果
    SpannableString ss = new SpannableString(
        "text4: Manually created spans. Click here to dial the phone.");
    ss.setSpan(new StyleSpan(Typeface.BOLD), 0, 30,
        Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    ss.setSpan(new URLSpan("tel:4155551212"), 31 + 6, 31 + 10,
        Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    tv_4 = (TextView) findViewById(R.id.text4);
    tv_4.setText(ss);
    tv_4.setMovementMethod(LinkMovementMethod.getInstance());
  }
}

布局文件

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="wrap_content" >
  <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:divider="?android:attr/listDivider"
    android:orientation="vertical"
    android:showDividers="middle" >
    <!-- 通过在布局中autoLink属性设置TextView的链接功能. -->
    <TextView
      android:id="@+id/text1"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:autoLink="all"
      android:paddingBottom="8dp"
      android:text="&lt;b>text1: Various kinds
   of data that will be auto-linked.&lt;/b> In
   this text are some things that are actionable. For instance,
   you can click on http://www.google.com and it will launch the
   web browser. You can click on google.com too. If you
   click on (415) 555-1212 it should dial the phone. Or just write
   foobar@example.com for an e-mail link. If you have a URI like
   http://www.example.com/lala/foobar@example.com you should get the
   full link not the e-mail address. Or you can put a location
   like 1600 Amphitheatre Parkway, Mountain View, CA 94043. To summarize:
   https://www.google.com, or 650-253-0000, somebody@example.com,
   or 9606 North MoPac Expressway, Suite 400, Austin, TX 78759."
      android:textAppearance="?android:attr/textAppearanceMedium" />
    <TextView
      android:id="@+id/text3"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:paddingBottom="8dp"
      android:paddingTop="8dp"
      android:textAppearance="?android:attr/textAppearanceMedium" />
    <TextView
      android:id="@+id/text4"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:paddingTop="8dp"
      android:textAppearance="?android:attr/textAppearanceMedium" />
  </LinearLayout>
</ScrollView>

其中通过在而已代码中android:autoLink属性的选项目有:none(无链接效果),web(网页链接),email(发邮件),phone(打电话),map(定位)及all(默认全都自动链接)。

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

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

更多精彩内容其他人还在看

Android中加入名片扫描功能实例代码

这篇文章主要介绍了Android中加入名片扫描功能实例代码的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

Android仿微信发表说说实现拍照、多图上传功能

这篇文章主要为大家详细介绍了Android仿微信发表说说实现拍照、多图上传功能,使用Retrofit2.0技术,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

设置Android系统永不锁屏永不休眠的方法

在进行Android系统开发的时候,有些特定的情况需要设置系统永不锁屏,永不休眠。本篇文章给大家介绍Android 永不锁屏,开机不锁屏,删除设置中休眠时间选项,需要的朋友一起学习吧
收藏 0 赞 0 分享

Android Retrofit 2.0框架上传图片解决方案

这篇文章主要介绍了Android Retrofit 2.0框架上传一张与多张图片解决方案,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android自定义等待对话框

这篇文章主要为大家详细介绍了Android自定义等待对话框的实现方法,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android中Window添加View的底层原理

这篇文章主要介绍了Android中Window添加View的底层原理,需要的朋友可以参考下
收藏 0 赞 0 分享

Android调用系统默认浏览器访问的方法

这篇文章主要介绍了Android调用系统默认浏览器访问的方法的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

Android开发退出程序的方法汇总

Android程序有很多Activity,比如说主窗口A,调用了子窗口B,子窗口B又调用子窗口C,back返回子窗口B后,在B中如何关闭整个Android应用程序呢? 下面脚本之家小编就给大家介绍android开发退出程序的几种方法,感兴趣的朋友参考下吧
收藏 0 赞 0 分享

Android程序开发中单选按钮(RadioGroup)的使用详解

在android程序开发中,无论是单选按钮还是多选按钮都非常的常见,接下来通过本文给大家介绍Android程序开发中单选按钮(RadioGroup)的使用,需要的朋友参考下吧
收藏 0 赞 0 分享

Android实现仿网易今日头条等自定义频道listview 或者grideview等item上移到另一个view中

这篇文章主要介绍了Android实现仿网易今日头条等自定义频道listview 或者grideview等item上移到另一个view中 的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多