这是把中文转换成拼音字母的代码,我在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中就不行了
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中就不行了
解决方案 »
- 关于谷歌地图的请教 调用系统谷歌
- android 中点击一个Button,怎样在TextView中显示点击次数?
- 请问大家多线程操作sqlite数据库时如何处理的呢?
- opengl画正方体问题
- 在自定义thread中handler.sendMessage()执行两次?
- 怎么在ndk环境下利用opengl es显示文字或者字符?
- repo下了源码,发现没有kernel文件夹?
- Android 网络API 处理过后的网页源代码以网页显示出来!!!
- ViewPager 刷新每个 View的问题。
- 请教彩信发送问题
- android多任务下载,并实时显示在多个进度条问题
- 用Flex开发安卓的天气预报碰到了解析JSON的问题!求大牛指导
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 类
通过语句
通过语句
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) { // 中文字符
根本无法执行。