找了好多自定义软键盘的博客,但是都没说明白汉字是怎么转换成codes的,记录一下我的两种方法。

一、在线转换

1、http://www.msxindl.com/tools/unicode16.asp输入汉字转换Unicode,例如“浙”转换成“\u6d59”。

2、https://tool.oschina.net/hexconvert/选16进制转10进制,输入上面的“6d59”,转换结果为“27993”。

<Key android:codes="27993" android:keyLabel="浙"
    android:horizontalGap="2%p" android:keyWidth="8%p" />

二、代码实现

public static String str2Unicode(String str){
    StringBuffer buffer = new StringBuffer();
    for (int i = 0; i < str.length(); i++) {
        buffer.append(Integer.toHexString(str.charAt(i)));
    }
    return buffer.toString();
}

public static String sixteen2Ten(String str){
    int ten = Integer.parseInt(str,16);
    return String.valueOf(ten);
}

调用

sixteen2Ten(str2Unicode("浙"));

输出

27993

本文地址:https://blog.csdn.net/qy1993qy/article/details/107313154