Android汉字转拼音HanziToPinyin

287 篇文章 7 订阅

Android系统本身自带有有将汉字转化为英文拼音的类和方法。具体的类就是HanziToPinyin.java。Android系统自身实现的通讯录中就使用了HanziToPinyin.java对中文通讯录做分组整理。通过HanziToPinyin.java可以将汉字转化为拼音输出,在一些应用中非常必须,比如联系人的分组,假设一个人通讯录中存有若干姓张(ZHANG)的联系人,那么所有姓张的联系人按理都应该分组在“Z”组下。又比如微信、QQ等等此类社交类APP,凡是涉及到联系人、好友分组排序的应用场景,则均需要将汉字转化为拼音然后依据首字母排序归类。
HanziToPinyin.java不是一个公开的类,只是谷歌官方内部在实现Android通讯录中私有使用的一个类,我们不能够直接像使用普通Android SDK API一样使用,但这没关系,我们完全可以将这个类文件拷贝出来,放到我们自己的项目中,直接使用。
HanziToPinyin.java的代码文件,谷歌官方的通讯录APP下:

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

网上也有这个HanziToPinyin.java类文件的项目地址。但是,直接使用这个 类不能正常工作,错误原因是:

"There is no Chinese collator, HanziToPinyin is disabled"

发生这一错误的代码块是在HanziToPinyin.java的方法:
public static HanziToPinyin getInstance();
具体原因是这个方法在一些非原生定制的Android系统中,对中文Locale的定义规则不同,导致原代码文件中的locale[i].equals(Locale.CHINA)返回false,不能识别,致使以后的代码全部失去功效。

对此问题的修复(解决方案)

我改进了判断条件,增加一些代码:
final Locale chinaAddition = new Locale("zh");
将此chinaAddition作为辅助条件也加入到条件判断中,

if ( locale[i].equals(Locale.CHINA) ||  locale[i].equals(chinaAddition) ){
…
}

下面是我改进后的getInstance()方法全部代码:

public static HanziToPinyin getInstance() {
		synchronized (HanziToPinyin.class) {
			if (sInstance != null) {
				return sInstance;
			}
			// Check if zh_CN collation data is available
			final Locale locale[] = Collator.getAvailableLocales();

			// 增加的代码,增强。
			final Locale chinaAddition = new Locale("zh");

			for (int i = 0; i < locale.length; i++) {
				if (locale[i].equals(Locale.CHINA)
						|| locale[i].equals(chinaAddition)) {
					// Do self validation just once.
					if (DEBUG) {
						Log.d(TAG, "Self validation. Result: "
								+ doSelfValidation());
					}
					sInstance = new HanziToPinyin(true);
					return sInstance;
				}
			}
			Log.w(TAG,
					"There is no Chinese collator, HanziToPinyin is disabled");
			sInstance = new HanziToPinyin(false);
			return sInstance;
		}
	}

经由改进增强,HanziToPinyin.java的全部源代码如下(代码可以复制到自己的项目中直接使用):

/*
 * Copyright (C) 2011 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package zhangphil.hanyupinyin;

import android.text.TextUtils;
import android.util.Log;

import java.text.Collator;
import java.util.ArrayList;
import java.util.Locale;

/**
 * An object to convert Chinese character to its corresponding pinyin string.
 * For characters with multiple possible pinyin string, only one is selected
 * according to collator. Polyphone is not supported in this implementation.
 * This class is implemented to achieve the best runtime performance and minimum
 * runtime resources with tolerable sacrifice of accuracy. This implementation
 * highly depends on zh_CN ICU collation data and must be always synchronized
 * with ICU.
 *
 * Currently this file is aligned to zh.txt in ICU 4.6 鏉ヨ嚜android4.2婧愮爜
 */
public class HanziToPinyin {
	private static final String TAG = "HanziToPinyin";

	// Turn on this flag when we want to check internal data structure.
	private static final boolean DEBUG = false;

	/**
	 * Unihans array.
	 *
	 * Each unihans is the first one within same pinyin when collator is zh_CN.
	 */
	public static final char[] UNIHANS = { '\u963f', '\u54ce', '\u5b89',
			'\u80ae', '\u51f9', '\u516b', '\u6300', '\u6273', '\u90a6',
			'\u52f9', '\u9642', '\u5954', '\u4f3b', '\u5c44', '\u8fb9',
			'\u706c', '\u618b', '\u6c43', '\u51ab', '\u7676', '\u5cec',
			'\u5693', '\u5072', '\u53c2', '\u4ed3', '\u64a1', '\u518a',
			'\u5d7e', '\u66fd', '\u66fe', '\u5c64', '\u53c9', '\u8286',
			'\u8fbf', '\u4f25', '\u6284', '\u8f66', '\u62bb', '\u6c88',
			'\u6c89', '\u9637', '\u5403', '\u5145', '\u62bd', '\u51fa',
			'\u6b3b', '\u63e3', '\u5ddb', '\u5205', '\u5439', '\u65fe',
			'\u9034', '\u5472', '\u5306', '\u51d1', '\u7c97', '\u6c46',
			'\u5d14', '\u90a8', '\u6413', '\u5491', '\u5446', '\u4e39',
			'\u5f53', '\u5200', '\u561a', '\u6265', '\u706f', '\u6c10',
			'\u55f2', '\u7538', '\u5201', '\u7239', '\u4e01', '\u4e1f',
			'\u4e1c', '\u543a', '\u53be', '\u8011', '\u8968', '\u5428',
			'\u591a', '\u59b8', '\u8bf6', '\u5940', '\u97a5', '\u513f',
			'\u53d1', '\u5e06', '\u531a', '\u98de', '\u5206', '\u4e30',
			'\u8985', '\u4ecf', '\u7d11', '\u4f15', '\u65ee', '\u4f85',
			'\u7518', '\u5188', '\u768b', '\u6208', '\u7ed9', '\u6839',
			'\u522f', '\u5de5', '\u52fe', '\u4f30', '\u74dc', '\u4e56',
			'\u5173', '\u5149', '\u5f52', '\u4e28', '\u5459', '\u54c8',
			'\u548d', '\u4f44', '\u592f', '\u8320', '\u8bc3', '\u9ed2',
			'\u62eb', '\u4ea8', '\u5677', '\u53ff', '\u9f41', '\u4e6f',
			'\u82b1', '\u6000', '\u72bf', '\u5ddf', '\u7070', '\u660f',
			'\u5419', '\u4e0c', '\u52a0', '\u620b', '\u6c5f', '\u827d',
			'\u9636', '\u5dfe', '\u5755', '\u5182', '\u4e29', '\u51e5',
			'\u59e2', '\u5658', '\u519b', '\u5494', '\u5f00', '\u520a',
			'\u5ffc', '\u5c3b', '\u533c', '\u808e', '\u52a5', '\u7a7a',
			'\u62a0', '\u625d', '\u5938', '\u84af', '\u5bbd', '\u5321',
			'\u4e8f', '\u5764', '\u6269', '\u5783', '\u6765', '\u5170',
			'\u5577', '\u635e', '\u808b', '\u52d2', '\u5d1a', '\u5215',
			'\u4fe9', '\u5941', '\u826f', '\u64a9', '\u5217', '\u62ce',
			'\u5222', '\u6e9c', '\u56d6', '\u9f99', '\u779c', '\u565c',
			'\u5a08', '\u7567', '\u62a1', '\u7f57', '\u5463', '\u5988',
			'\u57cb', '\u5ada', '\u7264', '\u732b', '\u4e48', '\u5445',
			'\u95e8', '\u753f', '\u54aa', '\u5b80', '\u55b5', '\u4e5c',
			'\u6c11', '\u540d', '\u8c2c', '\u6478', '\u54de', '\u6bea',
			'\u55ef', '\u62cf', '\u8149', '\u56e1', '\u56d4', '\u5b6c',
			'\u7592', '\u5a1e', '\u6041', '\u80fd', '\u59ae', '\u62c8',
			'\u5b22', '\u9e1f', '\u634f', '\u56dc', '\u5b81', '\u599e',
			'\u519c', '\u7fba', '\u5974', '\u597b', '\u759f', '\u9ec1',
			'\u90cd', '\u5594', '\u8bb4', '\u5991', '\u62cd', '\u7705',
			'\u4e53', '\u629b', '\u5478', '\u55b7', '\u5309', '\u4e15',
			'\u56e8', '\u527d', '\u6c15', '\u59d8', '\u4e52', '\u948b',
			'\u5256', '\u4ec6', '\u4e03', '\u6390', '\u5343', '\u545b',
			'\u6084', '\u767f', '\u4eb2', '\u72c5', '\u828e', '\u4e18',
			'\u533a', '\u5cd1', '\u7f3a', '\u590b', '\u5465', '\u7a63',
			'\u5a06', '\u60f9', '\u4eba', '\u6254', '\u65e5', '\u8338',
			'\u53b9', '\u909a', '\u633c', '\u5827', '\u5a51', '\u77a4',
			'\u637c', '\u4ee8', '\u6be2', '\u4e09', '\u6852', '\u63bb',
			'\u95aa', '\u68ee', '\u50e7', '\u6740', '\u7b5b', '\u5c71',
			'\u4f24', '\u5f30', '\u5962', '\u7533', '\u8398', '\u6552',
			'\u5347', '\u5c38', '\u53ce', '\u4e66', '\u5237', '\u8870',
			'\u95e9', '\u53cc', '\u8c01', '\u542e', '\u8bf4', '\u53b6',
			'\u5fea', '\u635c', '\u82cf', '\u72fb', '\u590a', '\u5b59',
			'\u5506', '\u4ed6', '\u56fc', '\u574d', '\u6c64', '\u5932',
			'\u5fd1', '\u71a5', '\u5254', '\u5929', '\u65eb', '\u5e16',
			'\u5385', '\u56f2', '\u5077', '\u51f8', '\u6e4d', '\u63a8',
			'\u541e', '\u4e47', '\u7a75', '\u6b6a', '\u5f2f', '\u5c23',
			'\u5371', '\u6637', '\u7fc1', '\u631d', '\u4e4c', '\u5915',
			'\u8672', '\u4eda', '\u4e61', '\u7071', '\u4e9b', '\u5fc3',
			'\u661f', '\u51f6', '\u4f11', '\u5401', '\u5405', '\u524a',
			'\u5743', '\u4e2b', '\u6079', '\u592e', '\u5e7a', '\u503b',
			'\u4e00', '\u56d9', '\u5e94', '\u54df', '\u4f63', '\u4f18',
			'\u625c', '\u56e6', '\u66f0', '\u6655', '\u7b60', '\u7b7c',
			'\u5e00', '\u707d', '\u5142', '\u5328', '\u50ae', '\u5219',
			'\u8d3c', '\u600e', '\u5897', '\u624e', '\u635a', '\u6cbe',
			'\u5f20', '\u957f', '\u9577', '\u4f4b', '\u8707', '\u8d1e',
			'\u4e89', '\u4e4b', '\u5cd9', '\u5ea2', '\u4e2d', '\u5dde',
			'\u6731', '\u6293', '\u62fd', '\u4e13', '\u5986', '\u96b9',
			'\u5b92', '\u5353', '\u4e72', '\u5b97', '\u90b9', '\u79df',
			'\u94bb', '\u539c', '\u5c0a', '\u6628', '\u5159', '\u9fc3',
			'\u9fc4', };

	/**
	 * Pinyin array.
	 *
	 * Each pinyin is corresponding to unihans of same offset in the unihans
	 * array.
	 */
	public static final byte[][] PINYINS = { { 65, 0, 0, 0, 0, 0 },
			{ 65, 73, 0, 0, 0, 0 }, { 65, 78, 0, 0, 0, 0 },
			{ 65, 78, 71, 0, 0, 0 }, { 65, 79, 0, 0, 0, 0 },
			{ 66, 65, 0, 0, 0, 0 }, { 66, 65, 73, 0, 0, 0 },
			{ 66, 65, 78, 0, 0, 0 }, { 66, 65, 78, 71, 0, 0 },
			{ 66, 65, 79, 0, 0, 0 }, { 66, 69, 73, 0, 0, 0 },
			{ 66, 69, 78, 0, 0, 0 }, { 66, 69, 78, 71, 0, 0 },
			{ 66, 73, 0, 0, 0, 0 }, { 66, 73, 65, 78, 0, 0 },
			{ 66, 73, 65, 79, 0, 0 }, { 66, 73, 69, 0, 0, 0 },
			{ 66, 73, 78, 0, 0, 0 }, { 66, 73, 78, 71, 0, 0 },
			{ 66, 79, 0, 0, 0, 0 }, { 66, 85, 0, 0, 0, 0 },
			{ 67, 65, 0, 0, 0, 0 }, { 67, 65, 73, 0, 0, 0 },
			{ 67, 65, 78, 0, 0, 0 }, { 67, 65, 78, 71, 0, 0 },
			{ 67, 65, 79, 0, 0, 0 }, { 67, 69, 0, 0, 0, 0 },
			{ 67, 69, 78, 0, 0, 0 }, { 67, 69, 78, 71, 0, 0 },
			{ 90, 69, 78, 71, 0, 0 }, { 67, 69, 78, 71, 0, 0 },
			{ 67, 72, 65, 0, 0, 0 }, { 67, 72, 65, 73, 0, 0 },
			{ 67, 72, 65, 78, 0, 0 }, { 67, 72, 65, 78, 71, 0 },
			{ 67, 72, 65, 79, 0, 0 }, { 67, 72, 69, 0, 0, 0 },
			{ 67, 72, 69, 78, 0, 0 }, { 83, 72, 69, 78, 0, 0 },
			{ 67, 72, 69, 78, 0, 0 }, { 67, 72, 69, 78, 71, 0 },
			{ 67, 72, 73, 0, 0, 0 }, { 67, 72, 79, 78, 71, 0 },
			{ 67, 72, 79, 85, 0, 0 }, { 67, 72, 85, 0, 0, 0 },
			{ 67, 72, 85, 65, 0, 0 }, { 67, 72, 85, 65, 73, 0 },
			{ 67, 72, 85, 65, 78, 0 }, { 67, 72, 85, 65, 78, 71 },
			{ 67, 72, 85, 73, 0, 0 }, { 67, 72, 85, 78, 0, 0 },
			{ 67, 72, 85, 79, 0, 0 }, { 67, 73, 0, 0, 0, 0 },
			{ 67, 79, 78, 71, 0, 0 }, { 67, 79, 85, 0, 0, 0 },
			{ 67, 85, 0, 0, 0, 0 }, { 67, 85, 65, 78, 0, 0 },
			{ 67, 85, 73, 0, 0, 0 }, { 67, 85, 78, 0, 0, 0 },
			{ 67, 85, 79, 0, 0, 0 }, { 68, 65, 0, 0, 0, 0 },
			{ 68, 65, 73, 0, 0, 0 }, { 68, 65, 78, 0, 0, 0 },
			{ 68, 65, 78, 71, 0, 0 }, { 68, 65, 79, 0, 0, 0 },
			{ 68, 69, 0, 0, 0, 0 }, { 68, 69, 78, 0, 0, 0 },
			{ 68, 69, 78, 71, 0, 0 }, { 68, 73, 0, 0, 0, 0 },
			{ 68, 73, 65, 0, 0, 0 }, { 68, 73, 65, 78, 0, 0 },
			{ 68, 73, 65, 79, 0, 0 }, { 68, 73, 69, 0, 0, 0 },
			{ 68, 73, 78, 71, 0, 0 }, { 68, 73, 85, 0, 0, 0 },
			{ 68, 79, 78, 71, 0, 0 }, { 68, 79, 85, 0, 0, 0 },
			{ 68, 85, 0, 0, 0, 0 }, { 68, 85, 65, 78, 0, 0 },
			{ 68, 85, 73, 0, 0, 0 }, { 68, 85, 78, 0, 0, 0 },
			{ 68, 85, 79, 0, 0, 0 }, { 69, 0, 0, 0, 0, 0 },
			{ 69, 73, 0, 0, 0, 0 }, { 69, 78, 0, 0, 0, 0 },
			{ 69, 78, 71, 0, 0, 0 }, { 69, 82, 0, 0, 0, 0 },
			{ 70, 65, 0, 0, 0, 0 }, { 70, 65, 78, 0, 0, 0 },
			{ 70, 65, 78, 71, 0, 0 }, { 70, 69, 73, 0, 0, 0 },
			{ 70, 69, 78, 0, 0, 0 }, { 70, 69, 78, 71, 0, 0 },
			{ 70, 73, 65, 79, 0, 0 }, { 70, 79, 0, 0, 0, 0 },
			{ 70, 79, 85, 0, 0, 0 }, { 70, 85, 0, 0, 0, 0 },
			{ 71, 65, 0, 0, 0, 0 }, { 71, 65, 73, 0, 0, 0 },
			{ 71, 65, 78, 0, 0, 0 }, { 71, 65, 78, 71, 0, 0 },
			{ 71, 65, 79, 0, 0, 0 }, { 71, 69, 0, 0, 0, 0 },
			{ 71, 69, 73, 0, 0, 0 }, { 71, 69, 78, 0, 0, 0 },
			{ 71, 69, 78, 71, 0, 0 }, { 71, 79, 78, 71, 0, 0 },
			{ 71, 79, 85, 0, 0, 0 }, { 71, 85, 0, 0, 0, 0 },
			{ 71, 85, 65, 0, 0, 0 }, { 71, 85, 65, 73, 0, 0 },
			{ 71, 85, 65, 78, 0, 0 }, { 71, 85, 65, 78, 71, 0 },
			{ 71, 85, 73, 0, 0, 0 }, { 71, 85, 78, 0, 0, 0 },
			{ 71, 85, 79, 0, 0, 0 }, { 72, 65, 0, 0, 0, 0 },
			{ 72, 65, 73, 0, 0, 0 }, { 72, 65, 78, 0, 0, 0 },
			{ 72, 65, 78, 71, 0, 0 }, { 72, 65, 79, 0, 0, 0 },
			{ 72, 69, 0, 0, 0, 0 }, { 72, 69, 73, 0, 0, 0 },
			{ 72, 69, 78, 0, 0, 0 }, { 72, 69, 78, 71, 0, 0 },
			{ 72, 77, 0, 0, 0, 0 }, { 72, 79, 78, 71, 0, 0 },
			{ 72, 79, 85, 0, 0, 0 }, { 72, 85, 0, 0, 0, 0 },
			{ 72, 85, 65, 0, 0, 0 }, { 72, 85, 65, 73, 0, 0 },
			{ 72, 85, 65, 78, 0, 0 }, { 72, 85, 65, 78, 71, 0 },
			{ 72, 85, 73, 0, 0, 0 }, { 72, 85, 78, 0, 0, 0 },
			{ 72, 85, 79, 0, 0, 0 }, { 74, 73, 0, 0, 0, 0 },
			{ 74, 73, 65, 0, 0, 0 }, { 74, 73, 65, 78, 0, 0 },
			{ 74, 73, 65, 78, 71, 0 }, { 74, 73, 65, 79, 0, 0 },
			{ 74, 73, 69, 0, 0, 0 }, { 74, 73, 78, 0, 0, 0 },
			{ 74, 73, 78, 71, 0, 0 }, { 74, 73, 79, 78, 71, 0 },
			{ 74, 73, 85, 0, 0, 0 }, { 74, 85, 0, 0, 0, 0 },
			{ 74, 85, 65, 78, 0, 0 }, { 74, 85, 69, 0, 0, 0 },
			{ 74, 85, 78, 0, 0, 0 }, { 75, 65, 0, 0, 0, 0 },
			{ 75, 65, 73, 0, 0, 0 }, { 75, 65, 78, 0, 0, 0 },
			{ 75, 65, 78, 71, 0, 0 }, { 75, 65, 79, 0, 0, 0 },
			{ 75, 69, 0, 0, 0, 0 }, { 75, 69, 78, 0, 0, 0 },
			{ 75, 69, 78, 71, 0, 0 }, { 75, 79, 78, 71, 0, 0 },
			{ 75, 79, 85, 0, 0, 0 }, { 75, 85, 0, 0, 0, 0 },
			{ 75, 85, 65, 0, 0, 0 }, { 75, 85, 65, 73, 0, 0 },
			{ 75, 85, 65, 78, 0, 0 }, { 75, 85, 65, 78, 71, 0 },
			{ 75, 85, 73, 0, 0, 0 }, { 75, 85, 78, 0, 0, 0 },
			{ 75, 85, 79, 0, 0, 0 }, { 76, 65, 0, 0, 0, 0 },
			{ 76, 65, 73, 0, 0, 0 }, { 76, 65, 78, 0, 0, 0 },
			{ 76, 65, 78, 71, 0, 0 }, { 76, 65, 79, 0, 0, 0 },
			{ 76, 69, 0, 0, 0, 0 }, { 76, 69, 73, 0, 0, 0 },
			{ 76, 69, 78, 71, 0, 0 }, { 76, 73, 0, 0, 0, 0 },
			{ 76, 73, 65, 0, 0, 0 }, { 76, 73, 65, 78, 0, 0 },
			{ 76, 73, 65, 78, 71, 0 }, { 76, 73, 65, 79, 0, 0 },
			{ 76, 73, 69, 0, 0, 0 }, { 76, 73, 78, 0, 0, 0 },
			{ 76, 73, 78, 71, 0, 0 }, { 76, 73, 85, 0, 0, 0 },
			{ 76, 79, 0, 0, 0, 0 }, { 76, 79, 78, 71, 0, 0 },
			{ 76, 79, 85, 0, 0, 0 }, { 76, 85, 0, 0, 0, 0 },
			{ 76, 85, 65, 78, 0, 0 }, { 76, 85, 69, 0, 0, 0 },
			{ 76, 85, 78, 0, 0, 0 }, { 76, 85, 79, 0, 0, 0 },
			{ 77, 0, 0, 0, 0, 0 }, { 77, 65, 0, 0, 0, 0 },
			{ 77, 65, 73, 0, 0, 0 }, { 77, 65, 78, 0, 0, 0 },
			{ 77, 65, 78, 71, 0, 0 }, { 77, 65, 79, 0, 0, 0 },
			{ 77, 69, 0, 0, 0, 0 }, { 77, 69, 73, 0, 0, 0 },
			{ 77, 69, 78, 0, 0, 0 }, { 77, 69, 78, 71, 0, 0 },
			{ 77, 73, 0, 0, 0, 0 }, { 77, 73, 65, 78, 0, 0 },
			{ 77, 73, 65, 79, 0, 0 }, { 77, 73, 69, 0, 0, 0 },
			{ 77, 73, 78, 0, 0, 0 }, { 77, 73, 78, 71, 0, 0 },
			{ 77, 73, 85, 0, 0, 0 }, { 77, 79, 0, 0, 0, 0 },
			{ 77, 79, 85, 0, 0, 0 }, { 77, 85, 0, 0, 0, 0 },
			{ 78, 0, 0, 0, 0, 0 }, { 78, 65, 0, 0, 0, 0 },
			{ 78, 65, 73, 0, 0, 0 }, { 78, 65, 78, 0, 0, 0 },
			{ 78, 65, 78, 71, 0, 0 }, { 78, 65, 79, 0, 0, 0 },
			{ 78, 69, 0, 0, 0, 0 }, { 78, 69, 73, 0, 0, 0 },
			{ 78, 69, 78, 0, 0, 0 }, { 78, 69, 78, 71, 0, 0 },
			{ 78, 73, 0, 0, 0, 0 }, { 78, 73, 65, 78, 0, 0 },
			{ 78, 73, 65, 78, 71, 0 }, { 78, 73, 65, 79, 0, 0 },
			{ 78, 73, 69, 0, 0, 0 }, { 78, 73, 78, 0, 0, 0 },
			{ 78, 73, 78, 71, 0, 0 }, { 78, 73, 85, 0, 0, 0 },
			{ 78, 79, 78, 71, 0, 0 }, { 78, 79, 85, 0, 0, 0 },
			{ 78, 85, 0, 0, 0, 0 }, { 78, 85, 65, 78, 0, 0 },
			{ 78, 85, 69, 0, 0, 0 }, { 78, 85, 78, 0, 0, 0 },
			{ 78, 85, 79, 0, 0, 0 }, { 79, 0, 0, 0, 0, 0 },
			{ 79, 85, 0, 0, 0, 0 }, { 80, 65, 0, 0, 0, 0 },
			{ 80, 65, 73, 0, 0, 0 }, { 80, 65, 78, 0, 0, 0 },
			{ 80, 65, 78, 71, 0, 0 }, { 80, 65, 79, 0, 0, 0 },
			{ 80, 69, 73, 0, 0, 0 }, { 80, 69, 78, 0, 0, 0 },
			{ 80, 69, 78, 71, 0, 0 }, { 80, 73, 0, 0, 0, 0 },
			{ 80, 73, 65, 78, 0, 0 }, { 80, 73, 65, 79, 0, 0 },
			{ 80, 73, 69, 0, 0, 0 }, { 80, 73, 78, 0, 0, 0 },
			{ 80, 73, 78, 71, 0, 0 }, { 80, 79, 0, 0, 0, 0 },
			{ 80, 79, 85, 0, 0, 0 }, { 80, 85, 0, 0, 0, 0 },
			{ 81, 73, 0, 0, 0, 0 }, { 81, 73, 65, 0, 0, 0 },
			{ 81, 73, 65, 78, 0, 0 }, { 81, 73, 65, 78, 71, 0 },
			{ 81, 73, 65, 79, 0, 0 }, { 81, 73, 69, 0, 0, 0 },
			{ 81, 73, 78, 0, 0, 0 }, { 81, 73, 78, 71, 0, 0 },
			{ 81, 73, 79, 78, 71, 0 }, { 81, 73, 85, 0, 0, 0 },
			{ 81, 85, 0, 0, 0, 0 }, { 81, 85, 65, 78, 0, 0 },
			{ 81, 85, 69, 0, 0, 0 }, { 81, 85, 78, 0, 0, 0 },
			{ 82, 65, 78, 0, 0, 0 }, { 82, 65, 78, 71, 0, 0 },
			{ 82, 65, 79, 0, 0, 0 }, { 82, 69, 0, 0, 0, 0 },
			{ 82, 69, 78, 0, 0, 0 }, { 82, 69, 78, 71, 0, 0 },
			{ 82, 73, 0, 0, 0, 0 }, { 82, 79, 78, 71, 0, 0 },
			{ 82, 79, 85, 0, 0, 0 }, { 82, 85, 0, 0, 0, 0 },
			{ 82, 85, 65, 0, 0, 0 }, { 82, 85, 65, 78, 0, 0 },
			{ 82, 85, 73, 0, 0, 0 }, { 82, 85, 78, 0, 0, 0 },
			{ 82, 85, 79, 0, 0, 0 }, { 83, 65, 0, 0, 0, 0 },
			{ 83, 65, 73, 0, 0, 0 }, { 83, 65, 78, 0, 0, 0 },
			{ 83, 65, 78, 71, 0, 0 }, { 83, 65, 79, 0, 0, 0 },
			{ 83, 69, 0, 0, 0, 0 }, { 83, 69, 78, 0, 0, 0 },
			{ 83, 69, 78, 71, 0, 0 }, { 83, 72, 65, 0, 0, 0 },
			{ 83, 72, 65, 73, 0, 0 }, { 83, 72, 65, 78, 0, 0 },
			{ 83, 72, 65, 78, 71, 0 }, { 83, 72, 65, 79, 0, 0 },
			{ 83, 72, 69, 0, 0, 0 }, { 83, 72, 69, 78, 0, 0 },
			{ 88, 73, 78, 0, 0, 0 }, { 83, 72, 69, 78, 0, 0 },
			{ 83, 72, 69, 78, 71, 0 }, { 83, 72, 73, 0, 0, 0 },
			{ 83, 72, 79, 85, 0, 0 }, { 83, 72, 85, 0, 0, 0 },
			{ 83, 72, 85, 65, 0, 0 }, { 83, 72, 85, 65, 73, 0 },
			{ 83, 72, 85, 65, 78, 0 }, { 83, 72, 85, 65, 78, 71 },
			{ 83, 72, 85, 73, 0, 0 }, { 83, 72, 85, 78, 0, 0 },
			{ 83, 72, 85, 79, 0, 0 }, { 83, 73, 0, 0, 0, 0 },
			{ 83, 79, 78, 71, 0, 0 }, { 83, 79, 85, 0, 0, 0 },
			{ 83, 85, 0, 0, 0, 0 }, { 83, 85, 65, 78, 0, 0 },
			{ 83, 85, 73, 0, 0, 0 }, { 83, 85, 78, 0, 0, 0 },
			{ 83, 85, 79, 0, 0, 0 }, { 84, 65, 0, 0, 0, 0 },
			{ 84, 65, 73, 0, 0, 0 }, { 84, 65, 78, 0, 0, 0 },
			{ 84, 65, 78, 71, 0, 0 }, { 84, 65, 79, 0, 0, 0 },
			{ 84, 69, 0, 0, 0, 0 }, { 84, 69, 78, 71, 0, 0 },
			{ 84, 73, 0, 0, 0, 0 }, { 84, 73, 65, 78, 0, 0 },
			{ 84, 73, 65, 79, 0, 0 }, { 84, 73, 69, 0, 0, 0 },
			{ 84, 73, 78, 71, 0, 0 }, { 84, 79, 78, 71, 0, 0 },
			{ 84, 79, 85, 0, 0, 0 }, { 84, 85, 0, 0, 0, 0 },
			{ 84, 85, 65, 78, 0, 0 }, { 84, 85, 73, 0, 0, 0 },
			{ 84, 85, 78, 0, 0, 0 }, { 84, 85, 79, 0, 0, 0 },
			{ 87, 65, 0, 0, 0, 0 }, { 87, 65, 73, 0, 0, 0 },
			{ 87, 65, 78, 0, 0, 0 }, { 87, 65, 78, 71, 0, 0 },
			{ 87, 69, 73, 0, 0, 0 }, { 87, 69, 78, 0, 0, 0 },
			{ 87, 69, 78, 71, 0, 0 }, { 87, 79, 0, 0, 0, 0 },
			{ 87, 85, 0, 0, 0, 0 }, { 88, 73, 0, 0, 0, 0 },
			{ 88, 73, 65, 0, 0, 0 }, { 88, 73, 65, 78, 0, 0 },
			{ 88, 73, 65, 78, 71, 0 }, { 88, 73, 65, 79, 0, 0 },
			{ 88, 73, 69, 0, 0, 0 }, { 88, 73, 78, 0, 0, 0 },
			{ 88, 73, 78, 71, 0, 0 }, { 88, 73, 79, 78, 71, 0 },
			{ 88, 73, 85, 0, 0, 0 }, { 88, 85, 0, 0, 0, 0 },
			{ 88, 85, 65, 78, 0, 0 }, { 88, 85, 69, 0, 0, 0 },
			{ 88, 85, 78, 0, 0, 0 }, { 89, 65, 0, 0, 0, 0 },
			{ 89, 65, 78, 0, 0, 0 }, { 89, 65, 78, 71, 0, 0 },
			{ 89, 65, 79, 0, 0, 0 }, { 89, 69, 0, 0, 0, 0 },
			{ 89, 73, 0, 0, 0, 0 }, { 89, 73, 78, 0, 0, 0 },
			{ 89, 73, 78, 71, 0, 0 }, { 89, 79, 0, 0, 0, 0 },
			{ 89, 79, 78, 71, 0, 0 }, { 89, 79, 85, 0, 0, 0 },
			{ 89, 85, 0, 0, 0, 0 }, { 89, 85, 65, 78, 0, 0 },
			{ 89, 85, 69, 0, 0, 0 }, { 89, 85, 78, 0, 0, 0 },
			{ 74, 85, 78, 0, 0, 0 }, { 89, 85, 78, 0, 0, 0 },
			{ 90, 65, 0, 0, 0, 0 }, { 90, 65, 73, 0, 0, 0 },
			{ 90, 65, 78, 0, 0, 0 }, { 90, 65, 78, 71, 0, 0 },
			{ 90, 65, 79, 0, 0, 0 }, { 90, 69, 0, 0, 0, 0 },
			{ 90, 69, 73, 0, 0, 0 }, { 90, 69, 78, 0, 0, 0 },
			{ 90, 69, 78, 71, 0, 0 }, { 90, 72, 65, 0, 0, 0 },
			{ 90, 72, 65, 73, 0, 0 }, { 90, 72, 65, 78, 0, 0 },
			{ 90, 72, 65, 78, 71, 0 }, { 67, 72, 65, 78, 71, 0 },
			{ 90, 72, 65, 78, 71, 0 }, { 90, 72, 65, 79, 0, 0 },
			{ 90, 72, 69, 0, 0, 0 }, { 90, 72, 69, 78, 0, 0 },
			{ 90, 72, 69, 78, 71, 0 }, { 90, 72, 73, 0, 0, 0 },
			{ 83, 72, 73, 0, 0, 0 }, { 90, 72, 73, 0, 0, 0 },
			{ 90, 72, 79, 78, 71, 0 }, { 90, 72, 79, 85, 0, 0 },
			{ 90, 72, 85, 0, 0, 0 }, { 90, 72, 85, 65, 0, 0 },
			{ 90, 72, 85, 65, 73, 0 }, { 90, 72, 85, 65, 78, 0 },
			{ 90, 72, 85, 65, 78, 71 }, { 90, 72, 85, 73, 0, 0 },
			{ 90, 72, 85, 78, 0, 0 }, { 90, 72, 85, 79, 0, 0 },
			{ 90, 73, 0, 0, 0, 0 }, { 90, 79, 78, 71, 0, 0 },
			{ 90, 79, 85, 0, 0, 0 }, { 90, 85, 0, 0, 0, 0 },
			{ 90, 85, 65, 78, 0, 0 }, { 90, 85, 73, 0, 0, 0 },
			{ 90, 85, 78, 0, 0, 0 }, { 90, 85, 79, 0, 0, 0 },
			{ 0, 0, 0, 0, 0, 0 }, { 83, 72, 65, 78, 0, 0 },
			{ 0, 0, 0, 0, 0, 0 }, };

	/**
	 * First and last Chinese character with known Pinyin according to zh
	 * collation
	 */
	private static final String FIRST_PINYIN_UNIHAN = "\u963F";
	private static final String LAST_PINYIN_UNIHAN = "\u9FFF";

	private static final Collator COLLATOR = Collator.getInstance(Locale.CHINA);

	private static HanziToPinyin sInstance;
	private final boolean mHasChinaCollator;

	public static class Token {
		/**
		 * Separator between target string for each source char
		 */
		public static final String SEPARATOR = " ";

		public static final int LATIN = 1;
		public static final int PINYIN = 2;
		public static final int UNKNOWN = 3;

		public Token() {
		}

		public Token(int type, String source, String target) {
			this.type = type;
			this.source = source;
			this.target = target;
		}

		/**
		 * Type of this token, ASCII, PINYIN or UNKNOWN.
		 */
		public int type;
		/**
		 * Original string before translation.
		 */
		public String source;
		/**
		 * Translated string of source. For Han, target is corresponding Pinyin.
		 * Otherwise target is original string in source.
		 */
		public String target;
	}

	protected HanziToPinyin(boolean hasChinaCollator) {
		mHasChinaCollator = hasChinaCollator;
	}

	public static HanziToPinyin getInstance() {
		synchronized (HanziToPinyin.class) {
			if (sInstance != null) {
				return sInstance;
			}
			// Check if zh_CN collation data is available
			final Locale locale[] = Collator.getAvailableLocales();

			// 增加的代码,增强。
			final Locale chinaAddition = new Locale("zh");

			for (int i = 0; i < locale.length; i++) {
				if (locale[i].equals(Locale.CHINA)
						|| locale[i].equals(chinaAddition)) {
					// Do self validation just once.
					if (DEBUG) {
						Log.d(TAG, "Self validation. Result: "
								+ doSelfValidation());
					}
					sInstance = new HanziToPinyin(true);
					return sInstance;
				}
			}
			Log.w(TAG,
					"There is no Chinese collator, HanziToPinyin is disabled");
			sInstance = new HanziToPinyin(false);
			return sInstance;
		}
	}

	/**
	 * Validate if our internal table has some wrong value.
	 *
	 * @return true when the table looks correct.
	 */
	private static boolean doSelfValidation() {
		char lastChar = UNIHANS[0];
		String lastString = Character.toString(lastChar);
		for (char c : UNIHANS) {
			if (lastChar == c) {
				continue;
			}
			final String curString = Character.toString(c);
			int cmp = COLLATOR.compare(lastString, curString);
			if (cmp >= 0) {
				Log.e(TAG, "Internal error in Unihan table. "
						+ "The last string \"" + lastString
						+ "\" is greater than current string \"" + curString
						+ "\".");
				return false;
			}
			lastString = curString;
		}
		return true;
	}

	private Token getToken(char character) {
		Token token = new Token();
		final String letter = Character.toString(character);
		token.source = letter;
		int offset = -1;
		int cmp;
		if (character < 256) {
			token.type = Token.LATIN;
			token.target = letter;
			return token;
		} else {
			cmp = COLLATOR.compare(letter, FIRST_PINYIN_UNIHAN);
			if (cmp < 0) {
				token.type = Token.UNKNOWN;
				token.target = letter;
				return token;
			} else if (cmp == 0) {
				token.type = Token.PINYIN;
				offset = 0;
			} else {
				cmp = COLLATOR.compare(letter, LAST_PINYIN_UNIHAN);
				if (cmp > 0) {
					token.type = Token.UNKNOWN;
					token.target = letter;
					return token;
				} else if (cmp == 0) {
					token.type = Token.PINYIN;
					offset = UNIHANS.length - 1;
				}
			}
		}

		token.type = Token.PINYIN;
		if (offset < 0) {
			int begin = 0;
			int end = UNIHANS.length - 1;
			while (begin <= end) {
				offset = (begin + end) / 2;
				final String unihan = Character.toString(UNIHANS[offset]);
				cmp = COLLATOR.compare(letter, unihan);
				if (cmp == 0) {
					break;
				} else if (cmp > 0) {
					begin = offset + 1;
				} else {
					end = offset - 1;
				}
			}
		}
		if (cmp < 0) {
			offset--;
		}
		StringBuilder pinyin = new StringBuilder();
		for (int j = 0; j < PINYINS[offset].length && PINYINS[offset][j] != 0; j++) {
			pinyin.append((char) PINYINS[offset][j]);
		}
		token.target = pinyin.toString();
		if (TextUtils.isEmpty(token.target)) {
			token.type = Token.UNKNOWN;
			token.target = token.source;
		}
		return token;
	}

	/**
	 * Convert the input to a array of tokens. The sequence of ASCII or Unknown
	 * characters without space will be put into a Token, One Hanzi character
	 * which has pinyin will be treated as a Token. If these is no China
	 * collator, the empty token array is returned.
	 */
	public ArrayList<Token> get(final String input) {
		ArrayList<Token> tokens = new ArrayList<Token>();
		if (!mHasChinaCollator || TextUtils.isEmpty(input)) {
			// return empty tokens.
			return tokens;
		}
		final int inputLength = input.length();
		final StringBuilder sb = new StringBuilder();
		int tokenType = Token.LATIN;
		// Go through the input, create a new token when
		// a. Token type changed
		// b. Get the Pinyin of current charater.
		// c. current character is space.
		for (int i = 0; i < inputLength; i++) {
			final char character = input.charAt(i);
			if (character == ' ') {
				if (sb.length() > 0) {
					addToken(sb, tokens, tokenType);
				}
			} else if (character < 256) {
				if (tokenType != Token.LATIN && sb.length() > 0) {
					addToken(sb, tokens, tokenType);
				}
				tokenType = Token.LATIN;
				sb.append(character);
			} else {
				Token t = getToken(character);
				if (t.type == Token.PINYIN) {
					if (sb.length() > 0) {
						addToken(sb, tokens, tokenType);
					}
					tokens.add(t);
					tokenType = Token.PINYIN;
				} else {
					if (tokenType != t.type && sb.length() > 0) {
						addToken(sb, tokens, tokenType);
					}
					tokenType = t.type;
					sb.append(character);
				}
			}
		}
		if (sb.length() > 0) {
			addToken(sb, tokens, tokenType);
		}
		return tokens;
	}

	private void addToken(final StringBuilder sb,
			final ArrayList<Token> tokens, final int tokenType) {
		String str = sb.toString();
		tokens.add(new Token(tokenType, str, str));
		sb.setLength(0);
	}
}

写一个MainActivity.java测试汉字转化为汉语拼音输出的效果:

package zhangphil.hanyupinyin;

import java.util.ArrayList;

import zhangphil.hanyupinyin.HanziToPinyin.Token;
import android.app.Activity;
import android.os.Bundle;

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		String s = "安卓";
		System.out.println("汉字转拼音输出: " + getPinYin(s));
	}

	// 输入汉字返回拼音的通用方法函数。
	public static String getPinYin(String hanzi) {
		ArrayList<Token> tokens = HanziToPinyin.getInstance().get(hanzi);
		StringBuilder sb = new StringBuilder();
		if (tokens != null && tokens.size() > 0) {
			for (Token token : tokens) {
				if (Token.PINYIN == token.type) {
					sb.append(token.target);
				} else {
					sb.append(token.source);
				}
			}
		}

		return sb.toString().toUpperCase();
	}
}

结果输出如图:

  • 2
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
提供的源码资源涵盖了Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 适合毕业设计、课程设计作业。这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。 所有源码均经过严格测试,可以直接运行,可以放心下载使用。有任何使用问题欢迎随时与博主沟通,第一时间进行解答!
好的,让我来回答你的问题。 首先,你需要在你的布局文件中添加一个RecyclerView,如下所示: ```xml <androidx.recyclerview.widget.RecyclerView android:id="@+id/recyclerView" android:layout_width="match_parent" android:layout_height="match_parent" /> ``` 然后,在你的Activity或Fragment中,你需要定义一个适配器(Adapter)和数据源(List)用于为RecyclerView提供数据。在这个例子中,我们将使用一个包含应用程序包名的字符串列表来作为数据源。 ```java private List<String> packageNameList = new ArrayList<>(); private RecyclerView recyclerView; private PackageListAdapter adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); recyclerView = findViewById(R.id.recyclerView); // 获取所有应用程序包名 PackageManager pm = getPackageManager(); List<ApplicationInfo> packages = pm.getInstalledApplications(PackageManager.GET_META_DATA); for (ApplicationInfo packageInfo : packages) { packageNameList.add(packageInfo.packageName); } // 对包名按照拼音首字母进行排序 Collections.sort(packageNameList, new PinyinComparator()); // 初始化RecyclerView adapter = new PackageListAdapter(this, packageNameList); recyclerView.setLayoutManager(new LinearLayoutManager(this)); recyclerView.setAdapter(adapter); } ``` 在上面的代码中,我们使用PackageManager获取了所有应用程序的包名,并将它们添加到了一个字符串列表(packageNameList)中。然后,我们使用PinyinComparator类对包名列表进行排序,以便按照拼音首字母进行显示。最后,我们初始化了RecyclerView,将适配器(Adapter)和数据源(List)传递给RecyclerView。 接下来,我们需要定义一个用于将汉字换为拼音的PinyinUtil类。在这个类中,我们将使用android系统自带的HanziToPinyin类来实现汉字拼音的功能。 ```java public class PinyinUtil { /** * 将汉字换为拼音 * * @param chinese 中文字符串 * @return 拼音字符串 */ public static String getPinyin(String chinese) { StringBuilder pinyin = new StringBuilder(); char[] chars = chinese.toCharArray(); for (char c : chars) { String[] pinyinArray = HanziToPinyin.getInstance().get(c); if (pinyinArray != null && pinyinArray.length > 0) { pinyin.append(pinyinArray[0]); } else { pinyin.append(c); } } return pinyin.toString(); } } ``` 在上面的代码中,我们定义了一个getPinyin方法,它将一个中文字符串作为参数并返回一个拼音字符串。在这个方法中,我们首先将中文字符串换为一个字符数组,然后遍历每个字符。对于每个字符,我们使用HanziToPinyin类获取其对应的拼音数组,然后将第一个拼音添加到StringBuilder对象中。如果该字符没有对应的拼音,则将其原样添加到StringBuilder对象中。最后,我们将StringBuilder对象换为一个字符串并返回。 最后,我们需要定义一个PinyinComparator类,它将用于按照拼音首字母对包名进行排序。 ```java public class PinyinComparator implements Comparator<String> { /** * 比较两个包名的拼音首字母 * * @param o1 包名1 * @param o2 包名2 * @return 比较结果 */ @Override public int compare(String o1, String o2) { String pinyin1 = PinyinUtil.getPinyin(o1); String pinyin2 = PinyinUtil.getPinyin(o2); return pinyin1.compareToIgnoreCase(pinyin2); } } ``` 在上面的代码中,我们定义了一个compare方法,它将两个包名作为参数并返回一个整数。在这个方法中,我们首先将两个包名分别换为拼音字符串,然后使用String类的compareToIgnoreCase方法比较它们的拼音首字母。如果两个包名的拼音首字母相同,则比较它们的Unicode编码。 现在,我们已经完成了实现,可以通过运行应用程序来测试它。当我们滚动RecyclerView时,字幕条索引将会显示在右侧,点击字幕条索引将会自动滚动到相应的位置。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值