taghandler java_GitHub - cowthan/HtmlTagHandler: 支持TextView默认支持的所有标签,支持自定义,取代安卓默认的Html.TagHandler...

HtmlTagHandler

支持TextView默认支持的所有标签,支持自定义,取代安卓默认的Html.TagHandler

特性

支持TextView默认支持的标签

支持自定义标签,接口类似Html.TagHandler

1 自定义标签:

/**

* 解析哈哈哈

*/

public class SpanTagHandler implements HtmlTagHandler.TagHandler {

private String fontColor = "";

@Override

public void handleTag(boolean open, String tag, Editable output, Attributes attrs) {

if(tag.toLowerCase().equals("span")){

if(open){

//开标签,output是空(sax还没读到),attrs有值

for(int i = 0; i < attrs.getLength(); i++){

if(attrs.getLocalName(i).equals("style")){

String style = attrs.getValue(i); //{color:#e60012}

fontColor = style.replace("{", "").replace("}", "").replace("color", "").replace(":", "");

}

}

}else{

//闭标签,output有值了,attrs没值

output.setSpan(new ForegroundColorSpan(Color.parseColor(fontColor)), 0, output.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

}

}

}

}

2 html转换成spanned

String content = "哈哈哈";

Spanned s = HtmlTagHandler.fromHtml(content, null, new SpanTagHandler());

tv.setText(s);

3 为什么要有这个库

按照安卓默认提供的方式,让TextView显示html得这样:

tv_2.setText(Html.fromHtml(content, null, new Html.TagHandler()));

但是Html.TagHandler提供的接口是:

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

span标签默认是不被支持的,所以要自己写的话,要得到span标签的文本和style属性,但Attributes没传出来,

反而传了个XmlReader对象出来,XmlReader对象我不会用

看android.text.Html的源码:

handleStartTag(String tag, Attributes attributes)

这里其实已经解析出属性了,为何不传出来呢???

所以HtmlTagHandler就干了两件事,一是拷出源码,二是更改接口,去掉XmlReader,换成Attributes

4 问题

如果直接解析:

String content = "呵呵呵哈哈哈嘿嘿嘿";

会报错,需要处理成纯xml格式

content = "

" + content + "";
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值