如何以编程方式更改输入法? [英] How do I change the input method programatically?

查看:160
本文介绍了如何以编程方式更改输入法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要根据语言的变化来更换键盘.

I have a requirement of changing the keyboard based on the change of language.

我做了一些研究,发现可以使用这些API来完成

I have done a bit of research and found that it can be done using these APIs

  1. InputMethodManager setInputMethod(android.os.IBinder,java.lang.String)
  2. InputMethodService switchInputMethod(java.lang.String)

对于第一个API,我需要一个 IBinder令牌,该令牌可以通过调用

For the 1st API, I need an IBinder token which can be taken from InputMethodService instance by calling

mInputMethodService.getWindow().getWindow().getAttributes().token

mInputMethodService.getWindow().getWindow().getAttributes().token

或者如果我引用了 InputMethodService 对象,我可以简单地调用

or if I have the reference to InputMethodService object I can simply call

mInputMethodService.switchInputMethod(id)

mInputMethodService.switchInputMethod(id)

更改输入法.

真正的问题是如何获得对InputMethodService对象的引用.

PS:我不想使用InputMethodManager的showInputMethodPicker(),因为我需要更改可以从我现有的对话框(其中包含语言列表)中找到它.

PS: I don't want to use InputMethodManager's showInputMethodPicker() because for my requirement I want to change it from my existing dialog which has a list of languages.

我知道这对于用户应用程序是不可能的,但是不确定是否对于系统应用程序也不可能.

I know that this is not possible for a user app but not sure if it's also not possible for a system app or not.

推荐答案

成功!

我想出的更改当前IME的唯一方法是对其进行自定义.

The only way I have figured out of changing the current IME is by customizing it.

我的回应.问题如果更改系统语言,我必须将键盘更改为中文从我的自定义设置应用程序翻译成中文.

For my resp. problem I have to change the keyboard to chinese if I change the system language to chinese from my custom Settings application.

下面讨论的方法用于自定义的 LatinIME 应用.

The approach discussed below was used for a custom LatinIME app.

每个IME都有一个扩展 InputMethodService 类的类.在此类中,我们可以重写一个方法称为 onInitializeInterface .每次更改配置时都会调用此方法,即当您更改系统区域设置时,它将被调用.

Every IME has a class that extends InputMethodService class. In this class we can override a method called onInitializeInterface. This method gets called everytime when the configuration changes i.e. when you change your system Locale it will be called.

在这里,我们可以检查当前选择的语言环境是否受支持是否使用当前的IME.如果不是,那么我们可以通过调用方法来加载其各自的IME switchInputMethod(id).

Here we can check whether the Locale that's currently been selected is supported by the current IME or not. If not then we can load its respective IME by calling the method switchInputMethod(id).

要获取ID,我们可以通过 inputMethodManager 进行查询并获取可用ID的列表

To get the id, we can query through inputMethodManager and get a list of available ids

    String pinyinId = "";

    InputMethodManager inputMethodManager = (InputMethodManager) getApplicationContext()
                    .getSystemService(INPUT_METHOD_SERVICE);
    List<InputMethodInfo> inputMethodInfos = inputMethodManager.getInputMethodList();

    for (InputMethodInfo inputMethodInfo : inputMethodInfos) {
            if (inputMethodInfo.getId().contains("pinyin")) {
                    pinyinId = inputMethodInfo.getId();
            }
    }

获取ID后,我们可以调用switchInputMethod(pinyinId),它将更改IME.

After getting the id we can call switchInputMethod(pinyinId) and it will change the IME.

这篇关于如何以编程方式更改输入法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆