如何通过按Shift键更改android中的键盘布局

时间:2014-02-13 08:48:33

标签: android android-softkeyboard android-input-method

我目前正在开发Input Method Editor (IME)的自己实现,或者可以在Android中调用Softkeyboard,我已阅读creating an input method并已下载the SoftKeyboard sample code provided as part of the SDK。我有sample Softkeyboard的以下代码:

private void handleCharacter(int primaryCode, int[] keyCodes) {
    if (isInputViewShown()) {
        if (mInputView.isShifted()) {
            primaryCode = Character.toUpperCase(primaryCode);
        }
    }
    if (isAlphabet(primaryCode) && mPredictionOn) {
        /**
         * Swapping here with my desired unicode character
         * */
        if (primaryCode >= 97 && primaryCode <= 122 ) {
            mComposing.append(Swap.swapLetters(primaryCode));
        }else{
            mComposing.append((char) primaryCode);
        }
        getCurrentInputConnection().setComposingText(mComposing, 1);
        updateShiftKeyState(getCurrentInputEditorInfo());
        updateCandidates();
    } else {
        getCurrentInputConnection().commitText(
                String.valueOf((char) primaryCode), 1);
    }
}

上面给出的代码工作正常,但是当我点击密钥时:

<Key android:codes="-1" 
            android:keyWidth="15%p" android:isModifier="true"
            android:isSticky="true" android:keyEdgeFlags="left"/>

就像Shift键,这会将键转换为大写我不知道如何显示我的下一个键,这个键被点击/按下,以下是Logcat异常:

Logcat of exception

我跟踪过这可能发生在我们处理Shift密钥的地方:

private void handleShift() {
    if (mInputView == null) {
        return;
    }

    Keyboard currentKeyboard = mInputView.getKeyboard();
    if (mQwertyKeyboard == currentKeyboard) {
        // Alphabet keyboard
        checkToggleCapsLock();          
        mInputView.setKeyboard(mSindhi);            
    } else if (currentKeyboard == mSymbolsKeyboard) {
        mSymbolsKeyboard.setShifted(true);
        mInputView.setKeyboard(mSymbolsShiftedKeyboard);
        mSymbolsShiftedKeyboard.setShifted(true);
    } else if (currentKeyboard == mSymbolsShiftedKeyboard) {
        mSymbolsShiftedKeyboard.setShifted(false);
        mInputView.setKeyboard(mSymbolsKeyboard);
        mSymbolsKeyboard.setShifted(false);
    }
}

现在我不知道如何摆脱上面提到的异常,并让我的代码工作。请解决这个!

1 个答案:

答案 0 :(得分:4)

您的问题可能还有其他一些解决方案,但我做了一个技巧并提出了以下建议:

第1步您必须删除代码中updateShiftKeyState(getCurrentInputEditorInfo());方法的所有调用,例如:

private void handleCharacter(int primaryCode, int[] keyCodes) {
if (isInputViewShown()) {
    if (mInputView.isShifted()) {
        primaryCode = Character.toUpperCase(primaryCode);
    }
}
if (isAlphabet(primaryCode) && mPredictionOn) {
    /**
     * Swapping here with my desired unicode character
     * */
    if (primaryCode >= 97 && primaryCode <= 122 ) {
        mComposing.append(Swap.swapLetters(primaryCode));
    }else{
        mComposing.append((char) primaryCode);
    }
    getCurrentInputConnection().setComposingText(mComposing, 1);
    //updateShiftKeyState(getCurrentInputEditorInfo()); // you can delete this method from your class and clean-up all the occurances of that 
    updateCandidates();
} else {
    getCurrentInputConnection().commitText(
            String.valueOf((char) primaryCode), 1);
}}

第2步将您的handleShift方法更改为:

private void handleShift() {
    if (mInputView == null) {
        return;
    }

    Keyboard currentKeyboard = mInputView.getKeyboard();
    if (mQwertyKeyboard == currentKeyboard) {           
        mInputView.setKeyboard(mSindhi);

    } else if (currentKeyboard == mSymbolsKeyboard) {
        mInputView.setKeyboard(mSymbolsShiftedKeyboard);
    } else if (currentKeyboard == mSymbolsShiftedKeyboard) {
        mInputView.setKeyboard(mSymbolsKeyboard);
    }
}

就是这样,希望这对你有用......