android将中文转换为字母
2015-08-07 17:21
495 查看
资源连接:http://download.csdn.net/detail/txzdl/8974547
主要是修改android源码中的两个类,这是根据前辈的经验修改而来,具体文章出处已经找不到,具体项目在上面链接中
其中一个类如下:
package com.example.spelldemo;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import com.example.spelldemo.HanziToPinyin.Token;
import android.text.TextUtils;
import android.util.SparseArray;
import android.view.TextureView;
public class ContactLocaleUtils {
public interface FullNameStyle {
public static final int UNDEFINED = 0;
public static final int WESTERN = 1;
/**
* Used if the name is written in Hanzi/Kanji/Hanja and we could not determine
* which specific language it belongs to: Chinese, Japanese or Korean.
*/
public static final int CJK = 2;
public static final int CHINESE = 3;
public static final int JAPANESE = 4;
public static final int KOREAN = 5;
}
/**
* This class is the default implementation.
* <p>
* It should be the base class for other locales' implementation.
*/
public class ContactLocaleUtilsBase {
public String getSortKey(String displayName) {
return displayName;
}
@SuppressWarnings("unused")
public Iterator<String> getNameLookupKeys(String name) {
return null;
}
}
/**
* The classes to generate the Chinese style sort and search keys.
* <p>
* The sorting key is generated as each Chinese character' pinyin proceeding with
* space and character itself. If the character's pinyin unable to find, the character
* itself will be used.
* <p>
* The below additional name lookup keys will be generated.
* a. Chinese character's pinyin and pinyin's initial character.
* b. Latin word and the initial character for Latin word.
* The name lookup keys are generated to make sure the name can be found by from any
* initial character.
*/
private class ChineseContactUtils extends ContactLocaleUtilsBase {
@Override
public String getSortKey(String displayName) {
ArrayList<Token> tokens = HanziToPinyin.getInstance().get(displayName);
if (tokens != null && tokens.size() > 0) {
StringBuilder sb = new StringBuilder();
for (Token token : tokens) {
// Put Chinese character's pinyin, then proceed with the
// character itself.
if (Token.PINYIN == token.type) {
if (sb.length() > 0) {
sb.append(' ');
}
sb.append(token.target);
sb.append(' ');
sb.append(token.source);
} else {
if (sb.length() > 0) {
sb.append(' ');
}
sb.append(token.source);
}
}
return sb.toString();
}
return super.getSortKey(displayName);
}
@Override
public Iterator<String> getNameLookupKeys(String name) {
// TODO : Reduce the object allocation.
HashSet<String> keys = new HashSet<String>();
ArrayList<Token> tokens = HanziToPinyin.getInstance().get(name);
final int tokenCount = tokens.size();
final StringBuilder keyPinyin = new StringBuilder();
final StringBuilder keyInitial = new StringBuilder();
// There is no space among the Chinese Characters, the variant name
// lookup key wouldn't work for Chinese. The keyOrignal is used to
// build the lookup keys for itself.
final StringBuilder keyOrignal = new StringBuilder();
for (int i = tokenCount - 1; i >= 0; i--) {
final Token token = tokens.get(i);
if (Token.PINYIN == token.type) {
keyPinyin.insert(0, token.target);
keyInitial.insert(0, token.target.charAt(0));
} else if (Token.LATIN == token.type) {
// Avoid adding space at the end of String.
if (keyPinyin.length() > 0) {
keyPinyin.insert(0, ' ');
}
if (keyOrignal.length() > 0) {
keyOrignal.insert(0, ' ');
}
keyPinyin.insert(0, token.source);
keyInitial.insert(0, token.source.charAt(0));
}
keyOrignal.insert(0, token.source);
keys.add(keyOrignal.toString());
keys.add(keyPinyin.toString());
keys.add(keyInitial.toString());
}
return keys.iterator();
}
}
private static final String CHINESE_LANGUAGE = Locale.CHINESE.getLanguage().toLowerCase();
private static final String JAPANESE_LANGUAGE = Locale.JAPANESE.getLanguage().toLowerCase();
private static final String KOREAN_LANGUAGE = Locale.KOREAN.getLanguage().toLowerCase();
private static ContactLocaleUtils sSingleton;
private final SparseArray<ContactLocaleUtilsBase> mUtils =
new SparseArray<ContactLocaleUtilsBase>();
private final ContactLocaleUtilsBase mBase = new ContactLocaleUtilsBase();
private String mLanguage;
private ContactLocaleUtils() {
setLocale(null);
}
public void setLocale(Locale currentLocale) {
if (currentLocale == null) {
mLanguage = Locale.getDefault().getLanguage().toLowerCase();
} else {
mLanguage = currentLocale.getLanguage().toLowerCase();
}
}
//只返回汉字的拼音,例如:东方 DONG FANG
public String getSpell(String displayName, int nameStyle){
String full=getSortKey(displayName,nameStyle);
String result=SplitFullName(full,displayName);
return result;
}
/**
*
* @param full 东方 DONG 东 FANG 方
* @param name 东方
* @return
*/
private String SplitFullName(String full,String name){
char[] nameChar=name.toCharArray();
for(int i=0;i<nameChar.length;i++){
StringBuffer result = new StringBuffer();
Pattern p = Pattern.compile("[\u4e00-\u9fa5]");//判断是否为汉字
Matcher m = p.matcher(String.valueOf(nameChar[i]));
m = p.matcher(String.valueOf(nameChar[i]));
if (m.matches()) {
String[] temp=full.split(String.valueOf(nameChar[i]));
for(int j=0;j<temp.length;j++){
result.append(temp[j]);
}
}else{
result.append(full);
}
full=result.toString();
}
return full;
}
//返回拼音加文字,例如:东方 DONG 东 FANG 方
public String getSortKey(String displayName, int nameStyle) {
return getForSort(Integer.valueOf(nameStyle)).getSortKey(displayName);
}
public Iterator<String> getNameLookupKeys(String name, int nameStyle) {
return getForNameLookup(Integer.valueOf(nameStyle)).getNameLookupKeys(name);
}
/**
* Determine which utility should be used for generating NameLookupKey.
* <p>
* a. For Western style name, if the current language is Chinese, the
* ChineseContactUtils should be used.
* b. For Chinese and CJK style name if current language is neither Japanese or Korean,
* the ChineseContactUtils should be used.
*/
private ContactLocaleUtilsBase getForNameLookup(Integer nameStyle) {
int nameStyleInt = nameStyle.intValue();
Integer adjustedUtil = Integer.valueOf(getAdjustedStyle(nameStyleInt));
if (CHINESE_LANGUAGE.equals(mLanguage) && nameStyleInt == FullNameStyle.WESTERN) {
adjustedUtil = Integer.valueOf(FullNameStyle.CHINESE);
}
return get(adjustedUtil);
}
private synchronized ContactLocaleUtilsBase get(Integer nameStyle) {
ContactLocaleUtilsBase utils = mUtils.get(nameStyle);
if (utils == null) {
if (nameStyle.intValue() == FullNameStyle.CHINESE) {
utils = new ChineseContactUtils();
mUtils.put(nameStyle, utils);
}
}
return (utils == null) ? mBase : utils;
}
/**
* Determine the which utility should be used for generating sort key.
* <p>
* For Chinese and CJK style name if current language is neither Japanese or Korean,
* the ChineseContactUtils should be used.
*/
private ContactLocaleUtilsBase getForSort(Integer nameStyle) {
return get(Integer.valueOf(getAdjustedStyle(nameStyle.intValue())));
}
public static synchronized ContactLocaleUtils getIntance() {
if (sSingleton == null) {
sSingleton = new ContactLocaleUtils();
}
return sSingleton;
}
private int getAdjustedStyle(int nameStyle) {
if (nameStyle == FullNameStyle.CJK && !JAPANESE_LANGUAGE.equals(mLanguage) &&
!KOREAN_LANGUAGE.equals(mLanguage)) {
return FullNameStyle.CHINESE;
} else {
return nameStyle;
}
}
}
主要是修改android源码中的两个类,这是根据前辈的经验修改而来,具体文章出处已经找不到,具体项目在上面链接中
其中一个类如下:
package com.example.spelldemo;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import com.example.spelldemo.HanziToPinyin.Token;
import android.text.TextUtils;
import android.util.SparseArray;
import android.view.TextureView;
public class ContactLocaleUtils {
public interface FullNameStyle {
public static final int UNDEFINED = 0;
public static final int WESTERN = 1;
/**
* Used if the name is written in Hanzi/Kanji/Hanja and we could not determine
* which specific language it belongs to: Chinese, Japanese or Korean.
*/
public static final int CJK = 2;
public static final int CHINESE = 3;
public static final int JAPANESE = 4;
public static final int KOREAN = 5;
}
/**
* This class is the default implementation.
* <p>
* It should be the base class for other locales' implementation.
*/
public class ContactLocaleUtilsBase {
public String getSortKey(String displayName) {
return displayName;
}
@SuppressWarnings("unused")
public Iterator<String> getNameLookupKeys(String name) {
return null;
}
}
/**
* The classes to generate the Chinese style sort and search keys.
* <p>
* The sorting key is generated as each Chinese character' pinyin proceeding with
* space and character itself. If the character's pinyin unable to find, the character
* itself will be used.
* <p>
* The below additional name lookup keys will be generated.
* a. Chinese character's pinyin and pinyin's initial character.
* b. Latin word and the initial character for Latin word.
* The name lookup keys are generated to make sure the name can be found by from any
* initial character.
*/
private class ChineseContactUtils extends ContactLocaleUtilsBase {
@Override
public String getSortKey(String displayName) {
ArrayList<Token> tokens = HanziToPinyin.getInstance().get(displayName);
if (tokens != null && tokens.size() > 0) {
StringBuilder sb = new StringBuilder();
for (Token token : tokens) {
// Put Chinese character's pinyin, then proceed with the
// character itself.
if (Token.PINYIN == token.type) {
if (sb.length() > 0) {
sb.append(' ');
}
sb.append(token.target);
sb.append(' ');
sb.append(token.source);
} else {
if (sb.length() > 0) {
sb.append(' ');
}
sb.append(token.source);
}
}
return sb.toString();
}
return super.getSortKey(displayName);
}
@Override
public Iterator<String> getNameLookupKeys(String name) {
// TODO : Reduce the object allocation.
HashSet<String> keys = new HashSet<String>();
ArrayList<Token> tokens = HanziToPinyin.getInstance().get(name);
final int tokenCount = tokens.size();
final StringBuilder keyPinyin = new StringBuilder();
final StringBuilder keyInitial = new StringBuilder();
// There is no space among the Chinese Characters, the variant name
// lookup key wouldn't work for Chinese. The keyOrignal is used to
// build the lookup keys for itself.
final StringBuilder keyOrignal = new StringBuilder();
for (int i = tokenCount - 1; i >= 0; i--) {
final Token token = tokens.get(i);
if (Token.PINYIN == token.type) {
keyPinyin.insert(0, token.target);
keyInitial.insert(0, token.target.charAt(0));
} else if (Token.LATIN == token.type) {
// Avoid adding space at the end of String.
if (keyPinyin.length() > 0) {
keyPinyin.insert(0, ' ');
}
if (keyOrignal.length() > 0) {
keyOrignal.insert(0, ' ');
}
keyPinyin.insert(0, token.source);
keyInitial.insert(0, token.source.charAt(0));
}
keyOrignal.insert(0, token.source);
keys.add(keyOrignal.toString());
keys.add(keyPinyin.toString());
keys.add(keyInitial.toString());
}
return keys.iterator();
}
}
private static final String CHINESE_LANGUAGE = Locale.CHINESE.getLanguage().toLowerCase();
private static final String JAPANESE_LANGUAGE = Locale.JAPANESE.getLanguage().toLowerCase();
private static final String KOREAN_LANGUAGE = Locale.KOREAN.getLanguage().toLowerCase();
private static ContactLocaleUtils sSingleton;
private final SparseArray<ContactLocaleUtilsBase> mUtils =
new SparseArray<ContactLocaleUtilsBase>();
private final ContactLocaleUtilsBase mBase = new ContactLocaleUtilsBase();
private String mLanguage;
private ContactLocaleUtils() {
setLocale(null);
}
public void setLocale(Locale currentLocale) {
if (currentLocale == null) {
mLanguage = Locale.getDefault().getLanguage().toLowerCase();
} else {
mLanguage = currentLocale.getLanguage().toLowerCase();
}
}
//只返回汉字的拼音,例如:东方 DONG FANG
public String getSpell(String displayName, int nameStyle){
String full=getSortKey(displayName,nameStyle);
String result=SplitFullName(full,displayName);
return result;
}
/**
*
* @param full 东方 DONG 东 FANG 方
* @param name 东方
* @return
*/
private String SplitFullName(String full,String name){
char[] nameChar=name.toCharArray();
for(int i=0;i<nameChar.length;i++){
StringBuffer result = new StringBuffer();
Pattern p = Pattern.compile("[\u4e00-\u9fa5]");//判断是否为汉字
Matcher m = p.matcher(String.valueOf(nameChar[i]));
m = p.matcher(String.valueOf(nameChar[i]));
if (m.matches()) {
String[] temp=full.split(String.valueOf(nameChar[i]));
for(int j=0;j<temp.length;j++){
result.append(temp[j]);
}
}else{
result.append(full);
}
full=result.toString();
}
return full;
}
//返回拼音加文字,例如:东方 DONG 东 FANG 方
public String getSortKey(String displayName, int nameStyle) {
return getForSort(Integer.valueOf(nameStyle)).getSortKey(displayName);
}
public Iterator<String> getNameLookupKeys(String name, int nameStyle) {
return getForNameLookup(Integer.valueOf(nameStyle)).getNameLookupKeys(name);
}
/**
* Determine which utility should be used for generating NameLookupKey.
* <p>
* a. For Western style name, if the current language is Chinese, the
* ChineseContactUtils should be used.
* b. For Chinese and CJK style name if current language is neither Japanese or Korean,
* the ChineseContactUtils should be used.
*/
private ContactLocaleUtilsBase getForNameLookup(Integer nameStyle) {
int nameStyleInt = nameStyle.intValue();
Integer adjustedUtil = Integer.valueOf(getAdjustedStyle(nameStyleInt));
if (CHINESE_LANGUAGE.equals(mLanguage) && nameStyleInt == FullNameStyle.WESTERN) {
adjustedUtil = Integer.valueOf(FullNameStyle.CHINESE);
}
return get(adjustedUtil);
}
private synchronized ContactLocaleUtilsBase get(Integer nameStyle) {
ContactLocaleUtilsBase utils = mUtils.get(nameStyle);
if (utils == null) {
if (nameStyle.intValue() == FullNameStyle.CHINESE) {
utils = new ChineseContactUtils();
mUtils.put(nameStyle, utils);
}
}
return (utils == null) ? mBase : utils;
}
/**
* Determine the which utility should be used for generating sort key.
* <p>
* For Chinese and CJK style name if current language is neither Japanese or Korean,
* the ChineseContactUtils should be used.
*/
private ContactLocaleUtilsBase getForSort(Integer nameStyle) {
return get(Integer.valueOf(getAdjustedStyle(nameStyle.intValue())));
}
public static synchronized ContactLocaleUtils getIntance() {
if (sSingleton == null) {
sSingleton = new ContactLocaleUtils();
}
return sSingleton;
}
private int getAdjustedStyle(int nameStyle) {
if (nameStyle == FullNameStyle.CJK && !JAPANESE_LANGUAGE.equals(mLanguage) &&
!KOREAN_LANGUAGE.equals(mLanguage)) {
return FullNameStyle.CHINESE;
} else {
return nameStyle;
}
}
}
相关文章推荐
- Android自定义Style
- Android Studio配置OpenCV开发环境
- 【Android应用开发技术:用户界面】自定义View类设计
- android mediaplayer状态机
- Android 自定义ViewGroup 实现FlowLayout
- 2015年8月初iOS 8安装率碾压Android 5.0
- Android 软键盘盖住输入框的问题
- Android添加一个Native Service
- android:activity生命周期及几个主要函数应当做的事情
- Android - 视图Android应用(apk)签名
- Android PullToRefresh 使用详解
- Android实战经验之图像处理及特效处理的集锦(总结版)
- 让Android Studio 使用上vs的android模拟器
- Android+PHP+Mysql实现用户登录
- Android LayoutInflater深度解析 给你带来全新的认识
- Android NDK 开发
- smack for android 之登录openfire
- [转]Android加载图片堆栈溢出
- android studio右下角没有分支便签解决办法
- 即时通讯 - Android、iOS、J2EE服务端的非对称加密传输数据