转自:http://ghostfromheaven.iteye.com/blog/752181

Android 的实现TextView中文字链接的方式有很多种。 
总结起来大概有4种: 
1.当文字中出现URL、E-mail、电话号码等的时候,可以将TextView的android:autoLink属性设置为相应的的值,如果是所有的类型都出来就是android:autoLink="all"。当然也可以在java代码里做,textView01.setAutoLinkMask(Linkify.ALL); 
2.将要处理的文字写到一个资源文件,如string.xml,然后的java代码里引用(直接写在代码了是不可行的,会直接把文字都显示处理) 
3.用Html类的fromHtml()方法格式化要放到TextView里的文字 
4.用Spannable或实现它的类,如SpannableString来格式,部分字符串。

当然以上各种方法,不要在java代码里加上textView03.setMovementMethod(LinkMovementMethod.getInstance());这样的代码,否则无效。 

java代码

import android.app.Activity;
import android.graphics.Typeface;
import android.os.Bundle;
import android.text.Html;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.method.LinkMovementMethod;
import android.text.method.ScrollingMovementMethod;
import android.text.style.StyleSpan;
import android.text.style.URLSpan;
import android.text.util.Linkify;
import android.widget.TextView; public class LinkTest extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main); TextView textView01 = (TextView) findViewById(R.id.textView01);
textView01.setAutoLinkMask(Linkify.ALL);
String autoLinkText = "http://student.csdn.net/?232885我的CSDN博客 ";
textView01.setText(autoLinkText); TextView textView02 = (TextView) findViewById(R.id.textView02);
//String aLinkText = "<a href=/"http://student.csdn.net/?232885/">我的CSDN博客 </a>"
// + "<a href=/"tel:4155551212/">and my phone number</a>";
//textView02.setText(aLinkText);
textView02.setMovementMethod(LinkMovementMethod.getInstance()); TextView textView03 = (TextView) findViewById(R.id.textView03); String htmlLinkText = "<a href=/"http://student.csdn.net/?232885/"><u>我的CSDN博客 </u></a>";
textView03.setText(Html.fromHtml(htmlLinkText));
textView03.setMovementMethod(LinkMovementMethod.getInstance()); TextView textView04 = (TextView) findViewById(R.id.textView04);
SpannableString ss = new SpannableString("call: 4155551212.");
ss.setSpan(new StyleSpan(Typeface.BOLD), 0, 5, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
ss.setSpan(new URLSpan("tel:4155551212"), 6, 16, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
textView04.setText(ss);
textView04.setMovementMethod(LinkMovementMethod.getInstance()); }
}

string.xml文件

<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, LinkTest!</string>
<string name="app_name">LinkTest</string>
<string name="aLinkText">
<a href="http://student.csdn.net/?232885">我的CSDN博客 </a>
</string>
</resources>

main.xml文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:autoLink="all"
>
<TextView
android:id="@+id/textView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/textView02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/aLinkText"
/>
<TextView
android:id="@+id/textView03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/textView04"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>

Android—— TextView文字链接4中方法的更多相关文章

  1. Android TextView 文字居中

    有2种方法可以设置TextView文字居中: 一:在xml文件设置:android:gravity="center" 二:在程序中设置:m_TxtTitle.setGravity( ...

  2. Android TextView文字描边的实现!!

    Android开发:文字描边 转自:http://www.oschina.net/code/snippet_586849_37287 1. [代码][Java]代码 1 2 3 4 5 6 7 8 9 ...

  3. Android:TextView文字跑马灯的效果实现

    解决TextView文字显示不全的问题. 简单设置跑马灯的效果: <TextView android:id="@+id/textView" android:layout_wi ...

  4. Android TextView文字过多时通过滚动条显示多余内容

    方法一: TextView文字过多,显示不全,怎么办?我们可以为Textview添加滚动条. <TextView android:id="@+id/bus_detail_content ...

  5. [Android] TextView长按复制实现方法小结(转载)

    这是别人写的,既然别人总结过了,那我就不花时间研究这个了,但往后会补充一些使用经验之类的 原文地址:http://blog.csdn.net/stzy00/article/details/414778 ...

  6. Android TextView文字横向自动滚动(跑马灯)

    TextView实现文字滚动需要以下几个要点:   1.文字长度长于可显示范围:android:singleLine="true"   2.设置可滚到,或显示样式:android: ...

  7. Android TextView文字超出一屏不能显示其它的文字 解决方案

    在android上面让TextView 过多的文字实现有滚动条,之前想简单了以为设置TextView的属性就可以实现,结果还是需要ScrollView配合使用,才能达到滚动条的效果有两种方式实现, 一 ...

  8. Android TextView文字透明度和背景透明度设置

    textview1.setTextColor(Color.argb(255, 0, 255, 0)); //文字透明度 控件设为半透明: 控件名.getBackground().setAlpha(in ...

  9. android TextView 文字垂直的设置

    <TextView android:id="@+id/tv_status" android:layout_width="wrap_content" and ...

随机推荐

  1. ASP.NET#LinqDataSource控件配置对象模型时遇到的问题

    使用LinqDataSource控件时,配置数据源的时候,发现没有DataContext对象可选,但已通过可视化操作完成了对象模型的建立.这个时候,可以通过现在Default.aspx.cs文件中做如 ...

  2. static不实现多态

    class Father { public static String getName() { return "father"; } } class Children extend ...

  3. JS版的Server.UrlEncode

    <script>function (str) {//标准UrlEncode.execScript("function reHex(str)\reHex=hex(asc(str)) ...

  4. supervisor介绍与安装

    前言 今天同事让我帮忙安装一个叫supervisor的软件,但自己确实没接触过这个软件 自己做一下学习的记录 我首先是查询了一下supervisor的官网,初步认识一下这个软件 Supervisor是 ...

  5. 给maven仓库添加镜像

    用maven下载好慢啊,快试试阿里云镜像吧!修改$MAVEN_HOME/conf/settings.xml <!-- 阿里云仓库 --> <mirror> <id> ...

  6. web项目在eclipse workspace中的位置

    workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\项目名

  7. C#基础 - C# 的 常见概念简述

    在上篇文章中,你跟着我写了一个HelloWorld,本篇中,我们来谈谈一些C#程序中的小概念 1.C# 程序结构 一个 C# 程序主要包括以下部分: 命名空间声明(Namespace declarat ...

  8. go 学习 ---package

    1.包简述 GO本身没有项目的概念,只有包,包括可执行包和不可执行包,而不管什么包,都应该包含在 $GOPATH/src 目录下,GO命令和编译器会在 $GOPATH/src 目录下搜索相应的包.比如 ...

  9. Linux时间子系统(十四) tick broadcast framework

    一.前言 在内核中,有cpuidle framework可以控制cpu的节电:当没有进程调度到该cpu上执行的时候,swapper进程粉墨登场,将该cpu会被推入到idle状态.当然CPU的idle状 ...

  10. MAC 上搭建lua

    下载和安装lua:(转自这里) 1.  下载最新版的lua-5.3.1.tar.gz 请点击(http://www.lua.org/ftp/),然后解压  2. 执行"终端"进入到 ...