pinyin4j的主页:http://pinyin4j.sourceforge.net/
pinyin4j是一个支持将中文转换到拼音的Java开源类库
1.支持简体中文和繁体中文字符
2.支持转换到汉语拼音,通用拼音, 威妥玛拼音(威玛拼法), 注音符号第二式, 耶鲁拼法和国语罗马字
3.支持多音字,即可以获取一个中文字符的多种发音
4.支持多种字符串输出格式,比如支持Unicode格式的字符ü和声调符号(阴平 “ˉ”,阳平"ˊ",上声"ˇ",去声"ˋ")的输出
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile files('libs/pinyin4j-2.5.0.jar')
}
import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
public class PinyinUtils {
/**
* 获取拼音
*
* @param inputString
* @return
*/
public static String getPingYin(String inputString) {
HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
format.setCaseType(HanyuPinyinCaseType.LOWERCASE);
format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
format.setVCharType(HanyuPinyinVCharType.WITH_V);
char[] input = inputString.trim().toCharArray();
String output = "";
try {
for (char curChar : input) {
if (Character.toString(curChar).matches("[\\u4E00-\\u9FA5]+")) {
String[] temp = PinyinHelper.toHanyuPinyinStringArray(curChar, format);
output += temp[0];
} else
output += Character.toString(curChar);
}
} catch (BadHanyuPinyinOutputFormatCombination e) {
e.printStackTrace();
}
return output;
}
/**
* 获取第一个字的拼音首字母
* @param chinese
* @return
*/
public static String getFirstSpell(String chinese) {
StringBuffer pinYinBF = new StringBuffer();
char[] arr = chinese.toCharArray();
HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
for (char curChar : arr) {
if (curChar > 128) {
try {
String[] temp = PinyinHelper.toHanyuPinyinStringArray(curChar, defaultFormat);
if (temp != null) {
pinYinBF.append(temp[0].charAt(0));
}
} catch (BadHanyuPinyinOutputFormatCombination e) {
e.printStackTrace();
}
} else {
pinYinBF.append(curChar);
}
}
return pinYinBF.toString().replaceAll("\\W", "").trim();
}
public static void Vibrate(final Activity activity, long milliseconds) {
Vibrator vib = (Vibrator) activity
.getSystemService(Service.VIBRATOR_SERVICE);
vib.vibrate(milliseconds);
}
}
public List(String name){
if(name == null){
name = "无名";
}
pinyin = PinyinUtils.getPingYin(name); // 根据姓名获取拼音
letters = pinyin.substring(0, 1).toUpperCase(); // 获取拼音首字母并转成大写
if (!letters.matches("[A-Z]")) { // 如果不在A-Z中则默认为“#”
letters = "#";
}
}
String name = sortModel.getName();
if (name.indexOf(filterStr.toString()) != -1 ||
PinyinUtils.getFirstSpell(name).startsWith(filterStr.toString())
|| PinyinUtils.getFirstSpell(name).toLowerCase().startsWith(filterStr.toString())
|| PinyinUtils.getFirstSpell(name).toUpperCase().startsWith(filterStr.toString())
) {
filterDateList.add(sortModel);
}
}
pinyin4j 可以简单使用
但是06年以来已经停更
更丰富的功能需要继续发掘