if (displayNameStyle == FullNameStyle.CHINESE ||
displayNameStyle == FullNameStyle.CJK) {
sortKeyPrimary = sortKeyAlternative =
ContactLocaleUtils.getIntance().getSortKey(
displayNamePrimary, displayNameStyle);
}
ContactLocaleUtils.getIntance();
取得实例,然后调用:
1. public String getSortKey(String displayName, int nameStyle) {
return getForSort(Integer.valueOf(nameStyle)).getSortKey(displayName);
}
2. @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);
}
package com.spare.pinyin;
import java.util.ArrayList;
import com.spare.pinyin.HanziToPinyin.Token;
public class PinYin {
//汉字返回拼音,字母原样返回,都转换为小写(默认取得的拼音全大写)
public static String getPinYin(String input) {
ArrayList<Token> tokens = HanziToPinyin.getInstance().get(input);
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().toLowerCase();
}
}
以下附上frameworks/base /core/java/com/android/internal/util/HanziToPinyin.java源码
更新:4.X上代码位置已变更packages/providers/ContactsProvider/src/com/android/providers/contacts/HanziToPinyin.java,内容也有所改变,已基于android4.2源码提取并上传。在2.X和4.X上均测试通过。
下载地址:http://download.csdn.net/detail/spare_h/4881592