Android—— TextView文字链接4中方法
转自: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中方法的更多相关文章
- Android TextView 文字居中
有2种方法可以设置TextView文字居中: 一:在xml文件设置:android:gravity="center" 二:在程序中设置:m_TxtTitle.setGravity( ...
- Android TextView文字描边的实现!!
Android开发:文字描边 转自:http://www.oschina.net/code/snippet_586849_37287 1. [代码][Java]代码 1 2 3 4 5 6 7 8 9 ...
- Android:TextView文字跑马灯的效果实现
解决TextView文字显示不全的问题. 简单设置跑马灯的效果: <TextView android:id="@+id/textView" android:layout_wi ...
- Android TextView文字过多时通过滚动条显示多余内容
方法一: TextView文字过多,显示不全,怎么办?我们可以为Textview添加滚动条. <TextView android:id="@+id/bus_detail_content ...
- [Android] TextView长按复制实现方法小结(转载)
这是别人写的,既然别人总结过了,那我就不花时间研究这个了,但往后会补充一些使用经验之类的 原文地址:http://blog.csdn.net/stzy00/article/details/414778 ...
- Android TextView文字横向自动滚动(跑马灯)
TextView实现文字滚动需要以下几个要点: 1.文字长度长于可显示范围:android:singleLine="true" 2.设置可滚到,或显示样式:android: ...
- Android TextView文字超出一屏不能显示其它的文字 解决方案
在android上面让TextView 过多的文字实现有滚动条,之前想简单了以为设置TextView的属性就可以实现,结果还是需要ScrollView配合使用,才能达到滚动条的效果有两种方式实现, 一 ...
- Android TextView文字透明度和背景透明度设置
textview1.setTextColor(Color.argb(255, 0, 255, 0)); //文字透明度 控件设为半透明: 控件名.getBackground().setAlpha(in ...
- android TextView 文字垂直的设置
<TextView android:id="@+id/tv_status" android:layout_width="wrap_content" and ...
随机推荐
- url-pattern / /*匹配
http://hi.baidu.com/atell/item/522112d3db45081fd90e44e1 struts2中配置为 <url-pattern>/*</url-pa ...
- DP较为完整的知识
数位DP 这类题,才刚刚接触,记得去年网络赛,就有道这样的题,我完全不会, 对于这类题基本方法是,是利用数的位数来构造转移方程. 下面给出两篇论文的链接: <数位计数问题解法研究> < ...
- PyCharm 默认运行 unittest
若文件里面有某个函数名称或模块名称以test为前缀,Pycharm的话,就会自动认为是单元测试: 报错信息:test_file() missing 1 required positional argu ...
- Python 的 pass 语句
Python pass是空语句,是为了保持程序结构的完整性. pass 不做任何事情,一般用做占位语句. 例子1: if __name__ == '__main__': pass 例子2: # 输出 ...
- 【LeetCode】39. Combination Sum (2 solutions)
Combination Sum Given a set of candidate numbers (C) and a target number (T), find all unique combin ...
- 【LeetCode】37. Sudoku Solver
Sudoku Solver Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are i ...
- EditPlus正则表达式中英文详解(附常用事例操作)
http://www.cnblogs.com/JustinYoung/articles/editplus_regular_expression.html EditPlus正则表达式中英文详解 \t T ...
- 基于NOPI的Execl模板转换类,直接将Execl模板转换对应的Entity
1.创建实体属性标记 public class CellAttribute : Attribute { /// <summary> /// /// </summary> /// ...
- Linux调度器 - 进程优先级
一.前言 本文主要描述的是进程优先级这个概念.从用户空间来看,进程优先级就是nice value和scheduling priority,对应到内核,有静态优先级.realtime优先级.归一化优先级 ...
- jsp指令和重定向
1 声明指令 格式:<%!声明变量或函数 %> 作用:会生成一个成员变量或成员方法,也可以使用访问修饰符修饰,public,private,protected 2 注释指令 格式:< ...