Android在源代码上添加功能,android源代码:TextView超链接功能

跟大家介绍android源代码关于TextVie的几个简单效果和功能

一.TextView显示自我介绍

有关TextView的属性大家可以看这里:

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

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

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

2.xml的开头写法

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

android:orientation="vertical"

android:layout_width="fill_parent"

android:layout_height="fill_parent">

其中:

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

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

text_view.xml 代码:

android:orientation="vertical"

android:layout_width="fill_parent"

android:layout_height="fill_parent">

android:id="@+id/name"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="姓名:Android淘气小公主"

android:textColor="#000000"

android:textSize="20sp"/>

android:id="@+id/sex"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="性别:女"

android:textColor="#000000"

android:textSize="20sp"/>

android:id="@+id/age"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="年龄:24"

android:textColor="#000000"

android:textSize="20sp"/>

android:id="@+id/xz"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="星座:巨蟹座"

android:textColor="#000000"

android:textSize="20sp"/>

android:id="@+id/xx"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="血型:O型"

android:textColor="#000000"

android:textSize="20sp"/>

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"/>

android:id="@+id/weibo"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="微博:@Android淘气小公主"

android:textColor="#000000"

android:textSize="20sp"/>

android:id="@+id/work"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="工作:Android自由开发者"

android:textColor="#000000"

android:textSize="20sp"/>

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

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

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

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这个类来帮我们完成任务。方法如下:

SpannableString spStr = new SpannableString(weibo.getText().toString());

spStr.setSpan(new URLSpan(""), 0, weibo.getText().toString().length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

weibo.setText(spStr); //修改textview内容

weibo.setMovementMethod(LinkMovementMethod.getInstance());

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

SpannableString spStr = new SpannableString(weibo.getText().toString());

Pattern pattern = Pattern.compile("@([^>]*?s)|@([^>]*)");

Matcher matcher = pattern.matcher(spStr);

while (matcher.find()) {

spStr.setSpan(new URLSpan(""), matcher.start(),matcher.end(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

weibo.setText(spStr);

weibo.setMovementMethod(LinkMovementMethod.getInstance());

更多关于android源代码的信息,可查询天地会

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值