如何在Android软键盘中添加切换语言功能?
Android软键盘应用程序目前使用英语,我正在修改它以添加另一种语言。我几乎完成了新语言的布局和手动添加字母表,因为该语言尚未包含在Android中。新语言还具有与SHIFT键一起显示的键。我正在努力修复两种语言之间的切换功能:英语和新添加的 我可以通过硬编码来解决这一问题:使用按钮更改布局(xml),然后再使用按钮更改布局(xml),但我知道这不是正确的方法,因为存在如何在Android软键盘中添加切换语言功能?,android,android-softkeyboard,Android,Android Softkeyboard,Android软键盘应用程序目前使用英语,我正在修改它以添加另一种语言。我几乎完成了新语言的布局和手动添加字母表,因为该语言尚未包含在Android中。新语言还具有与SHIFT键一起显示的键。我正在努力修复两种语言之间的切换功能:英语和新添加的 我可以通过硬编码来解决这一问题:使用按钮更改布局(xml),然后再使用按钮更改布局(xml),但我知道这不是正确的方法,因为存在开关功能 我正在提供相关代码。如果需要提供更多代码,请发表评论 public void onKey(int primaryCo
开关
功能
我正在提供相关代码。如果需要提供更多代码,请发表评论
public void onKey(int primaryCode, int[] keyCodes) {
if (isWordSeparator(primaryCode)) {
// Handle separator
if (mComposing.length() > 0) {
commitTyped(getCurrentInputConnection());
}
sendKey(primaryCode);
updateShiftKeyState(getCurrentInputEditorInfo());
} else if (primaryCode == Keyboard.KEYCODE_DELETE) {
handleBackspace();
} else if (primaryCode == Keyboard.KEYCODE_SHIFT) {
handleShift();
} else if (primaryCode == Keyboard.KEYCODE_CANCEL) {
handleClose();
return;
} else if (primaryCode == LatinKeyboardView.KEYCODE_LANGUAGE_SWITCH) {
handleLanguageSwitch();
return;
} else if (primaryCode == LatinKeyboardView.KEYCODE_OPTIONS) {
// Show a menu or something'
} else if (primaryCode == Keyboard.KEYCODE_MODE_CHANGE
&& mInputView != null) {
Keyboard current = mInputView.getKeyboard();
if (current == mSymbolsKeyboard || current == mSymbolsShiftedKeyboard) {
setLatinKeyboard(mQwertyKeyboard);
} else {
setLatinKeyboard(mSymbolsKeyboard);
mSymbolsKeyboard.setShifted(false);
}
}
}
应用程序正是基于此。我终于找到了解决方案。我在上述代码(问题中的代码)之后添加了以下代码以在语言之间切换:
else if (primaryCode == 10000) {
Keyboard current = mInputView.getKeyboard();
current = mQwertyNewKeyboard;
mInputView.setKeyboard(current);
//Switch to qwerty (English Main)
}else if (primaryCode == 10001) {
Keyboard current = mInputView.getKeyboard();
current = mQwertyKeyboard;
mInputView.setKeyboard(current);
//Switch to qwertyNewShift
}else if (primaryCode == 10002) {
Keyboard current = mInputView.getKeyboard();
current = mQwertyNewKeyboardShift;
mInputView.setKeyboard(current);
}
在每种语言的布局(xml)文件中,我创建了一个开关按钮,并相应地设置了primaryCode
<Key android:codes="10001" android:keyIcon="@drawable/sym_keyboard_language_switch" android:keyWidth="10%p"/>
我终于想出了解决办法。我在上述代码(问题中的代码)之后添加了以下代码以在语言之间切换:
else if (primaryCode == 10000) {
Keyboard current = mInputView.getKeyboard();
current = mQwertyNewKeyboard;
mInputView.setKeyboard(current);
//Switch to qwerty (English Main)
}else if (primaryCode == 10001) {
Keyboard current = mInputView.getKeyboard();
current = mQwertyKeyboard;
mInputView.setKeyboard(current);
//Switch to qwertyNewShift
}else if (primaryCode == 10002) {
Keyboard current = mInputView.getKeyboard();
current = mQwertyNewKeyboardShift;
mInputView.setKeyboard(current);
}
在每种语言的布局(xml)文件中,我创建了一个开关按钮,并相应地设置了primaryCode
<Key android:codes="10001" android:keyIcon="@drawable/sym_keyboard_language_switch" android:keyWidth="10%p"/>