您的位置:首页 > 移动开发 > Android开发

android应用开发全程实录-用户界面部分章节-你真的会用最简单的TextView么?

2011-11-24 09:22 295 查看
TextView为文本控件,在前面的学习中多次用到,可能读者已经对它的用法有所了解了,这里再单独介绍一下,加深理解,因为TextView是一个非常重要和常用的控件。

我们通常在xml布局文件中声明一个TextView,代码如下:

<TextView
android:id="@+id/tv"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>


通常在Activity类的onCreate方法中初始化TextView,并且设置相关属性。代码如下所示:

package com.pms.tvdemo;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.TextView;

publicclassMyTextViewextends Activity {

private TextView mTextView;//声明TextView对象

/** Called when the activity is first created. */
@Override
publicvoid onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

mTextView = (TextView)findViewById(R.id.tv);//取得TextView对象
mTextView.setText("我的第一个文本");//设置TextView显示的内容
mTextView.setTextColor(Color.GREEN);//设置TextView的颜色
mTextView.setBackgroundColor(Color.WHITE);//设置TextView的背景色

}
}


当然也可以在xml中设置TextView的相关属性,表4-1列出了几个常用的方法以及对应的xml属性。

TextView对象方法及对应的xml属性

方法

xml属性

setText

android:text

setTextSize

android:textSize

setTextColor

android:textColor

setBackgroundResource

android:background

在上面的代码中用到了Color.Green来设置TextView的颜色,用Color.WHITE来设置TextView的背景色。

Android中提供了如下常见颜色:

Color.BLACK;

Color.BLUE;

Color.CYAN;

Color.DKGRAY;

Color.GRAY;

Color.GREEN;

Color.LTGRAY;

Color.MAGENTA;

Color.RED;

Color.TRANSPARENT。

除此之外,当文字中出现URL、E-mail、电话号码等的时候,还可以为TextView设置链接。总结起来,一共有4种方法来为TextView实现链接。

(1)在xml里添加android:autoLink属性。如果写为android:autoLink=”all”,则为所有种类添加链接。当然,同样的也可以在Java代码中完成,用法为tv.setAutoLinkMask(Linkify.ALL)。

(2)将显示内容写到资源文件,一般为String.xml中,并且用<a>标签来声明链接,但是这还不够,要激活这个链接,需要在Java代码中使用setMovementMethod()方法设置TextView可点击。

(3)用Html类的fromHtml()方法格式化要放到TextView里的文字。

(4)用Spannable或实现它的类,如SpannableString。与其他方法不同的是,Spannable对象可以为个别字符设置链接(当然也可以为个别字符设置颜色、字体等,实现某些字符高亮显示的效果等)。

看一个实例:

package com.pms.tvdemo;

import android.app.Activity;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.text.Html;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.method.LinkMovementMethod;
import android.text.style.BackgroundColorSpan;
import android.text.style.StyleSpan;
import android.text.style.URLSpan;
import android.widget.TextView;

publicclass MyTextView extends Activity {

/** Called when the activity is first created. */
@Override
publicvoid onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

TextView t2 = (TextView) findViewById(R.id.tv02);
t2.setMovementMethod(LinkMovementMethod.getInstance());       //设置TextView可点击,第二种方式实现

TextView t3 = (TextView) findViewById(R.id.tv03);
t3.setText(
Html.fromHtml(
            "<b>text3:</b> " +
            "<a href=\"http://www.google.com\">链接到google</a> " +
            "使用HTML在java代码中实现"));
        t3.setMovementMethod(LinkMovementMethod.getInstance());        //第三种方式实现

//创建一个 SpannableString对象
SpannableString ss = new SpannableString(
      "text4: 点击这里拨打电话,点击这里链接到google");
ss.setSpan(new StyleSpan(Typeface.BOLD), 0, 6,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);    //设置0~6字符为粗体
ss.setSpan(new URLSpan("tel:4155551212"), 9, 11,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);    //设置9~11字符为拨号链接
ss.setSpan(new URLSpan("http://www.google.com"), 18, 20,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);//设置18~20为网站链接
ss.setSpan(new BackgroundColorSpan(Color.RED), 23 ,29,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);    //设置  23~29字符为红色高亮
TextView t4 = (TextView) findViewById(R.id.tv04);
t4.setText(ss);//SpannableString对象设置给TextView
t4.setMovementMethod(LinkMovementMethod.getInstance());//第四种实现方式

}
}


一共定义了4种实现方法,其中第二种方法一定要将文字内容写在资源文件中,写在Java代码中是无法实现的。

main.xml:

<?xmlversion="1.0"encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<!-- text1 通过xml属性设置自动实现超链接 -->
<TextView
android:id="@+id/tv01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:autoLink="all"
android:text="@string/first_link"
/>
<!-- text2 通过含有<a>标签的string资源文件实现超链接 -->
<TextView
android:id="@+id/tv02"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/second_link"
/>
<!-- text3 通过在java代码中使用html来实现超链接 -->
<TextView
android:id="@+id/tv03"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<!-- text4 通过java代码实现。但是不用html-->
<TextView
android:id="@+id/tv04"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>


可以发现,第一种方式在TextView标签中加入android:auto=“all”属性来实现了链接。

String.xml:

<?xmlversion="1.0"encoding="utf-8"?>
<resources>
<stringname="hello">Hello World, MyTextView!</string>
<stringname="app_name">TextView Demo</string>
<stringname="first_link"><b>text1:</b>这是用第一种方式实现的超链接。你只要点击<!-- <b>标签设置粗体 -->


</string>
<stringname="second_link"><b>text2:</b>这是第二种方式实现的超链接,用一个<a>标签来声明。<ahref="http://www.google.com">链接到google</a><!-- <a>用了尖括号的转义字符 -->
用”tel“就可以链接到拨号<ahref="tel:123456789">拨打电话</a>.
</string>
</resources>


点击http://www.google.com ,就会进入浏览器打开该网页,点击google.com也行。点击15145625689就会拨打电话。

运行效果如图4-1所示。



(以上是书中对最简单也最常用的TextView的介绍)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: