为什么Android软键盘在点击数字或符号时会省略(删除)字母?

时间:2016-02-19 15:42:29

标签: java android android-softkeyboard

我有Android软键盘,可在两种语言之间切换。每种语言都有自己的布局(xml)。英语正在运作,但阿拉伯语有bug。当我输入阿拉伯字母后,在按下数字或符号后立即删除字母。

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,在输入英语和阿拉伯语时效果很好,但是数字和符号有问题。

1 个答案:

答案 0 :(得分: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);
    }

}