我必须在我的应用程序中实现一个简单的自定义软键盘,我也想在键盘上显示重音字符。
这些是: í,é,á,ű,ú,ő,ó,ü,ö
我的问题是如何在键盘xml中映射这些?这些的关键代码是什么?我无法在官方KeyEvent文件中找到它们。
我当前的键盘定义xml如下所示:
<Keyboard xmlns:android="http://schemas.android.com/apk/res/android"
android:keyWidth="10%p"
android:horizontalGap="0px"
android:verticalGap="0px"
>
<Row android:keyHeight="16%">
<Key android:codes="45" android:keyLabel="q" android:keyEdgeFlags="left"/>
<Key android:codes="51" android:keyLabel="w"/>
<Key android:codes="33" android:keyLabel="e"/>
<Key android:codes="46" android:keyLabel="r"/>
<Key android:codes="48" android:keyLabel="t"/>
<Key android:codes="54" android:keyLabel="z"/>
<Key android:codes="49" android:keyLabel="u"/>
<Key android:codes="37" android:keyLabel="i"/>
<Key android:codes="43" android:keyLabel="o"/>
<Key android:codes="44" android:keyLabel="p" android:keyEdgeFlags="right"/>
</Row>
<Row android:keyHeight="16%">
<Key android:codes="?" android:keyLabel="ö" android:keyEdgeFlags="left"/>
<Key android:codes="?" android:keyLabel="ő"/>
<Key android:codes="?" android:keyLabel="ü"/>
<Key android:codes="?" android:keyLabel="ű"/>
<Key android:codes="?" android:keyLabel="ó"/>
<Key android:codes="?" android:keyLabel="é"/>
<Key android:codes="?" android:keyLabel="á"/>
<Key android:codes="?" android:keyLabel="í" android:keyEdgeFlags="right"/>
</Row>
</Keyboard>
答案 0 :(得分:4)
最后我用一招来解决了这个问题。
将自定义键码从900到908插入到包含我的重音字符的行中。
<Row android:keyHeight="16%">
<Key android:codes="900" android:keyLabel="ö" android:keyEdgeFlags="left"/>
<Key android:codes="901" android:keyLabel="ő"/>
<Key android:codes="902" android:keyLabel="ü"/>
<Key android:codes="903" android:keyLabel="ű"/>
<Key android:codes="904" android:keyLabel="ó"/>
<Key android:codes="905" android:keyLabel="ú"/>
<Key android:codes="906" android:keyLabel="á"/>
<Key android:codes="907" android:keyLabel="é"/>
<Key android:codes="908" android:keyLabel="í" android:keyEdgeFlags="right"/>
</Row>
我设置了我的KeyboardView的Action Listener,我将目标EditText作为对象传递给我的OnKeyboardActionListener。
这样在我的OnKeyboardActionListener的onKey方法中,我可以检查primaryCode是否大于900或等于它,那么我只需设置EditText的文本并将我的重音字符插入光标位置:
@Override
public void onKey(int primaryCode, int[] keyCodes) {
if(primaryCode >= 900) {
String character = null;
switch(keyCode) {
case 900:
character = "ö";
break;
case 901:
character = "ő";
break;
case 902:
character = "ü";
break;
case 903:
character = "ű";
break;
case 904:
character = "ó";
break;
case 905:
character = "ú";
break;
case 906:
character = "á";
break;
case 907:
character = "é";
break;
case 908:
character = "í";
break;
default:
return;
}
int start = mTargetView.getSelectionStart();
int end = mTargetView.getSelectionEnd();
mTargetView.getText().replace(Math.min(start, end), Math.max(start, end), character, 0, character.length());
} else {
long eventTime = System.currentTimeMillis();
KeyEvent event = new KeyEvent(eventTime, eventTime,
KeyEvent.ACTION_DOWN, primaryCode, 0, 0, 0, 0,
KeyEvent.FLAG_SOFT_KEYBOARD | KeyEvent.FLAG_KEEP_TOUCH_MODE);
mTargetActivity.dispatchKeyEvent(event);
}
}
其中mTargetView是我的EditText。有了这个技巧,我可以将重音的unicode字符和基本上所有内容插入到带有标准键盘xml映射的EditText中。
答案 1 :(得分:0)
稍微简短的解决方案就是删除codes
,因为在这种情况下会发送标签文本。
<Row>
<Key android:keyLabel="ö" android:keyEdgeFlags="left"/>
<Key android:keyLabel="ő"/>
<Key android:keyLabel="ü"/>
<Key android:keyLabel="ű"/>
<Key android:keyLabel="ó"/>
<Key android:keyLabel="ú"/>
<Key android:keyLabel="á"/>
<Key android:keyLabel="é"/>
<Key android:keyLabel="í" android:keyEdgeFlags="right"/>
</Row>