歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux編程 >> Linux編程

Android中為TextView增加自定義的HTML標簽

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

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

然而,有一種場合,默認支持的標簽可能不夠用。比如,我們需要在textView中點擊某種鏈接,返回到應用中的某個界面,而不僅僅是網絡連接,如何實現?

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

先看Html類的源代碼中有這樣一段:

 

  1. /** 
  2.     * Is notified when HTML tags are encountered that the parser does 
  3.     * not know how to interpret. 
  4.     */  
  5.    public static interface TagHandler {  
  6.        /** 
  7.         * This method will be called whenn the HTML parser encounters 
  8.         * a tag that it does not know how to interpret. 
  9.         */  
  10.        public void handleTag(boolean opening, String tag,  
  11.                                 Editable output, XMLReader xmlReader);  

這裡定義了一個接口,接口用於什麼呢?

再繼續看代碼,看到對Html的tag進行解析部分的代碼:

 

  1. private void handleStartTag(String tag, Attributes attributes) {  
  2.         if (tag.equalsIgnoreCase("br")) {  
  3.             // We don't need to handle this. TagSoup will ensure that there's a </br> for each <br>   
  4.             // so we can safely emite the linebreaks when we handle the close tag.   
  5.         } else if (tag.equalsIgnoreCase("p")) {  
  6.             handleP(mSpannableStringBuilder);  
  7.         } else if (tag.equalsIgnoreCase("div")) {  
  8.             handleP(mSpannableStringBuilder);  
  9.         } else if (tag.equalsIgnoreCase("em")) {  
  10.             start(mSpannableStringBuilder, new Bold());  
  11.         } else if (tag.equalsIgnoreCase("b")) {  
  12.             start(mSpannableStringBuilder, new Bold());  
  13.         } else if (tag.equalsIgnoreCase("strong")) {  
  14.             start(mSpannableStringBuilder, new Italic());  
  15.         } else if (tag.equalsIgnoreCase("cite")) {  
  16.             start(mSpannableStringBuilder, new Italic());  
  17.         } else if (tag.equalsIgnoreCase("dfn")) {  
  18.             start(mSpannableStringBuilder, new Italic());  
  19.         } else if (tag.equalsIgnoreCase("i")) {  
  20.             start(mSpannableStringBuilder, new Italic());  
  21.         } else if (tag.equalsIgnoreCase("big")) {  
  22.             start(mSpannableStringBuilder, new Big());  
  23.         } else if (tag.equalsIgnoreCase("small")) {  
  24.             start(mSpannableStringBuilder, new Small());  
  25.         } else if (tag.equalsIgnoreCase("font")) {  
  26.             startFont(mSpannableStringBuilder, attributes);  
  27.         } else if (tag.equalsIgnoreCase("blockquote")) {  
  28.             handleP(mSpannableStringBuilder);  
  29.             start(mSpannableStringBuilder, new Blockquote());  
  30.         } else if (tag.equalsIgnoreCase("tt")) {  
  31.             start(mSpannableStringBuilder, new Monospace());  
  32.         } else if (tag.equalsIgnoreCase("a")) {  
  33.             startA(mSpannableStringBuilder, attributes);  
  34.         } else if (tag.equalsIgnoreCase("u")) {  
  35.             start(mSpannableStringBuilder, new Underline());  
  36.         } else if (tag.equalsIgnoreCase("sup")) {  
  37.             start(mSpannableStringBuilder, new Super());  
  38.         } else if (tag.equalsIgnoreCase("sub")) {  
  39.             start(mSpannableStringBuilder, new Sub());  
  40.         } else if (tag.length() == 2 &&  
  41.                    Character.toLowerCase(tag.charAt(0)) == 'h' &&  
  42.                    tag.charAt(1) >= '1' && tag.charAt(1) <= '6') {  
  43.             handleP(mSpannableStringBuilder);  
  44.             start(mSpannableStringBuilder, new Header(tag.charAt(1) - '1'));  
  45.         } else if (tag.equalsIgnoreCase("img")) {  
  46.             startImg(mSpannableStringBuilder, attributes, mImageGetter);  
  47.         } else if (mTagHandler != null) {  
  48.             mTagHandler.handleTag(true, tag, mSpannableStringBuilder, mReader);  
  49.         }  
  50.     }  
  51.   
  52.     private void handleEndTag(String tag) {  
  53.         if (tag.equalsIgnoreCase("br")) {  
  54.             handleBr(mSpannableStringBuilder);  
  55.         } else if (tag.equalsIgnoreCase("p")) {  
  56.             handleP(mSpannableStringBuilder);  
  57.         } else if (tag.equalsIgnoreCase("div")) {  
  58.             handleP(mSpannableStringBuilder);  
  59.         } else if (tag.equalsIgnoreCase("em")) {  
  60.             end(mSpannableStringBuilder, Bold.classnew StyleSpan(Typeface.BOLD));  
  61.         } else if (tag.equalsIgnoreCase("b")) {  
  62.             end(mSpannableStringBuilder, Bold.classnew StyleSpan(Typeface.BOLD));  
  63.         } else if (tag.equalsIgnoreCase("strong")) {  
  64.             end(mSpannableStringBuilder, Italic.classnew StyleSpan(Typeface.ITALIC));  
  65.         } else if (tag.equalsIgnoreCase("cite")) {  
  66.             end(mSpannableStringBuilder, Italic.classnew StyleSpan(Typeface.ITALIC));  
  67.         } else if (tag.equalsIgnoreCase("dfn")) {  
  68.             end(mSpannableStringBuilder, Italic.classnew StyleSpan(Typeface.ITALIC));  
  69.         } else if (tag.equalsIgnoreCase("i")) {  
  70.             end(mSpannableStringBuilder, Italic.classnew StyleSpan(Typeface.ITALIC));  
  71.         } else if (tag.equalsIgnoreCase("big")) {  
  72.             end(mSpannableStringBuilder, Big.classnew RelativeSizeSpan(1.25f));  
  73.         } else if (tag.equalsIgnoreCase("small")) {  
  74.             end(mSpannableStringBuilder, Small.classnew RelativeSizeSpan(0.8f));  
  75.         } else if (tag.equalsIgnoreCase("font")) {  
  76.             endFont(mSpannableStringBuilder);  
  77.         } else if (tag.equalsIgnoreCase("blockquote")) {  
  78.             handleP(mSpannableStringBuilder);  
  79.             end(mSpannableStringBuilder, Blockquote.classnew QuoteSpan());  
  80.         } else if (tag.equalsIgnoreCase("tt")) {  
  81.             end(mSpannableStringBuilder, Monospace.class,  
  82.                     new TypefaceSpan("monospace"));  
  83.         } else if (tag.equalsIgnoreCase("a")) {  
  84.             endA(mSpannableStringBuilder);  
  85.         } else if (tag.equalsIgnoreCase("u")) {  
  86.             end(mSpannableStringBuilder, Underline.classnew UnderlineSpan());  
  87.         } else if (tag.equalsIgnoreCase("sup")) {  
  88.             end(mSpannableStringBuilder, Super.classnew SuperscriptSpan());  
  89.         } else if (tag.equalsIgnoreCase("sub")) {  
  90.             end(mSpannableStringBuilder, Sub.classnew SubscriptSpan());  
  91.         } else if (tag.length() == 2 &&  
  92.                 Character.toLowerCase(tag.charAt(0)) == 'h' &&  
  93.                 tag.charAt(1) >= '1' && tag.charAt(1) <= '6') {  
  94.             handleP(mSpannableStringBuilder);  
  95.             endHeader(mSpannableStringBuilder);  
  96.         } else if (mTagHandler != null) {  
  97.             mTagHandler.handleTag(false, tag, mSpannableStringBuilder, mReader);  
  98.         }  
  99.     }  

Copyright © Linux教程網 All Rights Reserved