2014-02-13 113 views
1

我目前正在自己​​的實現Input Method Editor (IME)或可以在Android中調用Softkeyboard我已閱讀creating an input method我已經下載the SoftKeyboard sample code provided as part of the SDK。我有以下代碼sample Softkeyboard如何通過按Shift鍵更改android的鍵盤佈局

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

現在我沒有任何想法如何擺脫上述異常,並得到我的代碼工作。請解決這個問題!

回答

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方法給他:

就是這樣,希望這可能適合你...