汉语转为拼音

  首先要先导入架包:com.belerweb:pinyin4j:2.5.1

android中汉字转为拼音

  工具类:

  注释的方法,是可以将单个汉字转为拼音的,为注释的可以转化词语,

public class HanZiToPinYin {
    //    public static String toPinYin(char hanzi){
//        HanyuPinyinOutputFormat hanyuPinyin = new HanyuPinyinOutputFormat();
//        hanyuPinyin.setCaseType(HanyuPinyinCaseType.LOWERCASE);
//        hanyuPinyin.setToneType(HanyuPinyinToneType.WITH_TONE_MARK);
//        hanyuPinyin.setVCharType(HanyuPinyinVCharType.WITH_U_UNICODE);
//        String[] pinyinArray=null;
//        try {
//            //是否在汉字范围内
//            if(hanzi>=0x4e00 && hanzi<=0x9fa5){
//                pinyinArray = PinyinHelper.toHanyuPinyinStringArray(hanzi, hanyuPinyin);
//            }
//        } catch (BadHanyuPinyinOutputFormatCombination e) {
//            e.printStackTrace();
//        }
//        //将获取到的拼音返回
//        return pinyinArray[0];
//    }
    public static String getPinYinAllChar(String zn_str, int caseType) {
        char[] strChar = zn_str.toCharArray();
        HanyuPinyinOutputFormat hanYuPinOutputFormat = new HanyuPinyinOutputFormat();
        // 输出设置,大小写,音标方式等
        if (1 == caseType) {
            hanYuPinOutputFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
        } else {
            hanYuPinOutputFormat.setCaseType(HanyuPinyinCaseType.UPPERCASE);
        }
        hanYuPinOutputFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        hanYuPinOutputFormat.setVCharType(HanyuPinyinVCharType.WITH_V);

        StringBuffer pyStringBuffer = new StringBuffer();
        String[] strString = new String[strChar.length];
        try {
            for (int i = 0; i < strChar.length; i++) {
                if (Character.toString(strChar[i]).matches("[\\u4E00-\\u9FA5]+")) {//如果是汉字字符                   
                    //将汉字的几种全拼都存到strString数组中
 strString = PinyinHelper.toHanyuPinyinStringArray(strChar[i], hanYuPinOutputFormat); pyStringBuffer.append(strString[0]);//取出该汉字全拼的第一种读音并连接到字符串pyStringBuffer后 } else {//如果不是汉字字符,直接取出字符并连接到字符串pyStringBuffer后 pyStringBuffer.append(Character.toString(strChar[i])); } } } catch (BadHanyuPinyinOutputFormatCombination e) { e.printStackTrace(); } return pyStringBuffer.toString(); } MainActivity中

注释的代码是调用单个汉字转拼音的方法,为注释的是调用词语转拼音的方法

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TextView showText= (TextView) findViewById(R.id.showText);
//        String hanziString="人";
//        String pinyinString= HanZiToPinYin.toPinYin(hanziString.charAt(1));
//        showText.setText("汉字:"+hanziString+"\n"+"拼音:"+pinyinString);
        String s = HanZiToPinYin.getPinYinAllChar("大家好", 1);
        showText.setText(s);
    }
}
运行结果:

词语转拼音:

android中汉字转为拼音

单个字转拼音:

android中汉字转为拼音

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-08-23
  • 2022-12-23
  • 2022-12-23
  • 2021-11-24
  • 2021-11-01
猜你喜欢
  • 2021-11-26
  • 2022-12-23
  • 2021-12-09
  • 2021-12-01
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案