转自: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. PHP-各种下载安装

    Windows下常用PHP扩展下载: http://dev.freshsite.pl/php-accelerators.html http://windows.php.net/downloads/pe ...

  2. BZOJ 2466 中山市选2009 树 高斯消元+暴力

    题目大意:树上拉灯游戏 高斯消元解异或方程组,对于全部的自由元暴力2^n枚举状态,代入计算 这做法真是一点也不优雅... #include <cstdio> #include <cs ...

  3. python标准库介绍——1 os详解

    == os 模块 == ``os`` 模块为许多操作系统函数提供了统一的接口. 这个模块中的大部分函数通过对应平台相关模块实现, 比如 ``posix`` 和 ``nt. os`` 模块会在第一次导入 ...

  4. java日志-纯Java配置使用slf4j配置log4j(转)

    工程目录如下 代码里面用的是slf4j,但是想要用log4j来管理日志,就得添加slf4j本来的jar,然后添加log4j和slf4j箱关联的jar即可. 如果是maven项目的话添加下面的依赖即可 ...

  5. 依据经纬度返回地址的url -- GoogleMap

    latlng=34,112">https://maps.googleapis.com/maps/api/geocode/xml? latlng=34,112

  6. Windows Azure Mobiles Services实现client的登录注冊

    下文仅仅是简单实现,client以Android端的实现为例: 用户表Account: package com.microsoft.ecodrive.model; public class Accou ...

  7. CDH5.2+CM5.2+impala2+Spark1.1 集群搭建基础环境准备

    測试集群简单介绍:一共同拥有4台机器:10.10.244.136.10.10.244.137.10.10.244.138.10.10.244.139. 10.10.244.136是管理节点.另外3台是 ...

  8. 【Android】3.7 UI控制功能

    分类:C#.Android.VS2015.百度地图应用: 创建日期:2016-02-04 一.简介 简介:介绍开关手势功能和显示隐藏UI控件 详述: (1)地图操作开关:平移.缩放.双击放大.双指操作 ...

  9. 错误地使用catch

    try { // do something } catch (Exception e) { } 错误:这里,catch了Exception,但是在catch中什么动作都没做,那么所有的Exceptio ...

  10. HttpClient和HttpURLConnection的区别

    总结了网上的一些资源,主要有以下两个观点: 分析一: 在研究Volley框架的源码中,发现它在HTTP请求的使用上比较有意思,在Android 2.3及以上版本,使用的是HttpURLConnecti ...