控件TextView的用法

本文介绍在Android中如何为TextView设置链接,包括四种方法:XML属性、资源文件、HTML格式化和SpannableString。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、TextView一些方法与属性

TextView对象方法及应的xml属性

方法xml属性
setTextandroid:text
setTextSizeandroid:textSize
setTextColorandroid:textColor
setBackgroundResourceandroid:background

2、 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对象可以为个别字符设置链接(当然也可以为个别字符设置颜色、字体等,实现某些字符高亮显示的效果等)。

3、实例讲解

*************

java代码

***********

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;

public class MyTextView extends Activity {
	
	/** Called when the activity is first created. */
    @Override
    public void 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());//第四种实现方式
        
        
        
        
    }
}


***********

String.xml

**********

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello">Hello World, MyTextView!</string>
    <string name="app_name">TextView Demo</string>
    <string name="first_link"><b>text1:</b>这是用第一种方式实现的超链接。你只要点击<!-- <b>标签设置粗体 -->
      http://www.google.com , 就会进入浏览器打开该网页,点击 google.com 也行。点击15145625689
                就会拨打电话。
    </string>
    <string name="second_link"><b>text2:</b> 这是第二种方式实现的超链接,用一个
      <a> 标签来声明。<a href="http://www.google.com">链接到google</a> <!-- <a>用了尖括号的转义字符 -->
                 用”tel“就可以链接到拨号 <a href="tel:123456789">拨打电话</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"
    >    
  <!-- 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>

********************

运行结果

********************

总结:

方法:setMovementMethod(LinkMovementMethod.getInstance());//设置TextView可点击链连

类:SpannableString可实现文字的属性

OK,呵呵就写到这里了,希望对大家也有所帮助

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值