之前一直疑惑android上的链接是怎么生成的,今天终于知道了,happy的同时,把这个小东西跟朋友们分享以下。
首先还是延续我一贯的作风,先贴出来布局页面的代码(linkifylayout.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">

<TextView android:id="@+id/myTextView1"
android:layout_width= "wrap_content"
android:layout_height="wrap_content">
</TextView>
<EditText android:id="@+id/myEditText1"
android:layout_width= "wrap_content"
android:layout_height="wrap_content">
</EditText>
</LinearLayout>

主程序LinkifyTextView.java
package test.shi;

import android.app.Activity;
import android.os.Bundle;
import android.text.util.Linkify;
import android.view.KeyEvent;
import android.view.View;
import android.widget.*;

public class LinkifyTextView extends Activity {

private TextView myTextView1;
private EditText myEditText1;

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.linkifylayout);

myTextView1=(TextView)findViewById(R.id.myTextView1);
myEditText1=(EditText)findViewById(R.id.myEditText1);

myEditText1.setOnKeyListener(new EditText.OnKeyListener()
{

@Override
public boolean onKey(View v, int arg1, KeyEvent arg2) {
  // TODO Auto-generated method stub

myTextView1.setText(myEditText1.getText());

//此处是关键,感应myTextView1是web链接还是email链接,或者是电话号码
Linkify.addLinks
(
myTextView1,Linkify.WEB_URLS|
Linkify.EMAIL_ADDRESSES|
Linkify.PHONE_NUMBERS
);

return false;
}

});
}

}

除了使用Linkify之外,还可以在linkifylayout.xml文件中设置TextView的android:autoLink实现这种HyperLink的效果。如下:

<TextView android:id="@+id/myTextView1"
android:layout_width= "wrap_content"
android:layout_height="wrap_content"
android:autoLink="web|email|phone">
</TextView>
通过在程序中设置
myTextView1.setText(“http://www.google.cn”);
myTextView1.setText(“13512345678”);
myTextView1.setText(“hello@126.com”);
这样运行时myTextView1就会生成链接,当用户点击时,会自动调用系统的Intent,以浏览器打开网址、发送邮件或拨打电话。

转载于:https://blog.51cto.com/lovely1133/579590

android Linkify的用法相关推荐

  1. Android之Adapter用法总结

    本文转自http://kb.cnblogs.com/a/2328334/,转载请注明原出处. Android之Adapter用法总结 作者:Devin Zhang  来源:博客园  发布时间:2012 ...

  2. Android的Adapter用法总结

    Android之Adapter用法总结 1.Adapter概念   定义为将一个类的接口变换成客户端所期待的一种接口,从而使原本因接口不匹配而无法在一起工作的两个类能够在一起工作.   在androi ...

  3. Android webservice的用法详细讲解

    Android webservice的用法详细讲解 看到有很多朋友对WebService还不是很了解,在此就详细的讲讲WebService,争取说得明白吧.此文章采用的项目是我毕业设计的webserv ...

  4. android room表关联,Android Room的用法

    Android Room的用法 SQLite是Android内置的轻量级关系型数据库,但直接使用SQLite core包做数据库操作有以下劣势:需要编写长且重复的代码,这会很耗时且容易出错. 管理SQ ...

  5. android图片显示组件,Android可循环显示图像的Android Gallery组件用法实例

    本文实例分析了Android可循环显示图像的Android Gallery组件用法.分享给大家供大家参考,具体如下: Gallery组件主要用于横向显示图像列表,不过按常规做法.Gallery组件只能 ...

  6. Android RxJava 基本用法

    Android RxJava 基本用法 RxJava 使用函数响应式编程方式,它可以简化项目,处理嵌套回调的异步事件. RxJava 依赖 这里以 RxJava 2.2.1 为例.在 build.gr ...

  7. Android中富文本用法包括点击事件处理

    Android中富文本用法包括点击事件处理 关于 只需要富文本展示效果,不需要点击事件 展示富文本加点击事件 关于   最近项目需要中需要用到富文本展示及富文本点击功能,先去百度了一下,大多数的说法是 ...

  8. Android中list常用方法,Android中ListActivity用法实例分析

    本文实例分析了Android中ListActivity用法.分享给大家供大家参考,具体如下: 程序如下: import android.app.ListActivity; import android ...

  9. android默认exported_Android android:exported = true 用法详解

    Android android:exported = true 用法详解 Android相关属性的介绍:android:exported = true 在Activity中该属性用来标示:当前Acti ...

最新文章

  1. 阿里巴巴 Java 开发手册之编程规约(一)-------我的经验
  2. python自动控制windows、Android的软件用来实现机器人流程自动化--RPA,外挂、自动化测试等方面的解决方案
  3. SQL语言之用户(Oracle)
  4. BROCADE 300和MD3200扩展柜FC SAN,截图
  5. 飞书×帆软数知鸟 | 飞书上的一站式需求管理
  6. 语音交互有哪些优势与劣势?
  7. php悬浮框,PopupWindow(悬浮框)的基本使用
  8. java-通过JDBC操作数据库
  9. jupyter 写sql_了解适用于SQL Server的Jupyter Notebooks
  10. pandas —— pd.read_csv 与df.to_csv
  11. so easy 的弹出层——使用jquery
  12. 浅谈C语言字符串结束符'\0'
  13. VS编译器各版本代号
  14. 器件基础知识——电容
  15. 十六进制转float
  16. 电脑屏幕亮度变暗、动态对比度问题,解决方法汇总
  17. 剑网三客户端修复连接服务器失败,剑网3客户端异常 无法打开处理解决办法
  18. 去掉选中div后出现的高亮淡蓝色边框
  19. C/C++基础 hypot函数
  20. 先行试点,创新改造:中信期货关键业务系统自主可控的实践之路

热门文章

  1. 使用 CSS3 实现 3D 图片滑块效果
  2. Android源码设计模式解析与实战(四)
  3. (原)caffe在ubuntu中设置GPU的ID号及使用多个GPU
  4. [Android] 环境优化配置Android Studio发展NDK
  5. VS2010 error LNK2019: 无法解析的外部符号
  6. 转:PHP中文乱码问题
  7. 重装系统后,腾讯TM2013 “应用程序无法启动,因为程序的并行配置不正确
  8. 将勾选数据从dataset中筛选出来
  9. 大数据和人工智能,金融产业的创新发展通道
  10. canvas图形绘制