private HanyuPinyinOutputFormat mOutputFormat = null;
mOutputFormat = new HanyuPinyinOutputFormat();
mOutputFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);//设置大小写
mOutputFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);//设置音调格式
public String parserWordsToPinyin(String words) {
if (words == null || words.trim().length() == 0) {
return "###";
}
String py = "", temp = "";
String[] t = null;
for (int i = 0; i < words.length(); i++) {
char c = words.charAt(i);
if (((int) c) <= 128) { //ascii 码小于128的都是字符数字之类
py += c;
} else {
try {
t = PinyinHelper.toHanyuPinyinStringArray(c, mOutputFormat);
if (t == null) {
py += c;
} else {
temp = t[0];
// py += temp + (i == words.length() -1 ? "" : "_");
py += temp;
}
} catch (BadHanyuPinyinOutputFormatCombination e) {
e.printStackTrace();
}
}
}
return py.trim();
}
pinyin4j-2.5.0.jar 下载路径:
https://download.csdn.net/download/myjie0527/10648124
参考自:https://blog.csdn.net/peekabo_o/article/details/50888322