I've been developing a custom keyboard for Android, and I'm experiencing some weird behaviour when changing keyboards... On the first run (either after being build from Android Studio or distributed via Fabric), the keyboard will not close, when I use:
InputMethodManager imeManager = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
imeManager.switchToNextInputMethod(getToken(), false /* onlyCurrentIme */);
It will open the next input method behind my keyboard... But my keyboard will be forced upon on top of the other keyboard... It will even stay there, if you go to the home screen... But as soon as the device has been restarted, the keyboard changes without any issues... I've been reading up on this, and I've seen there was some issues with keyboards in previous versions of Android, are there still any?
Below you can see the relevant code:
public class CustomKeyboard extends InputMethodService implements KeyboardView.OnKeyboardActionListener {
private KeyboardView kv;
private Keyboard keyboard;
public final static int CodeRowFourGlobe = 55041;
@Override
public View onCreateInputView() {
kv = (KeyboardView)getLayoutInflater().inflate(R.layout.keyboard, null);
keyboard = new Keyboard(this, R.xml.custom_keyboard);
kv.setPreviewEnabled(false);
kv.setKeyboard(keyboard);
kv.setOnKeyboardActionListener(this);
return kv;
}
private IBinder getToken() {
final Dialog dialog = getWindow();
if (dialog == null) {
return null;
}
final Window window = dialog.getWindow();
if (window == null) {
return null;
}
return window.getAttributes().token;
}
@Override
public void onPress(int primaryCode) {
}
@Override
public void onRelease(int primaryCode) {
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
@Override
public void onKey(int primaryCode, int[] keyCodes) {
InputConnection ic = getCurrentInputConnection();
if (primaryCode == Keyboard.KEYCODE_DELETE) {
ic.deleteSurroundingText(1, 0);
} else if (primaryCode == CodeRowFourGlobe) {
InputMethodManager imeManager = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
imeManager.switchToNextInputMethod(getToken(), false /* onlyCurrentIme */);
} else {
/* not relevant */
}
}
@Override
public void onText(CharSequence text) {
}
@Override
public void swipeLeft() {
}
@Override
public void swipeRight() {
}
@Override
public void swipeDown() {
}
@Override
public void swipeUp() {
}
}
Do you have any idea if it's a problem with how I'm changing the keyboard or a known Android issue?
try this it works for me anytime
public static void hideKeyboard( Context context ) {
try {
InputMethodManager inputManager = ( InputMethodManager ) context.getSystemService( Context.INPUT_METHOD_SERVICE );
View view = ( (Activity) context ).getCurrentFocus();
if ( view != null ) {
inputManager.hideSoftInputFromWindow( view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS );
}
} catch ( Exception e ) {
e.printStackTrace();
}
}
Visibility of the ime is handled by the system itself.
I faced a similar issue a few days ago and fixed it by coding the IME from scratch again & doing all the time consuming stuff on background thread (like getTextBeforeCursor()/getTextAfterCursor, local database read/write, etc.).
If you switch from/to another IME more than once and the same thing happens, then it's the issue i'm talking about.
So maybe you are invoking something that takes time or gets the IME stuck. Just refer the documentation of the calls you are making to check if they take indeterminate time.
Hope this helps.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.