问题
The Android Soft Keyboard application currently has English language, and I am modifying it to add another language into it. I am almost done with layout of new language and adding alphabets manually as the language is not included in the Android yet. The new language also has keys which appears with SHIFT key. I am struggling to fix the switching functionality between two languages: English and new added.
I can fix this by hard coding: to change the layout (xml) with button and then do again vice versa, but I know this is not the right approach as there is switch
functionality.
I am providing the related code. Please comment if need more code to provide.
public void onKey(int primaryCode, int[] keyCodes) {
if (isWordSeparator(primaryCode)) {
// Handle separator
if (mComposing.length() > 0) {
commitTyped(getCurrentInputConnection());
}
sendKey(primaryCode);
updateShiftKeyState(getCurrentInputEditorInfo());
} else if (primaryCode == Keyboard.KEYCODE_DELETE) {
handleBackspace();
} else if (primaryCode == Keyboard.KEYCODE_SHIFT) {
handleShift();
} else if (primaryCode == Keyboard.KEYCODE_CANCEL) {
handleClose();
return;
} else if (primaryCode == LatinKeyboardView.KEYCODE_LANGUAGE_SWITCH) {
handleLanguageSwitch();
return;
} else if (primaryCode == LatinKeyboardView.KEYCODE_OPTIONS) {
// Show a menu or something'
} else if (primaryCode == Keyboard.KEYCODE_MODE_CHANGE
&& mInputView != null) {
Keyboard current = mInputView.getKeyboard();
if (current == mSymbolsKeyboard || current == mSymbolsShiftedKeyboard) {
setLatinKeyboard(mQwertyKeyboard);
} else {
setLatinKeyboard(mSymbolsKeyboard);
mSymbolsKeyboard.setShifted(false);
}
}
}
The application is based on this sample.
回答1:
I finally figured out the solution. I added the following code right after the above code (code in the question) to switch between languages:
else if (primaryCode == 10000) {
Keyboard current = mInputView.getKeyboard();
current = mQwertyNewKeyboard;
mInputView.setKeyboard(current);
//Switch to qwerty (English Main)
}else if (primaryCode == 10001) {
Keyboard current = mInputView.getKeyboard();
current = mQwertyKeyboard;
mInputView.setKeyboard(current);
//Switch to qwertyNewShift
}else if (primaryCode == 10002) {
Keyboard current = mInputView.getKeyboard();
current = mQwertyNewKeyboardShift;
mInputView.setKeyboard(current);
}
And in the layout (xml) file of each language, I created a switch button and set the primaryCode
accordingly.
<Key android:codes="10001" android:keyIcon="@drawable/sym_keyboard_language_switch" android:keyWidth="10%p"/>
来源:https://stackoverflow.com/questions/35099182/how-to-add-switch-language-functionality-in-the-android-soft-keyboard