【问题标题】:how to change keyboard layout in android by pressing Shift key如何通过按 Shift 键更改 android 中的键盘布局
【发布时间】:2014-02-13 08:48:33
【问题描述】:

我目前正在自己​​实现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的异常:

我发现这可能发生在我们处理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);
    }
}

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

【问题讨论】:

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


    【解决方案1】:

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

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

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

    【讨论】:

      猜你喜欢
      • 2017-06-11
      • 2016-07-15
      • 2021-10-03
      • 2020-08-02
      • 1970-01-01
      • 1970-01-01
      • 2013-03-18
      • 1970-01-01
      • 2023-04-10
      相关资源
      最近更新 更多