Android InputMethodService - 如何获取unicode字符?

时间:2011-08-31 12:34:17

标签: android

我正在尝试制作键盘......

int code = 29; // Key code constant: 'A' key. Constant Value: 29 (0x0000001d)
KeyEvent k = new KeyEvent(KeyEvent.ACTION_UP, code);

InputConnection ic = getCurrentInputConnection();
ic.sendKeyEvent(k);

此代码很好地将字母“A”发送到应用程序,因为它与代码“29”相关联,从此处http://developer.android.com/reference/android/view/KeyEvent.html

但是,如果我想使用unicode字符,我应该怎么做,例如在这里 http://en.wikipedia.org/wiki/List_of_Unicode_characters

2 个答案:

答案 0 :(得分:0)

如果您正在构建键盘应用,并且在xml中使用键盘布局,则可以查看用户Laurent'在另一个thread中提供的建议:

<Key android:codes="946" android:keyLabel="\u03B2"/> 
<Key android:codes="946" android:keyLabel="&946;"/> 
<Key android:codes="946" android:keyLabel="β"/>  
<!-- Warning, you should use UTF-8 encoding for your project files if you use the third solution -->
<!-- all produce the same key with greek β character (unicode \u03B2, decimal 946) -->

答案 1 :(得分:-1)

请查看softkeyboard示例代码。

如果您正在开发新键盘(例如用于任何其他语言),请将android:code属性值更改为您希望键盘输出的unicode值。 这很好用,我认为你可以在同一个softkeyboard项目中测试它。