Android:以编程方式切换到其他IME

时间:2013-05-22 05:58:35

标签: android ime

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

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

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

3 个答案:

答案 0 :(得分:14)

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

//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()返回Dialog(不是Window的子类),第二个电话Dialog.getWindow()返回Window。没有Dialog.getAttributes(),因此使用单个.getWindow()它甚至不会编译。

答案 1 :(得分:11)

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

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

InputMethodManager imeManager = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE); 
if (imeManager != null) {
    imeManager.showInputMethodPicker();
} else {
    Toast.makeText(context ,"Error", Toast.LENGTH_LONG).show();
}

答案 2 :(得分:0)

如果您已植根设备,则可以使用/ system / bin / ime实用程序。

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

将Google的键盘设置为默认键盘:

ime set com.google.android.inputmethod.latin/com.android.inputmethod.latin.LatinIME