Android中的android.text.Html类学习(补全了TagHandler部分)

原来只知道这个类可以转换一些Html的标签,常用的也就是fromHtml方法。但是之前一直不太明白这个方法的应用场景,最近通过项目的实践,了解到了strings.xml中使用Html样式的文案信息,然后通过更换颜色和字体样式等方式,就能完成在一个TextView为文案内容设置多种样式的需求,这样做还能减少layout中的组件使用数量,可以通过很少的组件就完成需求的页面。

常用方法:

1.格式化字符串成Html样式

publicstatic Spanned fromHtml(String source)

public static Spanned fromHtml(String source, ImageGetter imageGetter, TagHandler tagHandler)

以上两个方法都是通过String内容转换成Html样式的内容,然后就可以赋值给TextView中。

第二个方法和第一个的区别在于第二个可以传入图片,但是需要注意这个图片需要重写ImageGetter才可以支持网络图片获取。

也可以重写ImageGetter来获取本地的图片,或者传入drawable的id来获取drawable对象,这样的做法可以用来实现内容包含表情的功能开发。

例如:聊天中输入的editText和显示的textView中通过这样的方式获取表情图片。

2.public static String toHtml(Spanned text)

将spanned转换String

3.常用的Html标签和css:div,font,color,size,align,center,left,right(其中color可以传入十六进制,但是注意转义字符)

4.TagHandler的作用(特别像AngularJs中的自定义标签)

就像注释中的说明一样,这个对象就是一个所有标签的过滤器,可以通过重写对象中的

public void handleTag(boolean opening, String tag, Editable output, XMLReader xmlReader) 

方法来重新自定义标签中的样式,例如我在例子中自定义了一个hello的标签,然后通过自定义的TagHandler把hello标签替换成了一个字符串,其实我想到的可以通过自定义标签和属性来定义你的文字样式,然后加到spanned中。

同时这个过滤不仅对不知道标签过滤,已有的标签依旧会走这个handler,所以可以对原有的一些样式就行修饰。

源码备忘:

<span style="white-space:pre">	</span>else if (mTagHandler != null) {
            mTagHandler.handleTag(true, tag, mSpannableStringBuilder, mReader);
        }
<span style="white-space:pre">	</span>else if (mTagHandler != null) {
            mTagHandler.handleTag(false, tag, mSpannableStringBuilder, mReader);
        }
其中两段代码分别是开始标签和结束标签传回,可以看出handleTag(boolean opening, String tag, Editable output, XMLReader xmlReader) 的opening标识的是这个标签是否为开始标签,然后tag是源码帮忙过滤出的标签。

代码实现:

MainActivity.java:

package com.code41.demo;

import org.xml.sax.XMLReader;

import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.text.Editable;
import android.text.Html;
import android.text.Html.ImageGetter;
import android.text.Html.TagHandler;
import android.text.Spanned;
import android.util.Log;
import android.widget.TextView;

public class MainActivity extends Activity {

	private TextView demoTextView;// TextView for test string.xml
	private TextView styleDemoTextView;// TextView for test string.xml
	private TextView htmlTextView;// TextView for test string.xml
	private Handler mHandler = new Handler();

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

		demoTextView = (TextView) findViewById(R.id.text_view_string_demo);
		styleDemoTextView = (TextView) findViewById(R.id.text_view_style_string_demo);
		htmlTextView = (TextView) findViewById(R.id.text_view_html_demo);

		// 大部分应用都是网络请求应用,所以要养成异步赋值的好习惯
		post(new Runnable() {

			@Override
			public void run() {
				// 设置无样式的字符串内容
				String stringDemoValue = getString(R.string.code41_string_demo_value, "鲜花", 200);
				demoTextView.setText(stringDemoValue);
				// 设置HTML样式的字符串内容
				String styleStringDemoValue = getString(R.string.code41_style_string_demo_value, "鲜花", 200);
				Spanned styleSpanned = Html.fromHtml(styleStringDemoValue);
				Log.e("MainActivity", "content=>" + styleSpanned);
				styleDemoTextView.setText(styleSpanned);

				// 将spanned转换String
				String content = Html.toHtml(styleSpanned);
				Log.e("MainActivity", "content=>" + content);
				// log content=>content=><p
				// dir="ltr">很高兴可以收到你赠送的<b><font
				// color ="#eb6067">鲜花</font></b>和<b><font
				// color ="#eb6067">200</font></b>金币。</p>

				// 将图片内容img带入textView中
				String htmlDemoString = getString(R.string.code41_html_string_demo_value, R.drawable.ic_launcher);
				htmlTextView.setText(Html.fromHtml(htmlDemoString, imgGetter, tagHandler));

			}
		});
	}

	private void post(Runnable runnable) {
		if (isFinishing() || null == mHandler) {
			return;
		}
		mHandler.post(runnable);
	}

	/**
	 * 重写图片获取的ImageGetter
	 */
	ImageGetter imgGetter = new Html.ImageGetter() {
		public Drawable getDrawable(String source) {
			int id = Integer.parseInt(source);
			Drawable d = getResources().getDrawable(id);
			d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
			return d;
		}
	};

	/**
	 * 重写未知标签Handle
	 */
	TagHandler tagHandler = new TagHandler() {

		@Override
		public void handleTag(boolean opening, String tag, Editable output, XMLReader xmlReader) {
			if ("hello".equals(tag) && opening) {
				output.append("hello tag filter");
			}
		}

	};

}

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#f8f8f8" >

    <TextView
        android:id="@+id/text_view_string_demo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:textColor="#666666"
        android:textSize="20sp" />
    
    <TextView
        android:id="@+id/text_view_style_string_demo"
        android:layout_below="@+id/text_view_string_demo"
        android:layout_marginTop="10dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#666666"
        android:textSize="20sp" />
    
    <TextView
        android:id="@+id/text_view_html_demo"
        android:layout_below="@+id/text_view_style_string_demo"
        android:layout_marginTop="10dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#666666"
        android:textSize="20sp" />
    

</RelativeLayout>
strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">StringDemo</string>
    <string name="hello_world">Hello world!</string>
    
    <string name="code41_string_demo_value">很高兴可以收到你赠送的%1$s和%2$d金币。</string>
    <string name="code41_style_string_demo_value">很高兴可以收到你赠送的<Data><![CDATA[<font color="#eb6067"><b>%1$s</b></font>和<font color="#eb6067"><b>%2$d</b></font>金币]]></Data>。</string>
    <string name="code41_html_string_demo_value">嵌入的图片是<Data><![CDATA[<img src=\"%1$d\"/>和测试自定义的tag是<hello></hello>]]></Data></string>
	
</resources>


效果截图:



项目代码





發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章