2016-02-19 37 views
2

我有兩種語言切換的Android軟鍵盤。每種語言都有自己的佈局(xml)。英語正在工作,但阿拉伯語有錯誤。當我輸入阿拉伯文字母並按下數字或符號後立即刪除字母。爲什麼Android軟鍵盤在數字或符號點擊時會省略(刪除)字母?

private void handleCharacter(int primaryCode, int[] keyCodes) { 
    if (isInputViewShown()) { 
     if (mInputView.isShifted()) { 
      primaryCode = Character.toUpperCase(primaryCode); 
     } 
    } 
    if (isAlphabet(primaryCode) && mPredictionOn) { 
     mComposing.append((char) primaryCode); 
     getCurrentInputConnection().setComposingText(mComposing, 1); 
     updateShiftKeyState(getCurrentInputEditorInfo()); 
     updateCandidates(); 

    } 

    getCurrentInputConnection().commitText(
       String.valueOf((char) primaryCode), 1); 
} 

@Override public void onFinishInput() { 
    super.onFinishInput(); 

    // Clear current composing text and candidates. 
    mComposing.setLength(0); 
    updateCandidates(); 

    // We only hide the candidates window when finishing input on 
    // a particular editor, to avoid popping the underlying application 
    // up and down if the user is entering text into the bottom of 
    // its window. 
    setCandidatesViewShown(false); 

    mCurKeyboard = mQwertyKeyboard; 
    if (mInputView != null) { 
     mInputView.closing(); 
    } 
} 
@Override public void onUpdateSelection(int oldSelStart, int oldSelEnd, 
     int newSelStart, int newSelEnd, 
     int candidatesStart, int candidatesEnd) { 
    super.onUpdateSelection(oldSelStart, oldSelEnd, newSelStart, newSelEnd, 
      candidatesStart, candidatesEnd); 

    // If the current selection in the text view changes, we should 
    // clear whatever candidate text we have. 
    if (mComposing.length() > 0 && (newSelStart != candidatesEnd 
      || newSelEnd != candidatesEnd)) { 
     mComposing.setLength(0); 
     updateCandidates(); 
     InputConnection ic = getCurrentInputConnection(); 
     if (ic != null) { 
      ic.finishComposingText(); 
     } 
    } 
} 
private void commitTyped(InputConnection inputConnection) { 
    if (mComposing.length() > 0) { 
     inputConnection.commitText(mComposing, mComposing.length()); 
     mComposing.setLength(0); 
     updateCandidates(); 
    } 
} 

/** 
* Helper to update the shift state of our keyboard based on the initial 
* editor state. 
*/ 
private void updateShiftKeyState(EditorInfo attr) { 
    if (attr != null 
      && mInputView != null && mQwertyKeyboard == mInputView.getKeyboard()) { 
     int caps = 0; 
     EditorInfo ei = getCurrentInputEditorInfo(); 
     if (ei != null && ei.inputType != InputType.TYPE_NULL) { 
      caps = getCurrentInputConnection().getCursorCapsMode(attr.inputType); 
     } 
     mInputView.setShifted(mCapsLock || caps != 0); 
    } 
} 

/** 
* Helper to determine if a given character code is alphabetic. 
*/ 
private boolean isAlphabet(int code) { 
    if (Character.isLetter(code)) { 
     return true; 
    } else { 
     return false; 
    } 
} 

讓我知道是否需要更多代碼。我也檢查了關於這個問題的有關stackoverflow的問題,它只有幫助修復英語。

阿拉伯語是RTL,並且在輸入英文和阿拉伯文時工作正常,但數字和符號有問題。

+0

你找到解決辦法? –

+0

@Malwinder我發佈瞭解決方案,如果你正在尋找它。 –

+0

可能與我的問題有關,刪除發生在LTR語言中:https://stackoverflow.com/questions/46363286/new-android-keyboard-behaving-unexpectedly-in-certain-apps我將研究你選擇的答案並看看我能否應用它 –

回答

1

我解決了用下面的替代方法handlerCharacter問題:

private void handleCharacter(int primaryCode, int[] keyCodes) { 
    if (isInputViewShown()) { 
     if (mInputView.isShifted()) { 
      primaryCode = Character.toUpperCase(primaryCode); 
     } 
    } 
    if (isAlphabet(primaryCode) && mPredictionOn) { 
     mComposing.append((char) primaryCode); 
     getCurrentInputConnection().setComposingText(mComposing, 1); 
     updateShiftKeyState(getCurrentInputEditorInfo()); 
     updateCandidates(); 
    } 
    else { 
     mComposing.append((char) primaryCode); 
     getCurrentInputConnection().setComposingText(mComposing, 1); 
    } 

} 
相關問題