Android TextView与SpannableString讲解

一.TextView显示自我介绍

有关TextView的属性大家可以看这里:http://bbs.9tech.cn/topic-364380-1.html

textview这个控件就是用来显示文字的。我们在Eclipse中打开上一节建立的工程part1(大家也可以新建一个part2都是可以的)。

 

1.打开text_view.xml 我们来做第一个布局页面

这里跟大家说声,为了自己的应用程序能在更多的手机上使用,一般我们选用的SDK是2.1,我的教程里面是用SDK2.2的。

 

2.xml的开头写法

有的Eclipse里的ADT版本不同写法也不同,所以我采用一种通用的方法。

1
2
3
4
5
6
7
<?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">
<!--在这里添加控件-->
</LinearLayout> 

 

 

其中:

1
android:orientation="vertical"这个属性是设置你布局里的控件是要垂直显示的,horizontal值是水平显示。

 

我们要添加的控件是在<LinearLayout>节点下的。节点开始是<>以</>作为结束。每对节点都是有头有尾的,里面在包含的节点我们称之为他的子节点。

text_view.xml 代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
<?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">
<TextView
    android:id="@+id/name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="姓名:Android淘气小公主"
    android:textColor="#000000"
    android:textSize="20sp"/>
<TextView
    android:id="@+id/sex"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="性别:女"
    android:textColor="#000000"
    android:textSize="20sp"/>
<TextView
    android:id="@+id/age"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="年龄:24"
    android:textColor="#000000"
    android:textSize="20sp"/>
<TextView
    android:id="@+id/xz"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="星座:巨蟹座"
    android:textColor="#000000"
    android:textSize="20sp"/>
<TextView
    android:id="@+id/xx"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="血型:O型"
    android:textColor="#000000"
    android:textSize="20sp"/>
<TextView
    android:id="@+id/boke"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="博客:http://blog.9tech.cn/emaoer"
    android:textColor="#000000"
    android:textSize="20sp"/>
<TextView
    android:id="@+id/weibo"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="微博:@Android淘气小公主"
    android:textColor="#000000"
    android:textSize="20sp"/>
<TextView
    android:id="@+id/work"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="工作:Android自由开发者"
    android:textColor="#000000"
    android:textSize="20sp"/>
</LinearLayout>

3.在TextViewActivity.java文件里我们要做的

首先,我们要知道程序在创建Activity的时候就要走他的onCreat()方法,所以我们要添加他的onCreat()方法。

然后我们要在他的方法里去声明控件的id,这点很总要。尤其,是要监听的控件必须声明Android是有R文件的,R文件里的东西我们是不可以修改的。

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package com.tech.part2;
 
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
 
public class TextViewActivity extends Activity {
    private TextView name,sex,age,xz,xx,weibo,boke,work;
     
 @Override
protected void onCreate(Bundle savedInstanceState) {
     // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.text_view);//此方法是声明你的布局文件
    //注册声明控件
    name=(TextView)findViewById(R.id.name);
    sex=(TextView)findViewById(R.id.sex);
    age=(TextView)findViewById(R.id.age);
    xz=(TextView)findViewById(R.id.xz);
    xx=(TextView)findViewById(R.id.xx);
    weibo=(TextView)findViewById(R.id.weibo);
    boke=(TextView)findViewById(R.id.boke);
    work=(TextView)findViewById(R.id.work);
     
}
}

 

效果图:

 

 二 TextView超链接功能

 

1.把地址直接编程超链接

在TextView控件属性中,添加android:autoLink="all"属性。

 

2.文字添加超级链接

我们可以用SpannableString这个类来帮我们完成任务。方法如下:

1
2
3
4
SpannableString spStr = new SpannableString(weibo.getText().toString());
spStr.setSpan(new URLSpan("http://weibo.com/emaoer520"), 0, weibo.getText().toString().length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
weibo.setText(spStr);  //修改textview内容
weibo.setMovementMethod(LinkMovementMethod.getInstance());

 

 

效果截图:

 

显然这个还不是理想效果的如何,从截图来看大家可以看到,博客下面也划线了。怎样处理呢?我们来用正则表达式吧。

1
2
3
4
5
6
7
SpannableString spStr = new SpannableString(weibo.getText().toString());
Pattern pattern = Pattern.compile("@([^>]*?s)|@([^>]*)");
Matcher matcher = pattern.matcher(spStr);
while (matcher.find()) {
spStr.setSpan(new URLSpan("http://weibo.com/emaoer520"), matcher.start(),matcher.end(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);  
weibo.setText(spStr);
weibo.setMovementMethod(LinkMovementMethod.getInstance());
posted @ 2013-12-20 10:16  一个学渣  阅读(554)  评论(0编辑  收藏  举报

 

CSDNGitHub 联系邮箱:aikongmeng@gmail.com


Copyright 2008-2019 Government All Rights Reserved