Android: switch to a different IME programmatically

前端 未结 3 487
隐瞒了意图╮
隐瞒了意图╮ 2020-12-30 06:17

http://developer.android.com/guide/topics/text/creating-input-method.html#GeneralDesign reads:

Because multiple IMEs may be installed on the device, provide a wa

相关标签:
3条回答
  • 2020-12-30 06:23

    Switching to the previous input method from the current input method is:

    //final String LATIN = "com.android.inputmethod.latin/.LatinIME";
    // 'this' is an InputMethodService
    try {
        InputMethodManager imm = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
        final IBinder token = this.getWindow().getWindow().getAttributes().token;
        //imm.setInputMethod(token, LATIN);
        imm.switchToLastInputMethod(token);
    } catch (Throwable t) { // java.lang.NoSuchMethodError if API_level<11
        Log.e(TAG,"cannot set the previous input method:");
        t.printStackTrace();
    }
    

    If you want to switch to a particular input method whose ID you know, you may do as the commented-out lines suggest.

    EDIT @pRaNaY suggested a single .getWindow() in a silent edit (click "edited" below to see the history). I remember that it did not work for Android 2.3; if you consult the docs, you will see that the first call, InputMethodService.getWindow() returns a Dialog (which is not a subclass of Window), and the second call, Dialog.getWindow() returns a Window. There is no Dialog.getAttributes(), so with a single .getWindow() it will not even compile.

    0 讨论(0)
  • 2020-12-30 06:26

    You cannot change the user's currently active IME through code for security reasons, sorry.

    However, you can show a system provided dialog to allow the user to select one of the other enabled ones.

    InputMethodManager imeManager = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE); 
    if (imeManager != null) {
        imeManager.showInputMethodPicker();
    } else {
        Toast.makeText(context ,"Error", Toast.LENGTH_LONG).show();
    }
    
    0 讨论(0)
  • 2020-12-30 06:48

    If you have rooted device, you can use /system/bin/ime utility.

    List all installed input methods: # ime list -a

    Set google's keyboard as default:

    ime set com.google.android.inputmethod.latin/com.android.inputmethod.latin.LatinIME
    
    0 讨论(0)
提交回复
热议问题