码迷,mamicode.com
首页 > 移动开发 > 详细

Android TextView中展示含有html标签的文字

时间:2015-01-23 18:31:59      阅读:230      评论:0      收藏:0      [点我收藏+]

标签:android   textview html   

两个小实例:

1、服务端返回的带有html标签的文字,在textview中展示的时候,能正确的换行。

2、需要展示的下载的超链接,以文字代替,隐藏下载地址:

实现效果如下:

技术分享

代码如下:

package com.android.study;

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

public class FourthActivity extends Activity {
	private TextView textView;
	private TextView textUrl;
	private TextView textUrl2;
	
	// html格式换行
	private static String msgHtml = "1、在任务结束时间之前完成任务并上传数据。<br />2、任务内容:连续2天达到10000步。<br />3、您可以在下方的进度显示查看您的任务进度情况。";
	// 超链接
	private static String html = "我的客户端,下载地址:<a href='http://www.baidu.com'>点击下载</a>";

	private static String html2 = "我的客户端,下载地址:";
	private static String html22 = "<a href=\""
			+ "http://www.baidu.com" + "\">"
			+ "点击下载" + "</a> ";
	CharSequence charSequence = Html.fromHtml(html);

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_texttest);

		init();
	}

	private void init() {

		// textView.setAutoLinkMask(Linkify.ALL);
		textView = (TextView) findViewById(R.id.textView);
		textUrl = (TextView) findViewById(R.id.textUrl);
		textUrl2 = (TextView) findViewById(R.id.textUrl2);

		/**
		 * html格式换行
		 */
		textView.setText(Html.fromHtml(msgHtml));

		/**
		 * 超链接: 法一
		 */
		textUrl.setText(charSequence);
		textUrl.setMovementMethod(LinkMovementMethod.getInstance());

		/**
		 * 超链接: 法二
		 */
		textUrl2.setText(html2);
		textUrl2.append(Html.fromHtml(html22));
		textUrl2.setMovementMethod(LinkMovementMethod.getInstance());
	}
}

超链接形式两者在该Demo中都可以实现效果。

但法一在我的项目代码中使用不成功,换了法二后可以了。

有知道该情况的可以留言噢。

Android TextView中展示含有html标签的文字

标签:android   textview html   

原文地址:http://blog.csdn.net/u012440207/article/details/43057341

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!