gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 10:49:10 24 4
gpt4 key购买 nike

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

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,在输入英语和阿拉伯语时可以正常工作,但在输入数字和符号时会出现问题。

最佳答案

我通过将 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);
}

}

关于java - 为什么点击数字或符号时 Android 软键盘会省略(删除)字母?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35509175/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com