解析在Android中為TextView增加自定義HTML標籤的實現方法

NO IMAGE

Android中的TextView,本身就支援部分的Html格式標籤。這其中包括常用的字型大小顏色設定,文字連結等。使用起來也比較方便,只需要使用Html類轉換一下即可。比如:

textView.setText(Html.fromHtml(str));

然而,有一種場合,預設支援的標籤可能不夠用。比如,我們需要在textView中點選某種連結,返回到應用中的某個介面,而不僅僅是網路連線,如何實現?

經過幾個小時對android中的Html類原始碼的研究,找到了解決辦法,並且測試通過。

先看Html類的原始碼中有這樣一段:
複製程式碼 程式碼如下:
/**
    * Is notified when HTML tags are encountered that the parser does
    * not know how to interpret.
    */ 
   public static interface TagHandler { 
       /**
        * This method will be called whenn the HTML parser encounters
        * a tag that it does not know how to interpret.
        */ 
       public void handleTag(boolean opening, String tag, 
                                Editable output, XMLReader xmlReader); 

這裡定義了一個介面,介面用於什麼呢?

再繼續看程式碼,看到對Html的tag進行解析部分的程式碼:
複製程式碼 程式碼如下:
private void handleStartTag(String tag, Attributes attributes) { 
        if (tag.equalsIgnoreCase(“br”)) { 
            // We don’t need to handle this. TagSoup will ensure that there’s a </br> for each <br>  
            // so we can safely emite the linebreaks when we handle the close tag.  
        } else if (tag.equalsIgnoreCase(“p”)) { 
            handleP(mSpannableStringBuilder); 
        } else if (tag.equalsIgnoreCase(“div”)) { 
            handleP(mSpannableStringBuilder); 
        } else if (tag.equalsIgnoreCase(“em”)) { 
            start(mSpannableStringBuilder, new Bold()); 
        } else if (tag.equalsIgnoreCase(“b”)) { 
            start(mSpannableStringBuilder, new Bold()); 
        } else if (tag.equalsIgnoreCase(“strong”)) { 
            start(mSpannableStringBuilder, new Italic()); 
        } else if (tag.equalsIgnoreCase(“cite”)) { 
            start(mSpannableStringBuilder, new Italic()); 
        } else if (tag.equalsIgnoreCase(“dfn”)) { 
            start(mSpannableStringBuilder, new Italic()); 
        } else if (tag.equalsIgnoreCase(“i”)) { 
            start(mSpannableStringBuilder, new Italic()); 
        } else if (tag.equalsIgnoreCase(“big”)) { 
            start(mSpannableStringBuilder, new Big()); 
        } else if (tag.equalsIgnoreCase(“small”)) { 
            start(mSpannableStringBuilder, new Small()); 
        } else if (tag.equalsIgnoreCase(“font”)) { 
            startFont(mSpannableStringBuilder, attributes); 
        } else if (tag.equalsIgnoreCase(“blockquote”)) { 
            handleP(mSpannableStringBuilder); 
            start(mSpannableStringBuilder, new Blockquote()); 
        } else if (tag.equalsIgnoreCase(“tt”)) { 
            start(mSpannableStringBuilder, new Monospace()); 
        } else if (tag.equalsIgnoreCase(“a”)) { 
            startA(mSpannableStringBuilder, attributes); 
        } else if (tag.equalsIgnoreCase(“u”)) { 
            start(mSpannableStringBuilder, new Underline()); 
        } else if (tag.equalsIgnoreCase(“sup”)) { 
            start(mSpannableStringBuilder, new Super()); 
        } else if (tag.equalsIgnoreCase(“sub”)) { 
            start(mSpannableStringBuilder, new Sub()); 
        } else if (tag.length() == 2 && 
                   Character.toLowerCase(tag.charAt(0)) == ‘h’ && 
                   tag.charAt(1) >= ‘1’ && tag.charAt(1) <= ‘6’) { 
            handleP(mSpannableStringBuilder); 
            start(mSpannableStringBuilder, new Header(tag.charAt(1) – ‘1’)); 
        } else if (tag.equalsIgnoreCase(“img”)) { 
            startImg(mSpannableStringBuilder, attributes, mImageGetter); 
        } else if (mTagHandler != null) { 
            mTagHandler.handleTag(true, tag, mSpannableStringBuilder, mReader); 
        } 
    } 

    private void handleEndTag(String tag) { 
        if (tag.equalsIgnoreCase(“br”)) { 
            handleBr(mSpannableStringBuilder); 
        } else if (tag.equalsIgnoreCase(“p”)) { 
            handleP(mSpannableStringBuilder); 
        } else if (tag.equalsIgnoreCase(“div”)) { 
            handleP(mSpannableStringBuilder); 
        } else if (tag.equalsIgnoreCase(“em”)) { 
            end(mSpannableStringBuilder, Bold.class, new StyleSpan(Typeface.BOLD)); 
        } else if (tag.equalsIgnoreCase(“b”)) { 
            end(mSpannableStringBuilder, Bold.class, new StyleSpan(Typeface.BOLD)); 
        } else if (tag.equalsIgnoreCase(“strong”)) { 
            end(mSpannableStringBuilder, Italic.class, new StyleSpan(Typeface.ITALIC)); 
        } else if (tag.equalsIgnoreCase(“cite”)) { 
            end(mSpannableStringBuilder, Italic.class, new StyleSpan(Typeface.ITALIC)); 
        } else if (tag.equalsIgnoreCase(“dfn”)) { 
            end(mSpannableStringBuilder, Italic.class, new StyleSpan(Typeface.ITALIC)); 
        } else if (tag.equalsIgnoreCase(“i”)) { 
            end(mSpannableStringBuilder, Italic.class, new StyleSpan(Typeface.ITALIC)); 
        } else if (tag.equalsIgnoreCase(“big”)) { 
            end(mSpannableStringBuilder, Big.class, new RelativeSizeSpan(1.25f)); 
        } else if (tag.equalsIgnoreCase(“small”)) { 
            end(mSpannableStringBuilder, Small.class, new RelativeSizeSpan(0.8f)); 
        } else if (tag.equalsIgnoreCase(“font”)) { 
            endFont(mSpannableStringBuilder); 
        } else if (tag.equalsIgnoreCase(“blockquote”)) { 
            handleP(mSpannableStringBuilder); 
            end(mSpannableStringBuilder, Blockquote.class, new QuoteSpan()); 
        } else if (tag.equalsIgnoreCase(“tt”)) { 
            end(mSpannableStringBuilder, Monospace.class, 
                    new TypefaceSpan(“monospace”)); 
        } else if (tag.equalsIgnoreCase(“a”)) { 
            endA(mSpannableStringBuilder); 
        } else if (tag.equalsIgnoreCase(“u”)) { 
            end(mSpannableStringBuilder, Underline.class, new UnderlineSpan()); 
        } else if (tag.equalsIgnoreCase(“sup”)) { 
            end(mSpannableStringBuilder, Super.class, new SuperscriptSpan()); 
        } else if (tag.equalsIgnoreCase(“sub”)) { 
            end(mSpannableStringBuilder, Sub.class, new SubscriptSpan()); 
        } else if (tag.length() == 2 && 
                Character.toLowerCase(tag.charAt(0)) == ‘h’ && 
                tag.charAt(1) >= ‘1’ && tag.charAt(1) <= ‘6’) { 
            handleP(mSpannableStringBuilder); 
            endHeader(mSpannableStringBuilder); 
        } else if (mTagHandler != null) { 
            mTagHandler.handleTag(false, tag, mSpannableStringBuilder, mReader); 
        } 
    } 

可以看到,如果不是預設的標籤,會呼叫mTagHandler的handleTag方法。所以,我們可以實現此介面,來解析自己定義的標籤型別。

再看一段我實現的對<game>標籤進行解析的示例程式碼:
複製程式碼 程式碼如下:
public class GameTagHandler 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(“game”)) { 
            if (opening) { 
                startGame(tag, output, xmlReader); 
            } else { 
                endGame(tag, output, xmlReader); 
            } 
        }  

    } 
    public void startGame(String tag, Editable output, XMLReader xmlReader) { 
        startIndex = output.length(); 
    } 

    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 implements OnClickListener { 

        @Override 
        public void onClick(View v) { 
            // 跳轉某頁面  
        } 
    }

上面這段程式碼,是對<game>…</game>的自定義標籤進行解析。

具體呼叫方法:

       textView.setText(Html.fromHtml(“點選<game>這裡</game>跳轉到遊戲”,

              null, new GameTagHandler()));

       textView.setClickable(true);

       textView.setMovementMethod(LinkMovementMethod.getInstance());

執行後,能夠看到文字中的字串“這裡”帶了超連結,點選連結後,GameSpan類的onClick()方法被呼叫。就可以在這個方法中進行跳轉了。

您可能感興趣的文章:

Android實現3D標籤雲效果Android自定義控制元件ViewGroup實現標籤雲(四)Android開發技巧之在a標籤或TextView控制元件中單擊連結彈出Activity(自定義動作)Android中使用include標籤和merge標籤重複使用佈局android nfc常用標籤讀取總結android配合viewpager實現可滑動的標籤欄示例分享Android TextView顯示Html類解析的網頁和圖片及自定義標籤用法示例Android實現熱門標籤的流式佈局Android入門之ActivityGroup GridView實現Tab分頁標籤的方法Android實現3D標籤雲簡單效果