Android软键盘如何添加切换语言功能?

How to add switch language functionality in the Android soft keyboard?

Android Soft Keyboard 应用程序目前有英文语言,我正在修改它以添加另一种语言。我几乎完成了新语言的布局和手动添加字母表,因为 Android 尚未包含该语言。新语言也有与 SHIFT 键一起出现的键。我正在努力修复两种语言之间的切换功能:英语和新添加的。

我可以通过硬编码来解决这个问题:使用按钮更改布局 (xml),然后反之亦然,但我知道这不是正确的方法,因为 switch功能。

我提供相关代码。如果需要提供更多代码,请发表评论。

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);
        }
    }
}

应用程序基于此sample

我终于想出了解决办法。我在上面的代码(问题中的代码)之后添加了以下代码以在语言之间切换:

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"/>