美文网首页
Android动态切换输入法

Android动态切换输入法

作者: Elfkind | 来源:发表于2021-07-30 15:28 被阅读0次

Android中若想在自己的App中实现输入法切换,可以有两种方式,一种是直接代码中强制切换输入法,另一种是弹出输入法选择框,提示用户切换.

强制切换输入法方式:

InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.setInputMethod(IBinder token, NewInputMethodName);

这种方式需要获取token,哪位大神指点下如何获取token...

NewInputMethodName为对应输入法的包信息,格式为com.sohu.inputmethod.sogou.xiaomi/.SogouIME,下面有获取NewInputMethodName的方法可参考.

但是这种切换方法是强制使用代码方式切换输入法的,由于安全原因,Google进行了限制,这种方法要求具有系统权限android.permission.WRITE_SECURE_SETTINGS,需要你的APP是系统级的,所以没有安卓源码的话,不太好实现.

弹出选择框方式:

获取InputMethodManager,调用showInputMethodPicker()方法即可弹出切换框,此种方法不需添加系统权限.

InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showInputMethodPicker();

获取系统所有输入法类型的方式为:
//拿到输入法的全部条目

InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
List<InputMethodInfo> list = imm.getInputMethodList();
//textView中显示输入法的全部条目
textView.setText(list.toString());

附:各主流输入法的NewInputMethodName
搜狗输入法:com.sohu.inputmethod.sogou/.SogouIME
百度输入法:com.baidu.input/.ImeService
谷歌拼音输入法:com.android.inputmethod.pinyin/.PinyinIME
默认键盘输入法:com.android.inputmethod.latin/.LatinIME
讯飞输入法: com.iflytek.inputmethod/.FlyIME

相关文章

网友评论

      本文标题:Android动态切换输入法

      本文链接:https://www.haomeiwen.com/subject/nhqcvltx.html