13

http://developer.android.com/guide/topics/text/creating-input-method.html#GeneralDesign 内容如下:

由于设备上可能安装了多个 IME,因此为用户提供一种直接从输入法 UI 切换到不同 IME 的方法。

假设我有两个输入法的来源并且可以修改它。我想让用户在它们之间快速切换,并准备为此保留一个按钮。如何“直接从输入法 UI 切换到不同的 IME”?

4

3 回答 3

18

从当前输入法切换到上一个输入法是:

//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();
}

如果您想切换到您知道其 ID 的特定输入法,您可以按照注释掉的行的建议进行操作。

编辑@pRaNaY.getWindow()在静默编辑中建议了一个单曲(单击下面的“已编辑”以查看历史记录)。我记得它不适用于 Android 2.3;如果您查阅文档,您会看到第一次调用InputMethodService.getWindow()返回 a Dialog(不是 的子类Window),第二次调用Dialog.getWindow()返回 a Window。没有Dialog.getAttributes(),所以用一个.getWindow()它甚至不会编译。

于 2013-05-24T07:09:57.050 回答
12

出于安全原因,您不能通过代码更改用户当前活动的 IME,抱歉。

但是,您可以显示系统提供的对话框,以允许用户选择其他已启用的对话框之一。

InputMethodManager imeManager = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE); 
if (imeManager != null) {
    imeManager.showInputMethodPicker();
} else {
    Toast.makeText(context ,"Error", Toast.LENGTH_LONG).show();
}
于 2013-05-22T05:59:44.667 回答
0

如果您有 root 设备,您可以使用 /system/bin/ime 实用程序。

列出所有已安装的输入法:# ime list -a

将谷歌的键盘设置为默认:

ime set com.google.android.inputmethod.latin/com.android.inputmethod.latin.LatinIME
于 2020-08-25T06:22:00.790 回答