这是把中文转换成拼音字母的代码,我在android中只做了一个输入框和一个按钮,可当输入中文进行转换时就是出现乱码,下面为Button点击事件的代码
bt1 = (Button)findViewById(R.id.bt1);
        bt1.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
et = (EditText)findViewById(R.id.et);
String str = et.getText().toString();
System.out.println(ContoEn.getFullSpell(str).toString());
}
});
要怎么才能打印出拼音字母了。ContoEn.java为网上找的一个把中文转换成拼音字母的一个类,如果是个控制台的应用程序,中文可以转换成拼音字母,可到了android中就不行了

解决方案 »

  1.   

    ContoEn.java 支持的是ucs2编码的汉字么?
      

  2.   


    public class ContoEn {
    private static LinkedHashMap spellMap = null; static {
    if (spellMap == null) {
    spellMap = new LinkedHashMap(400);
    }
    initialize();
    System.out.println("Chinese   transfer   Spell   Done.");
    } private static void spellPut(String spell, int ascii) {
    spellMap.put(spell, new Integer(ascii));
    } private static void initialize() {
                    //这些东西太多了,贴不下了
    spellPut("a", -20319);
    spellPut("ai", -20317);
    spellPut("an", -20304);
    } /**
     * 获得单个汉字的Ascii.
     * 
     * @param cn
     *            char 汉字字符
     * @return int 错误返回 0,否则返回ascii
     */
    public static int getCnAscii(char cn) {
    byte[] bytes = (String.valueOf(cn)).getBytes();
    if (bytes == null || bytes.length > 2 || bytes.length <= 0) { // 错误
    return 0;
    }
    if (bytes.length == 1) { // 英文字符
    return bytes[0];
    }
    if (bytes.length == 2) { // 中文字符
    int hightByte = 256 + bytes[0];
    int lowByte = 256 + bytes[1]; int ascii = (256 * hightByte + lowByte) - 256 * 256; // System.out.println("ASCII=" + ascii); return ascii;
    } return 0; // 错误
    } /**
     * 根据ASCII码到SpellMap中查找对应的拼音
     * 
     * @param ascii
     *            int 字符对应的ASCII
     * @return String 拼音,首先判断ASCII是否>0&<160,如果是返回对应的字符,
     * 
     *         否则到SpellMap中查找,如果没有找到拼音,则返回null,如果找到则返回拼音.
     */
    public static String getSpellByAscii(int ascii) {
    if (ascii > 0 && ascii < 160) { // 单字符
    return String.valueOf((char) ascii);
    } if (ascii < -20319 || ascii > -10247) { // 不知道的字符
    return null;
    } Set keySet = spellMap.keySet();
    Iterator it = keySet.iterator(); String spell0 = null;
    ;
    String spell = null; int asciiRang0 = -20319;
    int asciiRang;
    while (it.hasNext()) { spell = (String) it.next();
    Object valObj = spellMap.get(spell);
    if (valObj instanceof Integer) {
    asciiRang = ((Integer) valObj).intValue(); if (ascii >= asciiRang0 && ascii < asciiRang) { // 区间找到
    return (spell0 == null) ? spell : spell0;
    } else {
    spell0 = spell;
    asciiRang0 = asciiRang;
    }
    }
    } return null; } /**
     * 返回字符串的全拼,是汉字转化为全拼,其它字符不进行转换
     * 
     * @param cnStr
     *            String 字符串
     * @return String 转换成全拼后的字符串
     */
    public static String getFullSpell(String cnStr) {
    if (null == cnStr || "".equals(cnStr.trim())) {
    return cnStr;
    } char[] chars = cnStr.toCharArray();
    StringBuffer retuBuf = new StringBuffer();
    for (int i = 0, Len = chars.length; i < Len; i++) {
    int ascii = getCnAscii(chars[i]);
    if (ascii == 0) { // 取ascii时出错
    retuBuf.append(chars[i]);
    } else {
    String spell = getSpellByAscii(ascii);
    if (spell == null) {
    retuBuf.append(chars[i]);
    } else {
    retuBuf.append(spell);
    } // end of if spell == null
    } // end of if ascii <= -20400
    } // end of for return retuBuf.toString();
    } public static String getFirstSpell(String cnStr) {
    return null;
    }
    }这就是那个ContoEn 类
      

  3.   

    ContoEn 调试中发现无法完成,汉字转换成Bytes数组时,生成的不是两个字节,而是三个字节,例如“啊”
    通过语句
      

  4.   

    ContoEn 调试中发现无法完成,汉字转换成Bytes数组时,生成的不是两个字节,而是三个字节,例如“啊”
    通过语句 
    public static int getCnAscii(char cn) {
            byte[] bytes = (String.valueOf(cn)).getBytes();
           .....
    }bytes数组的值是 -27,-107,-118,则 if (bytes == null || bytes.length > 2 || bytes.length <= 0) { // 错误
                return 0;
            }
            if (bytes.length == 1) { // 英文字符
                return bytes[0];
            }
            if (bytes.length == 2) { // 中文字符
    根本无法执行。
     
      

  5.   

    行啊  输入”国家“ 可以显示 “guojia”  我要的就是这种效果 不知道你们是不要这样的效果