Android输入法之——在代码中强制切换和获得当前输入法

来源:互联网 发布:亚信智慧数据科技 编辑:程序博客网 时间:2024/05/07 11:37

强制转换输入法

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”);

3.Settings.Secure.putString(ContentResolver resolver,String name, String value) (android.provider.Settings)

例如  Settings.Secure.putString(InPutChange.this.getContentResolver(),
                            Settings.Secure.DEFAULT_INPUT_METHOD,"com.android.inputmethod.pinyin/.PinyinIME"); (这里InPutChange是我自己写的一个Activity)
这个最简单  也是最后执行者


实际上当我们调用第二种方法时,它会调用第一种的方法,第一种方法又会调用第三种方法,

获得当前输入法

Settings.Secure.getString(ContentResolver resolver, String name)

例如   String ss=Settings.Secure.getString(InPutChange.this.getContentResolver(),

                Settings.Secure.DEFAULT_INPUT_METHOD);(这里InPutChange是我自己写的一个Activity)

这样可以获得当前输入法种类的id     我当前系统目前输入法的结果是"com.android.inputmethod.pinyin/.PinyinIME"     所以ss结果为"com.android.inputmethod.pinyin/.PinyinIME"





原创粉丝点击