Android输入法之——如何在代码中强制切换输入法
来源:互联网 发布:定制家具 知乎 编辑:程序博客网 时间:2024/05/16 18:51
转载自:http://xiaohang.org/2011/02/android%E8%BE%93%E5%85%A5%E6%B3%95%E4%B9%8B%E2%80%94%E2%80%94%E5%A6%82%E4%BD%95%E5%9C%A8%E4%BB%A3%E7%A0%81%E4%B8%AD%E5%BC%BA%E5%88%B6%E5%88%87%E6%8D%A2%E8%BE%93%E5%85%A5%E6%B3%95/
由于工作需要,追踪Android输入法Framework中了关于输入法切换的实现过程或者说是API调用的部分。
找到如下两个API:
1.InputMethodManager.setInputMethod (IBinder token, String id)
public void setInputMethod (IBinder token, String id)
Force switch to a new input method component. This can only be called from the currently active input method, as validated by the given token.
Parameters
token Supplies the identifying token given to an input method when it was started, which allows it to perform this operation on itself.
id The unique identifier for the new input method to be switched to.
此函数,当然是可以实现输入法的强制切换的,但是,IBinder的获得却需要一定的权限,调用起来需要比较深入,哥们能力有限专研的一阵子未果。日后,再做深入探讨吧。哪位仁兄有解,欢迎留言,不胜感激!!
2.InputMethodService.switchInputMethod(String id)
public void switchInputMethod (String id)
Since: API Level 3 Force switch to a new input method, as identified by id. This input method will be destroyed, and the requested one started on the current input field.
Parameters
id Unique identifier of the new input method ot start.
相对第一个API来说,这个却简单的多。最直观来看的话,就是这里不用获得相应的IBinder!我们几乎已经胜利了,但是,还是需要点破这层纸的。
首先,关于该API在何处调用?
该API属于类InputMethodService,而每个输入应用的入口类又都是继承于该类的,所以,在输入法入口类调用该API就可以了。比如,Google的SDK中给出的Google拼音输入法,就在类PinyinIME.java中调用就可以了!
再者,关于参数id?
从定义来看,它是一个String类型,看Google的解释也未免笼统。经小可仔细专研,原来,是这样的,还是用例子来说吧,依然选择上述例子。 Google拼音输入法的id就是:com.android.inputmethod.pinyin/.PinyinIME。完整的调用方法就 是:switchInputMethod(“com.android.inputmethod.pinyin/.PinyinIME”);
那么,如果不在入口类中如何调用呢?
在需要调用的类中定义:private PinyinIME mImeService;
需要的地方调用就可以了:mImeService.switchInputMethod(“com.android.inputmethod.pinyin/.PinyinIME”);
OK!打完收工!
- Android输入法之——如何在代码中强制切换输入法
- Android输入法之——如何在代码中强制切换输入法
- Android输入法之——在代码中强制切换和获得当前输入法
- Android在Fragment中强制隐藏输入法
- android模拟器如何切换输入法
- Android在dialog中如何隐藏输入法
- Android输入法API及如何强制修改默认输入法
- 如何在程序中自动控制切换中英文输入法?
- 如何在程序中自动控制切换中英文输入法?
- 如何在程序中自动控制切换中英文输入法?
- 如何在Edit中切换为默认输入法
- 如何在PB中切换中英文输入法数
- 在Android app中切换自定义的输入法
- Android 代码中操作输入法
- 在Winform应用程序中精确切换输入法
- 在phpstorm中无法切换输入法
- Android 切换输入法
- Android 切换输入法
- ASP.NET中在IHttpHandler里执行Server.Transfer时提示执行子请求时出错。(http://codeadmin.blog.163.com/blog/static/1158046532009512114926448/)
- 做更好的自己
- 介绍VC6.0的18个实用小技巧
- Linux File 结构说明
- wordpress访问加速
- Android输入法之——如何在代码中强制切换输入法
- WIN32结构化异常处理探秘(转)网址
- 快速返回目录
- Memcached缓存版本小结
- Android之Tab分页标签的实现方法一-----TabActivity和TabHost的结合(三)
- SQL例
- 一道腾讯面试题,自己解出,供大家参考参考
- 蛋疼的ubuntu 图形桌面
- 关于str_replace和strtr函数