以编程方式切换到其他IME

以编程方式切换到其他IME

本文介绍了Android:以编程方式切换到其他IME的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

让我们假设我有两种输入法的源,并且可以对其进行修改.我想让用户在他们之间快速切换,并准备为此保留一个按钮.如何直接从输入法UI切换到其他IME"?

Let's assume I have the source of two input methods and can modify it.I want to let the user switch between them quickly and am ready to reserve a button for that.How do I "switch to a different IME directly from the input method UI"?

推荐答案

从当前输入法切换到以前的输入法 是:

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

如果要切换到您知道其ID的特定输入法,则可以按照注释行的提示进行操作.

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建议在静默编辑中使用单个.getWindow()(单击下面的已编辑"以查看历史记录).我记得它不适用于Android 2.3;如果您查阅文档,将会看到第一个调用InputMethodService.getWindow()返回Dialog(它不是Window的子类),第二个调用Dialog.getWindow()返回Window.没有Dialog.getAttributes(),因此只有一个.getWindow()甚至无法编译.

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.

这篇关于Android:以编程方式切换到其他IME的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 20:16