让android中Textview支持丰富的文本和样式

直接看效果:

其中点击  Email自动跳转到发送Email界面,点击电话,自动拨打电话等功能。

布局如下:mutli_textview

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="match_parent"
  android:layout_height="match_parent">
    
    <TextView  
    	 android:id="@+id/tv1"
    	 android:textSize="18dp"
    	 android:layout_width="wrap_content" 
   	android:layout_height="wrap_content"
   	android:autoLink="all" 
    	 android:text="@string/hello"
    />
    <TextView  
    	 android:textSize="18dp"
     	 android:id="@+id/tv2"
    	 android:layout_width="wrap_content" 
           android:layout_height="wrap_content" 
    	 android:text="@string/hello"
    	 android:autoLink="all"
    />
</LinearLayout>


代码如下:

package geo.test.toeditxml;

import android.app.Activity;
import android.os.Bundle;
import android.text.Html;
import android.text.Spanned;
import android.text.method.LinkMovementMethod;
import android.widget.TextView;

public class JustFortestXmlActivity extends Activity
{
	private TextView tv1;
	private TextView tv2;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.mutli_textview);
		tv1 = (TextView) findViewById(R.id.tv1);
		tv2 = (TextView) findViewById(R.id.tv2);
		String strHtml = "<font color='red' >lixiaodaoaaa is a good boy</font></br>";
		strHtml += "<big><a  href='http://t.qq.com/lixiaodaoaaa'>欢迎访lixiaodaoaaa 的腾讯微博</a></big>";
		String str2 = "我的空间地址是URL:http://blog.csdn.net/lixiaodaoaaa\n";
		str2 += "我的email:lixiaodaoaaa@qq.com\n";
		str2 += "我的电话是:+86 010-89497392";
		tv1.setText(Html.fromHtml(strHtml));
		tv1.setMovementMethod(LinkMovementMethod.getInstance());
		tv2.setText(str2);
		tv2.setMovementMethod(LinkMovementMethod.getInstance());
	}
}


这里非常重要:tv1.setMovementMethod(LinkMovementMethod.getInstance());支持LInkMoveMent的动作。

这里也很重要(Html.fromHtml(strHtml));,将Html代码转换成丰富的文本进行显示。!

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值