Android中只支持下列html标签:
Android TextView 支持的HTML标签
<a href="...">
<b>
<big>
<blockquote>
<br>
<cite>
<dfn>
<div align="...">
<em>
<font size="..." color="..." face="...">
<h1>
<h2>
<h3>
<h4>
<h5>
<h6>
<i>
<img src="...">
<p>
<small>
<strike>
<strong>
<sub>
<sup>
<tt>
<u>
- <br> // 换行</br>不可用
那如果我们还要实现一些标签怎么办呢
那则需要自己实现TagHandler接口了。
实现的概要说明:
Step one 继承 TagHandler接口
Step Two 实现
public void handleTag(boolean opening, String tag, Editable output,
XMLReader xmlReader)
接口被调用的过程:
系统在解析html文本
发现tag时调用一次 如<html>
接口传递的参数为 handleTag(true,”html”,output , xmlReader )
发现tag的闭合时,调用接口
接口传递的参数为 handleTag(false,”html”,output , xmlReader )
下面是一个实际执行的例子 给< strike> 标签 中的文字加上横线
public class MyHtmlTagHandler implements TagHandler { public void handleTag(boolean opening, String tag, Editable output, XMLReader xmlReader) { if(tag.equalsIgnoreCase("strike") || tag.equals("s")) { processStrike(opening, output); } } private void processStrike(boolean opening, Editable output) { int len = output.length(); if(opening) { output.setSpan(new StrikethroughSpan(), len, len, Spannable.SPAN_MARK_MARK); } else { Object obj = getLast(output, StrikethroughSpan.class); int where = output.getSpanStart(obj); output.removeSpan(obj); if (where != len) { output.setSpan(new StrikethroughSpan(), where, len, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); } } } private Object getLast(Editable text, Class kind) { Object[] objs = text.getSpans(0, text.length(), kind); if (objs.length == 0) { return null; } else { for(int i = objs.length;i>0;i--) { if(text.getSpanFlags(objs[i-1]) == Spannable.SPAN_MARK_MARK) { return objs[i-1]; } } return null; } } }
显示超链接,并响应事件
public class HTMLIMGActivity extends Activity { private TextView tv_img; String html = "<h1>text3:</h1>" +"<font color='#00FF00'>the content is hehe</font><br>" +"<a href='http://www.google.com'>link to google~~~</a>"; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); tv_img = (TextView) findViewById(R.id.tv_img); URLImageParser p = new URLImageParser(tv_img, HTMLIMGActivity.this); Spanned htmlSpan = Html.fromHtml(html, p, null); Log.i("hehehe", "spannestring:"+htmlSpan); tv_img.setText(htmlSpan); tv_img.setMovementMethod(LinkMovementMethod.getInstance());//不加这句无法链接,点了无反应 } }
要想点击有反应,要注意下列事项:
1:不能设置TextView的android:autoLink属性。
2:必须加上tv_img.setMovementMethod(LinkMovementMethod.getInstance());这句。
3:a标签中的地址可以使用<a href='http://www.google.com'>link to google~~~</a>这种形式。
同样也可以使用<a href=\"http://www.google.com\">link to google~~~</a>
响应事件
public class MyTagHandler implements TagHandler { int start; int stop; Context context; int color; public MyTagHandler(Context context, int color) { this.context = context; this.color = color; } @Override public void handleTag(boolean opening, String tag, Editable output, XMLReader xmlReader) { if (tag.toLowerCase().equals("mytag")) { if (opening) { start = output.length(); } else { stop = output.length(); String content = output.subSequence(start, stop).toString(); output.setSpan(new MySpan(context, color, content), start, stop, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); } } } class MySpan extends ClickableSpan implements OnClickListener { String content; int color; int type; Context context; @Override public void updateDrawState(TextPaint ds) { ds.setColor(color);//设置颜色 ds.setUnderlineText(false);//是否显示下划线 } public MySpan(Context context, int color, String content) { this.context = context; this.color = color; this.content = content; } @Override public void onClick(View widget) { //添加点击事件 Toast.makeText(context, content, Toast.LENGTH_SHORT).show(); } } }