【问题标题】:How can I get a list of all the Input Methods (and their names) that are installed on the phone?如何获取手机上安装的所有输入法(及其名称)的列表?
【发布时间】:2011-05-11 17:44:06
【问题描述】:

我正在尝试显示手机上当前安装的所有输入法的列表。通过这样做,我得到了 InputMethodInfo 对象的列表:

InputMethodManager imeManager = (InputMethodManager)getApplicationContext().getSystemService(INPUT_METHOD_SERVICE);
List<InputMethodInfo> InputMethods = imeManager.getEnabledInputMethodList();

这可行,但 InputMethodInfo 对象在任何地方都没有友好的名称。例如,它会给我“com.swype.android.inputmethod.SwypeInputMethod”而不是“Swype”

这不是向用户显示列表的一种非常友好的方式,而且这些包名称不遵循严格的模式,因此我无法可靠地将键盘名称从类名中解析出来。

我什至试图变得非常花哨并获取 InputMethodInfo 对应的 ServiceInfo 对象,这样我就可以解析它的标签资源整数,但每次运行时我都会得到 NameNotFoundExceptions。

ComponentName componentName = inputMethodInfo.getComponent();
ServiceInfo serviceInfo = packageManager.getServiceInfo(componentName, 0);
Resources resources = getResources();
try 
{
 String imeServiceLabel = resources.getString(serviceInfo.labelRes);
} 
catch (NameNotFoundException e) { }

有人知道如何做到这一点吗?我不在乎我必须怎么做,我只需要能够生成一个输入法列表,因为它们出现在手机的语言和键盘菜单中,然后存储用户的选择。我想也许我可以使用 InputMethodManager 来启动标准输入法选择菜单,然后通过查看菜单关闭后当前选择的 IME 来查看用户选择了哪一个,但据我所知,没有办法看到当前在系统中选择了哪个 IME。

【问题讨论】:

    标签: android methods input ime


    【解决方案1】:

    当它就在我的眼皮底下时,我浪费了很多时间。我在做其他事情时发现了自己问题的答案。这将通过常规 InputMethodInfo 对象为您提供输入法的友好名称。

    inputMethodInfo.loadLabel(packageManager).toString();

    我太习惯于编写 C#,其中所有内容都作为属性实现,因此我通过在运行时查看对象属性来进行大部分调查。然而,Java 通过公共方法提供了大部分此类信息,当 Eclipse 中弹出自动完成框时,我恰好注意到了这一点。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-11-09
      • 2017-12-23
      • 1970-01-01
      • 2011-10-04
      • 2020-05-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多