利用android源码实现获取汉字的拼音


分类: android 拼音 源码   74人阅读  评论(0)  收藏  举报

最近做一个项目,需要实现输入人名得到对应的汉语拼音,在网上查了下,也有不少方案,但或多或少都有些问题(有个开源项目pinyin4j倒是挺好,有兴趣可以试试,这里只讲android自带的功能)。后来一想,android的通讯录不就有这样的功能吗,看看它是怎么做的不就行了。

经研究,发现和拼音转换相关的文件位于:

packages\providers\ContactsProvider\src\com\android\providers\contacts\ContactLocaleUtils.java

packages\providers\ContactsProvider\src\com\android\providers\contacts\HanziToPinyin.java

把这两个文件拷贝到自己的工程中,稍作修改就可以用了,代码如下:

[java]  view plain copy
  1. /* 
  2.  * Copyright (C) 2010 The Android Open Source Project 
  3.  * 
  4.  * Licensed under the Apache License, Version 2.0 (the "License"); 
  5.  * you may not use this file except in compliance with the License. 
  6.  * You may obtain a copy of the License at 
  7.  * 
  8.  *      http://www.apache.org/licenses/LICENSE-2.0 
  9.  * 
  10.  * Unless required by applicable law or agreed to in writing, software 
  11.  * distributed under the License is distributed on an "AS IS" BASIS, 
  12.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
  13.  * See the License for the specific language governing permissions and 
  14.  * limitations under the License 
  15.  */  
  16.   
  17. package com.test;  
  18.   
  19. import android.util.SparseArray;  
  20.   
  21.   
  22. import java.util.ArrayList;  
  23. import java.util.HashSet;  
  24. import java.util.Iterator;  
  25. import java.util.Locale;  
  26.   
  27. import com.test.HanziToPinyin.Token;  
  28.   
  29. /** 
  30.  * This utility class provides customized sort key and name lookup key according the locale. 
  31.  */  
  32. public class ContactLocaleUtils {  
  33.     public interface FullNameStyle {  
  34.         public static final int UNDEFINED = 0;  
  35.         public static final int WESTERN = 1;  
  36.   
  37.         /** 
  38.          * Used if the name is written in Hanzi/Kanji/Hanja and we could not determine 
  39.          * which specific language it belongs to: Chinese, Japanese or Korean. 
  40.          */  
  41.         public static final int CJK = 2;  
  42.   
  43.         public static final int CHINESE = 3;  
  44.         public static final int JAPANESE = 4;  
  45.         public static final int KOREAN = 5;  
  46.     }  
  47.     /** 
  48.      * This class is the default implementation. 
  49.      * <p> 
  50.      * It should be the base class for other locales' implementation. 
  51.      */  
  52.     public class ContactLocaleUtilsBase {  
  53.         public String getSortKey(String displayName) {  
  54.             return displayName;  
  55.         }  
  56.         @SuppressWarnings("unused")  
  57.         public Iterator<String> getNameLookupKeys(String name) {  
  58.             return null;  
  59.         }  
  60.     }  
  61.   
  62.     /** 
  63.      * The classes to generate the Chinese style sort and search keys. 
  64.      * <p> 
  65.      * The sorting key is generated as each Chinese character' pinyin proceeding with 
  66.      * space and character itself. If the character's pinyin unable to find, the character 
  67.      * itself will be used. 
  68.      * <p> 
  69.      * The below additional name lookup keys will be generated. 
  70.      * a. Chinese character's pinyin and pinyin's initial character. 
  71.      * b. Latin word and the initial character for Latin word. 
  72.      * The name lookup keys are generated to make sure the name can be found by from any 
  73.      * initial character. 
  74.      */  
  75.     private class ChineseContactUtils extends ContactLocaleUtilsBase {  
  76.         @Override  
  77.         public String getSortKey(String displayName) {  
  78.             ArrayList<Token> tokens = HanziToPinyin.getInstance().get(displayName);  
  79.             if (tokens != null && tokens.size() > 0) {  
  80.                 StringBuilder sb = new StringBuilder();  
  81.                 for (Token token : tokens) {  
  82.                     // Put Chinese character's pinyin, then proceed with the  
  83.                     // character itself.  
  84.                     if (Token.PINYIN == token.type) {  
  85.                         if (sb.length() > 0) {  
  86.                             sb.append(' ');  
  87.                         }  
  88.                         sb.append(token.target);  
  89.                         sb.append(' ');  
  90.                         sb.append(token.source);  
  91.                     } else {  
  92.                         if (sb.length() > 0) {  
  93.                             sb.append(' ');  
  94.                         }  
  95.                         sb.append(token.source);  
  96.                     }  
  97.                 }  
  98.                 return sb.toString();  
  99.             }  
  100.             return super.getSortKey(displayName);  
  101.         }  
  102.   
  103.         @Override  
  104.         public Iterator<String> getNameLookupKeys(String name) {  
  105.             // TODO : Reduce the object allocation.  
  106.             HashSet<String> keys = new HashSet<String>();  
  107.             ArrayList<Token> tokens = HanziToPinyin.getInstance().get(name);  
  108.             final int tokenCount = tokens.size();  
  109.             final StringBuilder keyPinyin = new StringBuilder();  
  110.             final StringBuilder keyInitial = new StringBuilder();  
  111.             // There is no space among the Chinese Characters, the variant name  
  112.             // lookup key wouldn't work for Chinese. The keyOrignal is used to  
  113.             // build the lookup keys for itself.  
  114.             final StringBuilder keyOrignal = new StringBuilder();  
  115.             for (int i = tokenCount - 1; i >= 0; i--) {  
  116.                 final Token token = tokens.get(i);  
  117.                 if (Token.PINYIN == token.type) {  
  118.                     keyPinyin.insert(0, token.target);  
  119.                     keyInitial.insert(0, token.target.charAt(0));  
  120.                 } else if (Token.LATIN == token.type) {  
  121.                     // Avoid adding space at the end of String.  
  122.                     if (keyPinyin.length() > 0) {  
  123.                         keyPinyin.insert(0' ');  
  124.                     }  
  125.                     if (keyOrignal.length() > 0) {  
  126.                         keyOrignal.insert(0' ');  
  127.                     }  
  128.                     keyPinyin.insert(0, token.source);  
  129.                     keyInitial.insert(0, token.source.charAt(0));  
  130.                 }  
  131.                 keyOrignal.insert(0, token.source);  
  132.                 keys.add(keyOrignal.toString());  
  133.                 keys.add(keyPinyin.toString());  
  134.                 keys.add(keyInitial.toString());  
  135.             }  
  136.             return keys.iterator();  
  137.         }  
  138.     }  
  139.   
  140.     private static final String CHINESE_LANGUAGE = Locale.CHINESE.getLanguage().toLowerCase();  
  141.     private static final String JAPANESE_LANGUAGE = Locale.JAPANESE.getLanguage().toLowerCase();  
  142.     private static final String KOREAN_LANGUAGE = Locale.KOREAN.getLanguage().toLowerCase();  
  143.   
  144.     private static ContactLocaleUtils sSingleton;  
  145.     private final SparseArray<ContactLocaleUtilsBase> mUtils =  
  146.             new SparseArray<ContactLocaleUtilsBase>();  
  147.   
  148.     private final ContactLocaleUtilsBase mBase = new ContactLocaleUtilsBase();  
  149.   
  150.     private String mLanguage;  
  151.   
  152.     private ContactLocaleUtils() {  
  153.         setLocale(null);  
  154.     }  
  155.   
  156.     public void setLocale(Locale currentLocale) {  
  157.         if (currentLocale == null) {  
  158.             mLanguage = Locale.getDefault().getLanguage().toLowerCase();  
  159.         } else {  
  160.             mLanguage = currentLocale.getLanguage().toLowerCase();  
  161.         }  
  162.     }  
  163.   
  164.     public String getSortKey(String displayName, int nameStyle) {  
  165.         return getForSort(Integer.valueOf(nameStyle)).getSortKey(displayName);  
  166.     }  
  167.   
  168.     public Iterator<String> getNameLookupKeys(String name, int nameStyle) {  
  169.         return getForNameLookup(Integer.valueOf(nameStyle)).getNameLookupKeys(name);  
  170.     }  
  171.   
  172.     /** 
  173.      *  Determine which utility should be used for generating NameLookupKey. 
  174.      *  <p> 
  175.      *  a. For Western style name, if the current language is Chinese, the 
  176.      *     ChineseContactUtils should be used. 
  177.      *  b. For Chinese and CJK style name if current language is neither Japanese or Korean, 
  178.      *     the ChineseContactUtils should be used. 
  179.      */  
  180.     private ContactLocaleUtilsBase getForNameLookup(Integer nameStyle) {  
  181.         int nameStyleInt = nameStyle.intValue();  
  182.         Integer adjustedUtil = Integer.valueOf(getAdjustedStyle(nameStyleInt));  
  183.         if (CHINESE_LANGUAGE.equals(mLanguage) && nameStyleInt == FullNameStyle.WESTERN) {  
  184.             adjustedUtil = Integer.valueOf(FullNameStyle.CHINESE);  
  185.         }  
  186.         return get(adjustedUtil);  
  187.     }  
  188.   
  189.     private synchronized ContactLocaleUtilsBase get(Integer nameStyle) {  
  190.         ContactLocaleUtilsBase utils = mUtils.get(nameStyle);  
  191.         if (utils == null) {  
  192.             if (nameStyle.intValue() == FullNameStyle.CHINESE) {  
  193.                 utils = new ChineseContactUtils();  
  194.                 mUtils.put(nameStyle, utils);  
  195.             }  
  196.         }  
  197.         return (utils == null) ? mBase : utils;  
  198.     }  
  199.   
  200.     /** 
  201.      *  Determine the which utility should be used for generating sort key. 
  202.      *  <p> 
  203.      *  For Chinese and CJK style name if current language is neither Japanese or Korean, 
  204.      *  the ChineseContactUtils should be used. 
  205.      */  
  206.     private ContactLocaleUtilsBase getForSort(Integer nameStyle) {  
  207.         return get(Integer.valueOf(getAdjustedStyle(nameStyle.intValue())));  
  208.     }  
  209.   
  210.     public static synchronized ContactLocaleUtils getIntance() {  
  211.         if (sSingleton == null) {  
  212.             sSingleton = new ContactLocaleUtils();  
  213.         }  
  214.         return sSingleton;  
  215.     }  
  216.   
  217.     private int getAdjustedStyle(int nameStyle) {  
  218.         if (nameStyle == FullNameStyle.CJK  && !JAPANESE_LANGUAGE.equals(mLanguage) &&  
  219.                 !KOREAN_LANGUAGE.equals(mLanguage)) {  
  220.             return FullNameStyle.CHINESE;  
  221.         } else {  
  222.             return nameStyle;  
  223.         }  
  224.     }  
  225. }  

[java]  view plain copy
  1. /* 
  2.  * Copyright (C) 2011 The Android Open Source Project 
  3.  * 
  4.  * Licensed under the Apache License, Version 2.0 (the "License"); 
  5.  * you may not use this file except in compliance with the License. 
  6.  * You may obtain a copy of the License at 
  7.  * 
  8.  *      http://www.apache.org/licenses/LICENSE-2.0 
  9.  * 
  10.  * Unless required by applicable law or agreed to in writing, software 
  11.  * distributed under the License is distributed on an "AS IS" BASIS, 
  12.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
  13.  * See the License for the specific language governing permissions and 
  14.  * limitations under the License. 
  15.  */  
  16.   
  17. package com.test;  
  18.   
  19. import android.text.TextUtils;  
  20. import android.util.Log;  
  21.   
  22. import java.text.Collator;  
  23. import java.util.ArrayList;  
  24. import java.util.Locale;  
  25.   
  26. /** 
  27.  * An object to convert Chinese character to its corresponding pinyin string. For characters with 
  28.  * multiple possible pinyin string, only one is selected according to collator. Polyphone is not 
  29.  * supported in this implementation. This class is implemented to achieve the best runtime 
  30.  * performance and minimum runtime resources with tolerable sacrifice of accuracy. This 
  31.  * implementation highly depends on zh_CN ICU collation data and must be always synchronized with 
  32.  * ICU. 
  33.  * 
  34.  * Currently this file is aligned to zh.txt in ICU 4.6 
  35.  */  
  36. public class HanziToPinyin {  
  37.     private static final String TAG = "HanziToPinyin";  
  38.   
  39.     // Turn on this flag when we want to check internal data structure.  
  40.     private static final boolean DEBUG = false;  
  41.   
  42.     /** 
  43.      * Unihans array. 
  44.      * 
  45.      * Each unihans is the first one within same pinyin when collator is zh_CN. 
  46.      */  
  47.     public static final char[] UNIHANS = {  
  48.             '\u963f''\u54ce''\u5b89''\u80ae''\u51f9''\u516b',  
  49.             '\u6300''\u6273''\u90a6''\u52f9''\u9642''\u5954',  
  50.             '\u4f3b''\u5c44''\u8fb9''\u706c''\u618b''\u6c43',  
  51.             '\u51ab''\u7676''\u5cec''\u5693''\u5072''\u53c2',  
  52.             '\u4ed3''\u64a1''\u518a''\u5d7e''\u66fd''\u66fe',  
  53.             '\u5c64''\u53c9''\u8286''\u8fbf''\u4f25''\u6284',  
  54.             '\u8f66''\u62bb''\u6c88''\u6c89''\u9637''\u5403',  
  55.             '\u5145''\u62bd''\u51fa''\u6b3b''\u63e3''\u5ddb',  
  56.             '\u5205''\u5439''\u65fe''\u9034''\u5472''\u5306',  
  57.             '\u51d1''\u7c97''\u6c46''\u5d14''\u90a8''\u6413',  
  58.             '\u5491''\u5446''\u4e39''\u5f53''\u5200''\u561a',  
  59.             '\u6265''\u706f''\u6c10''\u55f2''\u7538''\u5201',  
  60.             '\u7239''\u4e01''\u4e1f''\u4e1c''\u543a''\u53be',  
  61.             '\u8011''\u8968''\u5428''\u591a''\u59b8''\u8bf6',  
  62.             '\u5940''\u97a5''\u513f''\u53d1''\u5e06''\u531a',  
  63.             '\u98de''\u5206''\u4e30''\u8985''\u4ecf''\u7d11',  
  64.             '\u4f15''\u65ee''\u4f85''\u7518''\u5188''\u768b',  
  65.             '\u6208''\u7ed9''\u6839''\u522f''\u5de5''\u52fe',  
  66.             '\u4f30''\u74dc''\u4e56''\u5173''\u5149''\u5f52',  
  67.             '\u4e28''\u5459''\u54c8''\u548d''\u4f44''\u592f',  
  68.             '\u8320''\u8bc3''\u9ed2''\u62eb''\u4ea8''\u5677',  
  69.             '\u53ff''\u9f41''\u4e6f''\u82b1''\u6000''\u72bf',  
  70.             '\u5ddf''\u7070''\u660f''\u5419''\u4e0c''\u52a0',  
  71.             '\u620b''\u6c5f''\u827d''\u9636''\u5dfe''\u5755',  
  72.             '\u5182''\u4e29''\u51e5''\u59e2''\u5658''\u519b',  
  73.             '\u5494''\u5f00''\u520a''\u5ffc''\u5c3b''\u533c',  
  74.             '\u808e''\u52a5''\u7a7a''\u62a0''\u625d''\u5938',  
  75.             '\u84af''\u5bbd''\u5321''\u4e8f''\u5764''\u6269',  
  76.             '\u5783''\u6765''\u5170''\u5577''\u635e''\u808b',  
  77.             '\u52d2''\u5d1a''\u5215''\u4fe9''\u5941''\u826f',  
  78.             '\u64a9''\u5217''\u62ce''\u5222''\u6e9c''\u56d6',  
  79.             '\u9f99''\u779c''\u565c''\u5a08''\u7567''\u62a1',  
  80.             '\u7f57''\u5463''\u5988''\u57cb''\u5ada''\u7264',  
  81.             '\u732b''\u4e48''\u5445''\u95e8''\u753f''\u54aa',  
  82.             '\u5b80''\u55b5''\u4e5c''\u6c11''\u540d''\u8c2c',  
  83.             '\u6478''\u54de''\u6bea''\u55ef''\u62cf''\u8149',  
  84.             '\u56e1''\u56d4''\u5b6c''\u7592''\u5a1e''\u6041',  
  85.             '\u80fd''\u59ae''\u62c8''\u5b22''\u9e1f''\u634f',  
  86.             '\u56dc''\u5b81''\u599e''\u519c''\u7fba''\u5974',  
  87.             '\u597b''\u759f''\u9ec1''\u90cd''\u5594''\u8bb4',  
  88.             '\u5991''\u62cd''\u7705''\u4e53''\u629b''\u5478',  
  89.             '\u55b7''\u5309''\u4e15''\u56e8''\u527d''\u6c15',  
  90.             '\u59d8''\u4e52''\u948b''\u5256''\u4ec6''\u4e03',  
  91.             '\u6390''\u5343''\u545b''\u6084''\u767f''\u4eb2',  
  92.             '\u72c5''\u828e''\u4e18''\u533a''\u5cd1''\u7f3a',  
  93.             '\u590b''\u5465''\u7a63''\u5a06''\u60f9''\u4eba',  
  94.             '\u6254''\u65e5''\u8338''\u53b9''\u909a''\u633c',  
  95.             '\u5827''\u5a51''\u77a4''\u637c''\u4ee8''\u6be2',  
  96.             '\u4e09''\u6852''\u63bb''\u95aa''\u68ee''\u50e7',  
  97.             '\u6740''\u7b5b''\u5c71''\u4f24''\u5f30''\u5962',  
  98.             '\u7533''\u8398''\u6552''\u5347''\u5c38''\u53ce',  
  99.             '\u4e66''\u5237''\u8870''\u95e9''\u53cc''\u8c01',  
  100.             '\u542e''\u8bf4''\u53b6''\u5fea''\u635c''\u82cf',  
  101.             '\u72fb''\u590a''\u5b59''\u5506''\u4ed6''\u56fc',  
  102.             '\u574d''\u6c64''\u5932''\u5fd1''\u71a5''\u5254',  
  103.             '\u5929''\u65eb''\u5e16''\u5385''\u56f2''\u5077',  
  104.             '\u51f8''\u6e4d''\u63a8''\u541e''\u4e47''\u7a75',  
  105.             '\u6b6a''\u5f2f''\u5c23''\u5371''\u6637''\u7fc1',  
  106.             '\u631d''\u4e4c''\u5915''\u8672''\u4eda''\u4e61',  
  107.             '\u7071''\u4e9b''\u5fc3''\u661f''\u51f6''\u4f11',  
  108.             '\u5401''\u5405''\u524a''\u5743''\u4e2b''\u6079',  
  109.             '\u592e''\u5e7a''\u503b''\u4e00''\u56d9''\u5e94',  
  110.             '\u54df''\u4f63''\u4f18''\u625c''\u56e6''\u66f0',  
  111.             '\u6655''\u7b60''\u7b7c''\u5e00''\u707d''\u5142',  
  112.             '\u5328''\u50ae''\u5219''\u8d3c''\u600e''\u5897',  
  113.             '\u624e''\u635a''\u6cbe''\u5f20''\u957f''\u9577',  
  114.             '\u4f4b''\u8707''\u8d1e''\u4e89''\u4e4b''\u5cd9',  
  115.             '\u5ea2''\u4e2d''\u5dde''\u6731''\u6293''\u62fd',  
  116.             '\u4e13''\u5986''\u96b9''\u5b92''\u5353''\u4e72',  
  117.             '\u5b97''\u90b9''\u79df''\u94bb''\u539c''\u5c0a',  
  118.             '\u6628''\u5159''\u9fc3''\u9fc4', };  
  119.   
  120.     /** 
  121.      * Pinyin array. 
  122.      * 
  123.      * Each pinyin is corresponding to unihans of same 
  124.      * offset in the unihans array. 
  125.      */  
  126.     public static final byte[][] PINYINS = {  
  127.             { 65,   0,   0,   0,   0,   0}, { 65,  73,   0,   0,   0,   0},  
  128.             { 65,  78,   0,   0,   0,   0}, { 65,  78,  71,   0,   0,   0},  
  129.             { 65,  79,   0,   0,   0,   0}, { 66,  65,   0,   0,   0,   0},  
  130.             { 66,  65,  73,   0,   0,   0}, { 66,  65,  78,   0,   0,   0},  
  131.             { 66,  65,  78,  71,   0,   0}, { 66,  65,  79,   0,   0,   0},  
  132.             { 66,  69,  73,   0,   0,   0}, { 66,  69,  78,   0,   0,   0},  
  133.             { 66,  69,  78,  71,   0,   0}, { 66,  73,   0,   0,   0,   0},  
  134.             { 66,  73,  65,  78,   0,   0}, { 66,  73,  65,  79,   0,   0},  
  135.             { 66,  73,  69,   0,   0,   0}, { 66,  73,  78,   0,   0,   0},  
  136.             { 66,  73,  78,  71,   0,   0}, { 66,  79,   0,   0,   0,   0},  
  137.             { 66,  85,   0,   0,   0,   0}, { 67,  65,   0,   0,   0,   0},  
  138.             { 67,  65,  73,   0,   0,   0}, { 67,  65,  78,   0,   0,   0},  
  139.             { 67,  65,  78,  71,   0,   0}, { 67,  65,  79,   0,   0,   0},  
  140.             { 67,  69,   0,   0,   0,   0}, { 67,  69,  78,   0,   0,   0},  
  141.             { 67,  69,  78,  71,   0,   0}, { 90,  69,  78,  71,   0,   0},  
  142.             { 67,  69,  78,  71,   0,   0}, { 67,  72,  65,   0,   0,   0},  
  143.             { 67,  72,  65,  73,   0,   0}, { 67,  72,  65,  78,   0,   0},  
  144.             { 67,  72,  65,  78,  71,   0}, { 67,  72,  65,  79,   0,   0},  
  145.             { 67,  72,  69,   0,   0,   0}, { 67,  72,  69,  78,   0,   0},  
  146.             { 83,  72,  69,  78,   0,   0}, { 67,  72,  69,  78,   0,   0},  
  147.             { 67,  72,  69,  78,  71,   0}, { 67,  72,  73,   0,   0,   0},  
  148.             { 67,  72,  79,  78,  71,   0}, { 67,  72,  79,  85,   0,   0},  
  149.             { 67,  72,  85,   0,   0,   0}, { 67,  72,  85,  65,   0,   0},  
  150.             { 67,  72,  85,  65,  73,   0}, { 67,  72,  85,  65,  78,   0},  
  151.             { 67,  72,  85,  65,  78,  71}, { 67,  72,  85,  73,   0,   0},  
  152.             { 67,  72,  85,  78,   0,   0}, { 67,  72,  85,  79,   0,   0},  
  153.             { 67,  73,   0,   0,   0,   0}, { 67,  79,  78,  71,   0,   0},  
  154.             { 67,  79,  85,   0,   0,   0}, { 67,  85,   0,   0,   0,   0},  
  155.             { 67,  85,  65,  78,   0,   0}, { 67,  85,  73,   0,   0,   0},  
  156.             { 67,  85,  78,   0,   0,   0}, { 67,  85,  79,   0,   0,   0},  
  157.             { 68,  65,   0,   0,   0,   0}, { 68,  65,  73,   0,   0,   0},  
  158.             { 68,  65,  78,   0,   0,   0}, { 68,  65,  78,  71,   0,   0},  
  159.             { 68,  65,  79,   0,   0,   0}, { 68,  69,   0,   0,   0,   0},  
  160.             { 68,  69,  78,   0,   0,   0}, { 68,  69,  78,  71,   0,   0},  
  161.             { 68,  73,   0,   0,   0,   0}, { 68,  73,  65,   0,   0,   0},  
  162.             { 68,  73,  65,  78,   0,   0}, { 68,  73,  65,  79,   0,   0},  
  163.             { 68,  73,  69,   0,   0,   0}, { 68,  73,  78,  71,   0,   0},  
  164.             { 68,  73,  85,   0,   0,   0}, { 68,  79,  78,  71,   0,   0},  
  165.             { 68,  79,  85,   0,   0,   0}, { 68,  85,   0,   0,   0,   0},  
  166.             { 68,  85,  65,  78,   0,   0}, { 68,  85,  73,   0,   0,   0},  
  167.             { 68,  85,  78,   0,   0,   0}, { 68,  85,  79,   0,   0,   0},  
  168.             { 69,   0,   0,   0,   0,   0}, { 69,  73,   0,   0,   0,   0},  
  169.             { 69,  78,   0,   0,   0,   0}, { 69,  78,  71,   0,   0,   0},  
  170.             { 69,  82,   0,   0,   0,   0}, { 70,  65,   0,   0,   0,   0},  
  171.             { 70,  65,  78,   0,   0,   0}, { 70,  65,  78,  71,   0,   0},  
  172.             { 70,  69,  73,   0,   0,   0}, { 70,  69,  78,   0,   0,   0},  
  173.             { 70,  69,  78,  71,   0,   0}, { 70,  73,  65,  79,   0,   0},  
  174.             { 70,  79,   0,   0,   0,   0}, { 70,  79,  85,   0,   0,   0},  
  175.             { 70,  85,   0,   0,   0,   0}, { 71,  65,   0,   0,   0,   0},  
  176.             { 71,  65,  73,   0,   0,   0}, { 71,  65,  78,   0,   0,   0},  
  177.             { 71,  65,  78,  71,   0,   0}, { 71,  65,  79,   0,   0,   0},  
  178.             { 71,  69,   0,   0,   0,   0}, { 71,  69,  73,   0,   0,   0},  
  179.             { 71,  69,  78,   0,   0,   0}, { 71,  69,  78,  71,   0,   0},  
  180.             { 71,  79,  78,  71,   0,   0}, { 71,  79,  85,   0,   0,   0},  
  181.             { 71,  85,   0,   0,   0,   0}, { 71,  85,  65,   0,   0,   0},  
  182.             { 71,  85,  65,  73,   0,   0}, { 71,  85,  65,  78,   0,   0},  
  183.             { 71,  85,  65,  78,  71,   0}, { 71,  85,  73,   0,   0,   0},  
  184.             { 71,  85,  78,   0,   0,   0}, { 71,  85,  79,   0,   0,   0},  
  185.             { 72,  65,   0,   0,   0,   0}, { 72,  65,  73,   0,   0,   0},  
  186.             { 72,  65,  78,   0,   0,   0}, { 72,  65,  78,  71,   0,   0},  
  187.             { 72,  65,  79,   0,   0,   0}, { 72,  69,   0,   0,   0,   0},  
  188.             { 72,  69,  73,   0,   0,   0}, { 72,  69,  78,   0,   0,   0},  
  189.             { 72,  69,  78,  71,   0,   0}, { 72,  77,   0,   0,   0,   0},  
  190.             { 72,  79,  78,  71,   0,   0}, { 72,  79,  85,   0,   0,   0},  
  191.             { 72,  85,   0,   0,   0,   0}, { 72,  85,  65,   0,   0,   0},  
  192.             { 72,  85,  65,  73,   0,   0}, { 72,  85,  65,  78,   0,   0},  
  193.             { 72,  85,  65,  78,  71,   0}, { 72,  85,  73,   0,   0,   0},  
  194.             { 72,  85,  78,   0,   0,   0}, { 72,  85,  79,   0,   0,   0},  
  195.             { 74,  73,   0,   0,   0,   0}, { 74,  73,  65,   0,   0,   0},  
  196.             { 74,  73,  65,  78,   0,   0}, { 74,  73,  65,  78,  71,   0},  
  197.             { 74,  73,  65,  79,   0,   0}, { 74,  73,  69,   0,   0,   0},  
  198.             { 74,  73,  78,   0,   0,   0}, { 74,  73,  78,  71,   0,   0},  
  199.             { 74,  73,  79,  78,  71,   0}, { 74,  73,  85,   0,   0,   0},  
  200.             { 74,  85,   0,   0,   0,   0}, { 74,  85,  65,  78,   0,   0},  
  201.             { 74,  85,  69,   0,   0,   0}, { 74,  85,  78,   0,   0,   0},  
  202.             { 75,  65,   0,   0,   0,   0}, { 75,  65,  73,   0,   0,   0},  
  203.             { 75,  65,  78,   0,   0,   0}, { 75,  65,  78,  71,   0,   0},  
  204.             { 75,  65,  79,   0,   0,   0}, { 75,  69,   0,   0,   0,   0},  
  205.             { 75,  69,  78,   0,   0,   0}, { 75,  69,  78,  71,   0,   0},  
  206.             { 75,  79,  78,  71,   0,   0}, { 75,  79,  85,   0,   0,   0},  
  207.             { 75,  85,   0,   0,   0,   0}, { 75,  85,  65,   0,   0,   0},  
  208.             { 75,  85,  65,  73,   0,   0}, { 75,  85,  65,  78,   0,   0},  
  209.             { 75,  85,  65,  78,  71,   0}, { 75,  85,  73,   0,   0,   0},  
  210.             { 75,  85,  78,   0,   0,   0}, { 75,  85,  79,   0,   0,   0},  
  211.             { 76,  65,   0,   0,   0,   0}, { 76,  65,  73,   0,   0,   0},  
  212.             { 76,  65,  78,   0,   0,   0}, { 76,  65,  78,  71,   0,   0},  
  213.             { 76,  65,  79,   0,   0,   0}, { 76,  69,   0,   0,   0,   0},  
  214.             { 76,  69,  73,   0,   0,   0}, { 76,  69,  78,  71,   0,   0},  
  215.             { 76,  73,   0,   0,   0,   0}, { 76,  73,  65,   0,   0,   0},  
  216.             { 76,  73,  65,  78,   0,   0}, { 76,  73,  65,  78,  71,   0},  
  217.             { 76,  73,  65,  79,   0,   0}, { 76,  73,  69,   0,   0,   0},  
  218.             { 76,  73,  78,   0,   0,   0}, { 76,  73,  78,  71,   0,   0},  
  219.             { 76,  73,  85,   0,   0,   0}, { 76,  79,   0,   0,   0,   0},  
  220.             { 76,  79,  78,  71,   0,   0}, { 76,  79,  85,   0,   0,   0},  
  221.             { 76,  85,   0,   0,   0,   0}, { 76,  85,  65,  78,   0,   0},  
  222.             { 76,  85,  69,   0,   0,   0}, { 76,  85,  78,   0,   0,   0},  
  223.             { 76,  85,  79,   0,   0,   0}, { 77,   0,   0,   0,   0,   0},  
  224.             { 77,  65,   0,   0,   0,   0}, { 77,  65,  73,   0,   0,   0},  
  225.             { 77,  65,  78,   0,   0,   0}, { 77,  65,  78,  71,   0,   0},  
  226.             { 77,  65,  79,   0,   0,   0}, { 77,  69,   0,   0,   0,   0},  
  227.             { 77,  69,  73,   0,   0,   0}, { 77,  69,  78,   0,   0,   0},  
  228.             { 77,  69,  78,  71,   0,   0}, { 77,  73,   0,   0,   0,   0},  
  229.             { 77,  73,  65,  78,   0,   0}, { 77,  73,  65,  79,   0,   0},  
  230.             { 77,  73,  69,   0,   0,   0}, { 77,  73,  78,   0,   0,   0},  
  231.             { 77,  73,  78,  71,   0,   0}, { 77,  73,  85,   0,   0,   0},  
  232.             { 77,  79,   0,   0,   0,   0}, { 77,  79,  85,   0,   0,   0},  
  233.             { 77,  85,   0,   0,   0,   0}, { 78,   0,   0,   0,   0,   0},  
  234.             { 78,  65,   0,   0,   0,   0}, { 78,  65,  73,   0,   0,   0},  
  235.             { 78,  65,  78,   0,   0,   0}, { 78,  65,  78,  71,   0,   0},  
  236.             { 78,  65,  79,   0,   0,   0}, { 78,  69,   0,   0,   0,   0},  
  237.             { 78,  69,  73,   0,   0,   0}, { 78,  69,  78,   0,   0,   0},  
  238.             { 78,  69,  78,  71,   0,   0}, { 78,  73,   0,   0,   0,   0},  
  239.             { 78,  73,  65,  78,   0,   0}, { 78,  73,  65,  78,  71,   0},  
  240.             { 78,  73,  65,  79,   0,   0}, { 78,  73,  69,   0,   0,   0},  
  241.             { 78,  73,  78,   0,   0,   0}, { 78,  73,  78,  71,   0,   0},  
  242.             { 78,  73,  85,   0,   0,   0}, { 78,  79,  78,  71,   0,   0},  
  243.             { 78,  79,  85,   0,   0,   0}, { 78,  85,   0,   0,   0,   0},  
  244.             { 78,  85,  65,  78,   0,   0}, { 78,  85,  69,   0,   0,   0},  
  245.             { 78,  85,  78,   0,   0,   0}, { 78,  85,  79,   0,   0,   0},  
  246.             { 79,   0,   0,   0,   0,   0}, { 79,  85,   0,   0,   0,   0},  
  247.             { 80,  65,   0,   0,   0,   0}, { 80,  65,  73,   0,   0,   0},  
  248.             { 80,  65,  78,   0,   0,   0}, { 80,  65,  78,  71,   0,   0},  
  249.             { 80,  65,  79,   0,   0,   0}, { 80,  69,  73,   0,   0,   0},  
  250.             { 80,  69,  78,   0,   0,   0}, { 80,  69,  78,  71,   0,   0},  
  251.             { 80,  73,   0,   0,   0,   0}, { 80,  73,  65,  78,   0,   0},  
  252.             { 80,  73,  65,  79,   0,   0}, { 80,  73,  69,   0,   0,   0},  
  253.             { 80,  73,  78,   0,   0,   0}, { 80,  73,  78,  71,   0,   0},  
  254.             { 80,  79,   0,   0,   0,   0}, { 80,  79,  85,   0,   0,   0},  
  255.             { 80,  85,   0,   0,   0,   0}, { 81,  73,   0,   0,   0,   0},  
  256.             { 81,  73,  65,   0,   0,   0}, { 81,  73,  65,  78,   0,   0},  
  257.             { 81,  73,  65,  78,  71,   0}, { 81,  73,  65,  79,   0,   0},  
  258.             { 81,  73,  69,   0,   0,   0}, { 81,  73,  78,   0,   0,   0},  
  259.             { 81,  73,  78,  71,   0,   0}, { 81,  73,  79,  78,  71,   0},  
  260.             { 81,  73,  85,   0,   0,   0}, { 81,  85,   0,   0,   0,   0},  
  261.             { 81,  85,  65,  78,   0,   0}, { 81,  85,  69,   0,   0,   0},  
  262.             { 81,  85,  78,   0,   0,   0}, { 82,  65,  78,   0,   0,   0},  
  263.             { 82,  65,  78,  71,   0,   0}, { 82,  65,  79,   0,   0,   0},  
  264.             { 82,  69,   0,   0,   0,   0}, { 82,  69,  78,   0,   0,   0},  
  265.             { 82,  69,  78,  71,   0,   0}, { 82,  73,   0,   0,   0,   0},  
  266.             { 82,  79,  78,  71,   0,   0}, { 82,  79,  85,   0,   0,   0},  
  267.             { 82,  85,   0,   0,   0,   0}, { 82,  85,  65,   0,   0,   0},  
  268.             { 82,  85,  65,  78,   0,   0}, { 82,  85,  73,   0,   0,   0},  
  269.             { 82,  85,  78,   0,   0,   0}, { 82,  85,  79,   0,   0,   0},  
  270.             { 83,  65,   0,   0,   0,   0}, { 83,  65,  73,   0,   0,   0},  
  271.             { 83,  65,  78,   0,   0,   0}, { 83,  65,  78,  71,   0,   0},  
  272.             { 83,  65,  79,   0,   0,   0}, { 83,  69,   0,   0,   0,   0},  
  273.             { 83,  69,  78,   0,   0,   0}, { 83,  69,  78,  71,   0,   0},  
  274.             { 83,  72,  65,   0,   0,   0}, { 83,  72,  65,  73,   0,   0},  
  275.             { 83,  72,  65,  78,   0,   0}, { 83,  72,  65,  78,  71,   0},  
  276.             { 83,  72,  65,  79,   0,   0}, { 83,  72,  69,   0,   0,   0},  
  277.             { 83,  72,  69,  78,   0,   0}, { 88,  73,  78,   0,   0,   0},  
  278.             { 83,  72,  69,  78,   0,   0}, { 83,  72,  69,  78,  71,   0},  
  279.             { 83,  72,  73,   0,   0,   0}, { 83,  72,  79,  85,   0,   0},  
  280.             { 83,  72,  85,   0,   0,   0}, { 83,  72,  85,  65,   0,   0},  
  281.             { 83,  72,  85,  65,  73,   0}, { 83,  72,  85,  65,  78,   0},  
  282.             { 83,  72,  85,  65,  78,  71}, { 83,  72,  85,  73,   0,   0},  
  283.             { 83,  72,  85,  78,   0,   0}, { 83,  72,  85,  79,   0,   0},  
  284.             { 83,  73,   0,   0,   0,   0}, { 83,  79,  78,  71,   0,   0},  
  285.             { 83,  79,  85,   0,   0,   0}, { 83,  85,   0,   0,   0,   0},  
  286.             { 83,  85,  65,  78,   0,   0}, { 83,  85,  73,   0,   0,   0},  
  287.             { 83,  85,  78,   0,   0,   0}, { 83,  85,  79,   0,   0,   0},  
  288.             { 84,  65,   0,   0,   0,   0}, { 84,  65,  73,   0,   0,   0},  
  289.             { 84,  65,  78,   0,   0,   0}, { 84,  65,  78,  71,   0,   0},  
  290.             { 84,  65,  79,   0,   0,   0}, { 84,  69,   0,   0,   0,   0},  
  291.             { 84,  69,  78,  71,   0,   0}, { 84,  73,   0,   0,   0,   0},  
  292.             { 84,  73,  65,  78,   0,   0}, { 84,  73,  65,  79,   0,   0},  
  293.             { 84,  73,  69,   0,   0,   0}, { 84,  73,  78,  71,   0,   0},  
  294.             { 84,  79,  78,  71,   0,   0}, { 84,  79,  85,   0,   0,   0},  
  295.             { 84,  85,   0,   0,   0,   0}, { 84,  85,  65,  78,   0,   0},  
  296.             { 84,  85,  73,   0,   0,   0}, { 84,  85,  78,   0,   0,   0},  
  297.             { 84,  85,  79,   0,   0,   0}, { 87,  65,   0,   0,   0,   0},  
  298.             { 87,  65,  73,   0,   0,   0}, { 87,  65,  78,   0,   0,   0},  
  299.             { 87,  65,  78,  71,   0,   0}, { 87,  69,  73,   0,   0,   0},  
  300.             { 87,  69,  78,   0,   0,   0}, { 87,  69,  78,  71,   0,   0},  
  301.             { 87,  79,   0,   0,   0,   0}, { 87,  85,   0,   0,   0,   0},  
  302.             { 88,  73,   0,   0,   0,   0}, { 88,  73,  65,   0,   0,   0},  
  303.             { 88,  73,  65,  78,   0,   0}, { 88,  73,  65,  78,  71,   0},  
  304.             { 88,  73,  65,  79,   0,   0}, { 88,  73,  69,   0,   0,   0},  
  305.             { 88,  73,  78,   0,   0,   0}, { 88,  73,  78,  71,   0,   0},  
  306.             { 88,  73,  79,  78,  71,   0}, { 88,  73,  85,   0,   0,   0},  
  307.             { 88,  85,   0,   0,   0,   0}, { 88,  85,  65,  78,   0,   0},  
  308.             { 88,  85,  69,   0,   0,   0}, { 88,  85,  78,   0,   0,   0},  
  309.             { 89,  65,   0,   0,   0,   0}, { 89,  65,  78,   0,   0,   0},  
  310.             { 89,  65,  78,  71,   0,   0}, { 89,  65,  79,   0,   0,   0},  
  311.             { 89,  69,   0,   0,   0,   0}, { 89,  73,   0,   0,   0,   0},  
  312.             { 89,  73,  78,   0,   0,   0}, { 89,  73,  78,  71,   0,   0},  
  313.             { 89,  79,   0,   0,   0,   0}, { 89,  79,  78,  71,   0,   0},  
  314.             { 89,  79,  85,   0,   0,   0}, { 89,  85,   0,   0,   0,   0},  
  315.             { 89,  85,  65,  78,   0,   0}, { 89,  85,  69,   0,   0,   0},  
  316.             { 89,  85,  78,   0,   0,   0}, { 74,  85,  78,   0,   0,   0},  
  317.             { 89,  85,  78,   0,   0,   0}, { 90,  65,   0,   0,   0,   0},  
  318.             { 90,  65,  73,   0,   0,   0}, { 90,  65,  78,   0,   0,   0},  
  319.             { 90,  65,  78,  71,   0,   0}, { 90,  65,  79,   0,   0,   0},  
  320.             { 90,  69,   0,   0,   0,   0}, { 90,  69,  73,   0,   0,   0},  
  321.             { 90,  69,  78,   0,   0,   0}, { 90,  69,  78,  71,   0,   0},  
  322.             { 90,  72,  65,   0,   0,   0}, { 90,  72,  65,  73,   0,   0},  
  323.             { 90,  72,  65,  78,   0,   0}, { 90,  72,  65,  78,  71,   0},  
  324.             { 67,  72,  65,  78,  71,   0}, { 90,  72,  65,  78,  71,   0},  
  325.             { 90,  72,  65,  79,   0,   0}, { 90,  72,  69,   0,   0,   0},  
  326.             { 90,  72,  69,  78,   0,   0}, { 90,  72,  69,  78,  71,   0},  
  327.             { 90,  72,  73,   0,   0,   0}, { 83,  72,  73,   0,   0,   0},  
  328.             { 90,  72,  73,   0,   0,   0}, { 90,  72,  79,  78,  71,   0},  
  329.             { 90,  72,  79,  85,   0,   0}, { 90,  72,  85,   0,   0,   0},  
  330.             { 90,  72,  85,  65,   0,   0}, { 90,  72,  85,  65,  73,   0},  
  331.             { 90,  72,  85,  65,  78,   0}, { 90,  72,  85,  65,  78,  71},  
  332.             { 90,  72,  85,  73,   0,   0}, { 90,  72,  85,  78,   0,   0},  
  333.             { 90,  72,  85,  79,   0,   0}, { 90,  73,   0,   0,   0,   0},  
  334.             { 90,  79,  78,  71,   0,   0}, { 90,  79,  85,   0,   0,   0},  
  335.             { 90,  85,   0,   0,   0,   0}, { 90,  85,  65,  78,   0,   0},  
  336.             { 90,  85,  73,   0,   0,   0}, { 90,  85,  78,   0,   0,   0},  
  337.             { 90,  85,  79,   0,   0,   0}, {  0,   0,   0,   0,   0,   0},  
  338.             { 83,  72,  65,  78,   0,   0}, {  0,   0,   0,   0,   0,   0}, };  
  339.   
  340.     /** First and last Chinese character with known Pinyin according to zh collation */  
  341.     private static final String FIRST_PINYIN_UNIHAN = "\u963F";  
  342.     private static final String LAST_PINYIN_UNIHAN = "\u9FFF";  
  343.   
  344.     private static final Collator COLLATOR = Collator.getInstance(Locale.CHINA);  
  345.   
  346.     private static HanziToPinyin sInstance;  
  347.     private final boolean mHasChinaCollator;  
  348.   
  349.     public static class Token {  
  350.         /** 
  351.          * Separator between target string for each source char 
  352.          */  
  353.         public static final String SEPARATOR = " ";  
  354.   
  355.         public static final int LATIN = 1;  
  356.         public static final int PINYIN = 2;  
  357.         public static final int UNKNOWN = 3;  
  358.   
  359.         public Token() {  
  360.         }  
  361.   
  362.         public Token(int type, String source, String target) {  
  363.             this.type = type;  
  364.             this.source = source;  
  365.             this.target = target;  
  366.         }  
  367.   
  368.         /** 
  369.          * Type of this token, ASCII, PINYIN or UNKNOWN. 
  370.          */  
  371.         public int type;  
  372.         /** 
  373.          * Original string before translation. 
  374.          */  
  375.         public String source;  
  376.         /** 
  377.          * Translated string of source. For Han, target is corresponding Pinyin. Otherwise target is 
  378.          * original string in source. 
  379.          */  
  380.         public String target;  
  381.     }  
  382.   
  383.     protected HanziToPinyin(boolean hasChinaCollator) {  
  384.         mHasChinaCollator = hasChinaCollator;  
  385.     }  
  386.   
  387.     public static HanziToPinyin getInstance() {  
  388.         synchronized (HanziToPinyin.class) {  
  389.             if (sInstance != null) {  
  390.                 return sInstance;  
  391.             }  
  392.             // Check if zh_CN collation data is available  
  393.             final Locale locale[] = Collator.getAvailableLocales();  
  394.             for (int i = 0; i < locale.length; i++) {  
  395.                 if (locale[i].equals(Locale.CHINA)) {  
  396.                     // Do self validation just once.  
  397.                     if (DEBUG) {  
  398.                         Log.d(TAG, "Self validation. Result: " + doSelfValidation());  
  399.                     }  
  400.                     sInstance = new HanziToPinyin(true);  
  401.                     return sInstance;  
  402.                 }  
  403.             }  
  404.             Log.w(TAG, "There is no Chinese collator, HanziToPinyin is disabled");  
  405.             sInstance = new HanziToPinyin(false);  
  406.             return sInstance;  
  407.         }  
  408.     }  
  409.   
  410.     /** 
  411.      * Validate if our internal table has some wrong value. 
  412.      * 
  413.      * @return true when the table looks correct. 
  414.      */  
  415.     private static boolean doSelfValidation() {  
  416.         char lastChar = UNIHANS[0];  
  417.         String lastString = Character.toString(lastChar);  
  418.         for (char c : UNIHANS) {  
  419.             if (lastChar == c) {  
  420.                 continue;  
  421.             }  
  422.             final String curString = Character.toString(c);  
  423.             int cmp = COLLATOR.compare(lastString, curString);  
  424.             if (cmp >= 0) {  
  425.                 Log.e(TAG, "Internal error in Unihan table. " + "The last string \"" + lastString  
  426.                         + "\" is greater than current string \"" + curString + "\".");  
  427.                 return false;  
  428.             }  
  429.             lastString = curString;  
  430.         }  
  431.         return true;  
  432.     }  
  433.   
  434.     private Token getToken(char character) {  
  435.         Token token = new Token();  
  436.         final String letter = Character.toString(character);  
  437.         token.source = letter;  
  438.         int offset = -1;  
  439.         int cmp;  
  440.         if (character < 256) {  
  441.             token.type = Token.LATIN;  
  442.             token.target = letter;  
  443.             return token;  
  444.         } else {  
  445.             cmp = COLLATOR.compare(letter, FIRST_PINYIN_UNIHAN);  
  446.             if (cmp < 0) {  
  447.                 token.type = Token.UNKNOWN;  
  448.                 token.target = letter;  
  449.                 return token;  
  450.             } else if (cmp == 0) {  
  451.                 token.type = Token.PINYIN;  
  452.                 offset = 0;  
  453.             } else {  
  454.                 cmp = COLLATOR.compare(letter, LAST_PINYIN_UNIHAN);  
  455.                 if (cmp > 0) {  
  456.                     token.type = Token.UNKNOWN;  
  457.                     token.target = letter;  
  458.                     return token;  
  459.                 } else if (cmp == 0) {  
  460.                     token.type = Token.PINYIN;  
  461.                     offset = UNIHANS.length - 1;  
  462.                 }  
  463.             }  
  464.         }  
  465.   
  466.         token.type = Token.PINYIN;  
  467.         if (offset < 0) {  
  468.             int begin = 0;  
  469.             int end = UNIHANS.length - 1;  
  470.             while (begin <= end) {  
  471.                 offset = (begin + end) / 2;  
  472.                 final String unihan = Character.toString(UNIHANS[offset]);  
  473.                 cmp = COLLATOR.compare(letter, unihan);  
  474.                 if (cmp == 0) {  
  475.                     break;  
  476.                 } else if (cmp > 0) {  
  477.                     begin = offset + 1;  
  478.                 } else {  
  479.                     end = offset - 1;  
  480.                 }  
  481.             }  
  482.         }  
  483.         if (cmp < 0) {  
  484.             offset--;  
  485.         }  
  486.         StringBuilder pinyin = new StringBuilder();  
  487.         for (int j = 0; j < PINYINS[offset].length && PINYINS[offset][j] != 0; j++) {  
  488.             pinyin.append((char) PINYINS[offset][j]);  
  489.         }  
  490.         token.target = pinyin.toString();  
  491.         if (TextUtils.isEmpty(token.target)) {  
  492.             token.type = Token.UNKNOWN;  
  493.             token.target = token.source;  
  494.         }  
  495.         return token;  
  496.     }  
  497.   
  498.     /** 
  499.      * Convert the input to a array of tokens. The sequence of ASCII or Unknown characters without 
  500.      * space will be put into a Token, One Hanzi character which has pinyin will be treated as a 
  501.      * Token. If these is no China collator, the empty token array is returned. 
  502.      */  
  503.     public ArrayList<Token> get(final String input) {  
  504.         ArrayList<Token> tokens = new ArrayList<Token>();  
  505.         if (!mHasChinaCollator || TextUtils.isEmpty(input)) {  
  506.             // return empty tokens.  
  507.             return tokens;  
  508.         }  
  509.         final int inputLength = input.length();  
  510.         final StringBuilder sb = new StringBuilder();  
  511.         int tokenType = Token.LATIN;  
  512.         // Go through the input, create a new token when  
  513.         // a. Token type changed  
  514.         // b. Get the Pinyin of current charater.  
  515.         // c. current character is space.  
  516.         for (int i = 0; i < inputLength; i++) {  
  517.             final char character = input.charAt(i);  
  518.             if (character == ' ') {  
  519.                 if (sb.length() > 0) {  
  520.                     addToken(sb, tokens, tokenType);  
  521.                 }  
  522.             } else if (character < 256) {  
  523.                 if (tokenType != Token.LATIN && sb.length() > 0) {  
  524.                     addToken(sb, tokens, tokenType);  
  525.                 }  
  526.                 tokenType = Token.LATIN;  
  527.                 sb.append(character);  
  528.             } else {  
  529.                 Token t = getToken(character);  
  530.                 if (t.type == Token.PINYIN) {  
  531.                     if (sb.length() > 0) {  
  532.                         addToken(sb, tokens, tokenType);  
  533.                     }  
  534.                     tokens.add(t);  
  535.                     tokenType = Token.PINYIN;  
  536.                 } else {  
  537.                     if (tokenType != t.type && sb.length() > 0) {  
  538.                         addToken(sb, tokens, tokenType);  
  539.                     }  
  540.                     tokenType = t.type;  
  541.                     sb.append(character);  
  542.                 }  
  543.             }  
  544.         }  
  545.         if (sb.length() > 0) {  
  546.             addToken(sb, tokens, tokenType);  
  547.         }  
  548.         return tokens;  
  549.     }  
  550.   
  551.     private void addToken(  
  552.             final StringBuilder sb, final ArrayList<Token> tokens, final int tokenType) {  
  553.         String str = sb.toString();  
  554.         tokens.add(new Token(tokenType, str, str));  
  555.         sb.setLength(0);  
  556.     }  
  557. }  

测试代码如下:

[java]  view plain copy
  1. @Override  
  2. public void onCreate(Bundle savedInstanceStated) {  
  3.     super.onCreate(savedInstanceStated);  
  4.       
  5.     setContentView(R.layout.main);  
  6.   
  7.     EditText editText = (EditText) findViewById(R.id.edit);  
  8.       
  9.       
  10.     final TextView textView = (TextView) findViewById(R.id.text);  
  11.       
  12.     editText.addTextChangedListener(new TextWatcher()  
  13.     {  
  14.           
  15.         @Override  
  16.         public void onTextChanged(CharSequence s, int start, int before, int count)  
  17.         {  
  18.         }  
  19.           
  20.         @Override  
  21.         public void beforeTextChanged(CharSequence s, int start, int count,  
  22.                 int after)  
  23.         {  
  24.         }  
  25.           
  26.         @Override  
  27.         public void afterTextChanged(Editable s)  
  28.         {  
  29.             String sk = ContactLocaleUtils.getIntance().getSortKey(s.toString(), FullNameStyle.CHINESE);  
  30.               
  31.             textView.setText(sk);  
  32.         }  
  33.     });  
  34. }  

截图:



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值