美文网首页
Android 中文汉字转拼音

Android 中文汉字转拼音

作者: 懵懵懂懂_YOYO | 来源:发表于2022-09-21 16:08 被阅读0次

    1.引用:
    implementation 'com.belerweb:pinyin4j:2.5.1'

    2.代码如下:

       /**
         * 汉字转为拼音
         *
         * @param chinese
         * @return
         */
        public static String toPinyin(String chinese) {
            String pinyinStr = "";
            char[] newChar = chinese.toCharArray();
            HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
            defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
            defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
            for (int i = 0; i < newChar.length; i++) {
                if (newChar[i] > 128) {
                    try {
                        pinyinStr += PinyinHelper.toHanyuPinyinStringArray(newChar[i], defaultFormat)[0];
                    } catch (BadHanyuPinyinOutputFormatCombination e) {
                        e.printStackTrace();
                    }
                } else {
                    pinyinStr += newChar[i];
                }
            }
            return pinyinStr;
        }
    

    相关文章

      网友评论

          本文标题:Android 中文汉字转拼音

          本文链接:https://www.haomeiwen.com/subject/zowwortx.html