标签:
首先新建一个类实现TagHandler接口,里面的handleTag,是专门用来处理自定义标签。
handleTag(boolean opening, String tag, Editable output, XMLReader xmlReader)
opening:表示是否是标签起始
tag: 标签名
output: 内容
xmlReader:xml
如果要使用自定义标签的属性只能使用反射了。
public class MyTagHandler implements TagHandler { private int startIndex = 0; private int stopIndex = 0; @Override public void handleTag(boolean opening, String tag, Editable output, XMLReader xmlReader) { if (tag.toLowerCase().equals("tag")) { if (opening) { startGame(tag, output, xmlReader); } else { endGame(tag, output, xmlReader); } } } public void startGame(String tag, Editable output, XMLReader xmlReader) { startIndex = output.length(); try { Field elementField = xmlReader.getClass().getDeclaredField("theNewElement"); elementField.setAccessible(true); Object element = elementField.get(xmlReader); Field attsField = element.getClass().getDeclaredField("theAtts"); attsField.setAccessible(true); Object atts = attsField.get(element); Field dataField = atts.getClass().getDeclaredField("data"); dataField.setAccessible(true); String[] data = (String[]) dataField.get(atts); Field lengthField = atts.getClass().getDeclaredField("length"); lengthField.setAccessible(true); int len = (Integer) lengthField.get(atts); String myAttributeA = null; String myAttributeB = null; for (int i = 0; i < len; i++) { // 这边的src和type换成你自己的属性名就可以了 if ("href".equals(data[i * 5 + 1])) { myAttributeA = data[i * 5 + 4]; } } Log.i("log", "src: " + myAttributeA + " type: " + myAttributeB); } catch (NoSuchFieldException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } } public void endGame(String tag, Editable output, XMLReader xmlReader) { stopIndex = output.length(); output.setSpan(new GameSpan(), startIndex, stopIndex, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); } private class GameSpan extends ClickableSpan { @Override public void onClick(View v) { Log.e("my", "click"); } } }
标签:
原文地址:http://www.cnblogs.com/guojing1991/p/4842077.html