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

时间:2016-01-30 07:17:06

标签: android android-softkeyboard

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

1 个答案:

答案 0 :(得分:0)

我终于找到了解决方案。我在上面的代码(问题中的代码)之后添加了以下代码以在语言之间切换:

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