1.创建可被点击的TextView

1.1 在xml中创建可被点击的TextView

android:autoLink 是否将符合指定格式的文本转换成可单击的超链接。

属性值可以是如下几个属性值的一个或多个,多个属性值之间用竖线隔开。

  • none:不设置任何超链接。
  • web:将文本中的URL地址转换为超链接。
  • email:将文本中的email地址转换为超链接。
  • phone:
  • map:将文本中地址转换为超链接。
  • all:全部转换为超链接。
 <TextView
android:id="@+id/text1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:autoLink="all"
android:paddingBottom="8dp"
android:text="@string/link_text_auto"
android:textAppearance="?android:attr/textAppearanceMedium" />

link_text_auto

<string name="link_text_auto"><b>text1: Various kinds
of data that will be auto-linked.</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.
</string>

1.2 在代码调用setMovemoentMethod()方法设置超链接被点击:

  TextView t2 = (TextView) findViewById(R.id.text2);
t2.setMovementMethod(LinkMovementMethod.getInstance());
 <TextView
android:id="@+id/text2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="8dp"
android:paddingTop="8dp"
android:text="@string/link_text_manual"
android:textAppearance="?android:attr/textAppearanceMedium" />

link_text_manual

 <string name="link_text_manual"><b>text2: Explicit links using &lt;a&gt; markup.</b>
This has markup for a <a href="http://www.google.com">link</a> specified
via an &lt;a&gt; tag. Use a \"tel:\" URL
to <a href="tel:4155551212">dial a phone number</a>.
</string>

1.3 使用代码中使用html创建带超链接的文本

 TextView t3 = (TextView) findViewById(R.id.text3);
t3.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."));
t3.setMovementMethod(LinkMovementMethod.getInstance());

1.4 代码中不使用html创建带超链接的文本

SpannableString:http://orgcent.com/android-textview-spannablestring-span/http://developer.android.com/reference/android/text/style/CharacterStyle.html
        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); TextView t4 = (TextView) findViewById(R.id.text4);
t4.setText(ss);
t4.setMovementMethod(LinkMovementMethod.getInstance());

1.5 点击部分TextView跳转到另外一个Activity

有时候我们需要实现点击部分TextView跳转到另外一个Activity比如微博中的@功能。

通过跟踪TextView的源码,我们发现TextView支持的链接是由android.text.style.URLSpan这个类实现的,它重写了一个onClick方法:

    @Override
public void onClick(View widget) {
Uri uri = Uri.parse(getURL());
Context context = widget.getContext();
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
intent.putExtra(Browser.EXTRA_APPLICATION_ID, context.getPackageName());
context.startActivity(intent);
}

大家看到了吧startActivity,多么熟悉的方法。既然它能实现,为什么我们不能呢,答案是可以的。我们接着跟踪代码,可以看到URLSpan其实继承的是android.text.style.ClickableSpan,我们来看一下他的源码:

/**
* If an object of this type is attached to the text of a TextView
* with a movement method of LinkMovementMethod, the affected spans of
* text can be selected. If clicked, the {@link #onClick} method will
* be called.
*/
public abstract class ClickableSpan extends CharacterStyle implements UpdateAppearance { /**
* Performs the click action associated with this span.
*/
public abstract void onClick(View widget); /**
* Makes the text underlined and in the link color.
*/
@Override
public void updateDrawState(TextPaint ds) {
ds.setColor(ds.linkColor);
ds.setUnderlineText(true);
}
}

我们直接继承这个类,重写他的方法就可以了

实例:点击文本中的每一个字母跳转到另一个Activity并显示

效果:

点击The 则进入OtherActivity

自定义MyURLSpan

public class MyURLSpan extends ClickableSpan {

    private final String tag;

    public MyURLSpan(String tag) {
// TODO Auto-generated constructor stub
this.tag = tag;
} @Override
public void updateDrawState(TextPaint ds) {
//设置字体颜色
ds.setColor(Color.BLUE);
//去掉下划线
ds.setUnderlineText(false);
} @Override
public void onClick(View widget) {
Context context = widget.getContext();
Intent intent = new Intent(context, OtherActivity.class);
intent.putExtra("tag", tag);
context.startActivity(intent); } }

使用MyURLSpan

public class MainActivity extends Activity {
private final String str = "The Obama administration said Wednesday it would take action against the Syrian government even without the backing of allies or the United Nations because diplomatic paralysis must not prevent a response to the alleged chemical weapons attack"
+ " outside the Syrian capital last week "; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SpannableString ss = new SpannableString(str);
int fromIndex = 0;
while (str.indexOf(" ", fromIndex) != -1) {
int endIndex = str.indexOf(" ", fromIndex);
ss.setSpan(new MyURLSpan(str.substring(fromIndex, endIndex)), fromIndex, endIndex,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
fromIndex = endIndex + 1;
}
TextView tv = (TextView) findViewById(R.id.tv);
tv.setText(ss);
tv.setMovementMethod(LinkMovementMethod.getInstance());
} }

代码下载:http://url.cn/TLpCDZ

2.一些常用属性

android:ellipsize属性可支持如下几个属性值

  • none:不进行任何处理。
  • start:在文本开头部分进行省略。
  • middle:在文本中间部分进行省略。
  • end:在文本结尾处进行省略。
  • marquee:在文本结尾处以淡出的方式省略。
参考

http://zhangning290.iteye.com/blog/1134286

【Android Widget】1.TextView的更多相关文章

  1. 【Android Widget】FragmentTabHost

    android.support.v4包里面提供了FragmentTabHost用来替代TabHost,FragmentTabHost内容页面支持Fragment,下面我们就通过示例来看他的用法 效果图 ...

  2. 【Android Widget】2.ImageView

    1.属性详解 1.1 ScaleType属性详解 ImageView的Scaletype决定了图片在View上显示时的样子,如进行何种比例的缩放,及显示图片的整体还是部分,等等. 设置的方式包括: 1 ...

  3. 【Android - 框架】之GreenDao的使用

    上一篇博客([Android - 框架]之ORMLite的使用)中介绍了ORMLite的基本使用,今天我们来研究以下GreenDao的使用. GreenDao和ORMLite一样,都是基于ORM(Ob ...

  4. 【Android学习】《Android开发视频教程》第一季笔记

    视频地址: http://study.163.com/course/courseMain.htm?courseId=207001 课时5    Activity基础概念 1.Android开发技术结构 ...

  5. 【Android测试】【随笔】模拟双指点击

    ◆版权声明:本文出自胖喵~的博客,转载必须注明出处. 转载请注明出处:http://www.cnblogs.com/by-dream/p/5258660.html 手势 看到这个标题,很多人会想一想 ...

  6. 【Android测试】【随笔】模拟长按电源键

    ◆版权声明:本文出自胖喵~的博客,转载必须注明出处. 转载请注明出处:http://www.cnblogs.com/by-dream/p/5195121.html 起因 昨天群里看到有人问如何实现一个 ...

  7. 【Android - 框架】之Retrofit+RxJava的使用

    前几天分别对Retrofit和RxJava进行了总结,这个帖子打算把Retrofit结合RxJava使用的方法总结以下.有还不了解Retrofit或RxJava的朋友可以参考下面的帖子学习~ [And ...

  8. 【Android Studio】为Android Studio设置HTTP代理

    [Android Studio]为Android Studio设置HTTP代理   大陆的墙很厚很高,初次安装Android Studio下载SDK等必定失败,设置代理方法如下: 1. 到androi ...

  9. 【Android实战】----从Retrofit源代码分析到Java网络编程以及HTTP权威指南想到的

    一.简单介绍 接上一篇[Android实战]----基于Retrofit实现多图片/文件.图文上传中曾说非常想搞明确为什么Retrofit那么屌. 近期也看了一些其源代码分析的文章以及亲自查看了源代码 ...

随机推荐

  1. CentOs6系统安装mailx发邮件

    1. yum -y mail* sendmail* postfix* service sendmail start 2. cp /etc/mail.rc /etc/mail.rc.bak cat &g ...

  2. 应不应该使用inline-block代替float

    CSS布局创建网站,浮动绝对占据了很大的比例.大块区域如主内容及侧边栏,以及在其中的小块区域,都可以看到浮动的影子.这里浮动是唯一的解决方案吗? 浮动通常表现正常,但有时候搞起来会很纠结.特别是处理内 ...

  3. python 、mmap 实现内存数据共享

    import mmap mmap_file = None ##从内存中读取信息, def read_mmap_info(): global mmap_file mmap_file.seek(0) ## ...

  4. 关于开发环境中的消息在download时没有下载下来时的问题

    业务场景:在开发环境改了一些代码,现在需要将这些代码(包括class和数据库对象)移植到开发环境,整理出了Objectlist(就是该模块定义了哪些数据库对象),然后上传到FTP服务器上时,再执行do ...

  5. jquery、js获取页面高度宽度等

    jquery获取页面高度宽度 //获取浏览器显示区域(可视区域)的高度 : $(window).height(); //获取浏览器显示区域(可视区域)的宽度 : $(window).width(); ...

  6. PPT自动载入图片并矩阵分布

    最近有学生问到,能不能快速的向PPT一个页面里插入成百张图片,并让它们按统一大小的矩形排布到页面上.我写了以下代码可以在第1页中按照指定横向和纵向矩形数目,填充指定路径下的图片. Sub LoadPi ...

  7. 整合初步--------->SSH(注解版)

    上面的一篇博客已经介绍了 Spring和Hibernate之间的整合,没看过的童鞋可以去看看,这篇博客讲解Spring+Hibernate+Struts2注解版......... 个人觉得使用注解可能 ...

  8. navicat与phpmyadmin做mysql的自定义函数和事件

    自定义函数和事件是mysql一个很方便的功能,navicat在5.1以上版本就支持了自定义函数和事件,phpmyadmim不清楚. 用这个是由于一些简单的事情,没有必要去做一个服务器计划使用 接下来我 ...

  9. Spring基础学习(二)—详解Bean(上)

         在Spring配置文件中,用户不但可以将String.int等字面值注入Bean中,还可以将集合.Map等类型注入Bean中,此外还可以注入配置文件中其他定义的Bean. 一.字面值     ...

  10. 移植 DeepinQQ 到 Fedora 中

    本着自由/开源软件的分享精神创作此文,如有任何权力侵害请联系我,我将积极配合. 移植 DeepinQQ 到 Fedora 中 --也不知道是用移植还是迁移更合适 写在前面 首先,在这里要感谢武汉深之度 ...