Android 修改输入法的输入语言

来源:互联网 发布:巨人网络招聘 编辑:程序博客网 时间:2024/05/01 08:32

Android原生系统中,默认的输入法是“Android键盘(AOSP)”,此输入法包括55种输入语言,具体有哪一些,去看看原生系统里的就知道了,目测没有中文(不知道为啥)。

还有一种“Google语音输入”,包含78种输入语言,包括中文。


如果安装了其他的输入法,其输入语言得看输入法自身的了,比如搜狗中文就只有中文一种输入语言。


输入法操作有些是需要root权限的,比如勾选输入法等,需要的权限如下:

<uses-permission android:name="android.permission.WRITE_SETTINGS" />
    <uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS" />


下面介绍如何操作输入法。


0、取得输入法系统服务:

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



1、取得所有已安装的输入法:

// 取得当前所有的输入法List<InputMethodInfo> infos = imm.getInputMethodList();for (InputMethodInfo info : infos){System.out.println("输入法包名:" + info.getPackageName());}

我的机器上安装了搜狗输入,所以所有的输入法包名是:

com.sohu.inputmethod.sogou(此为搜狗)

com.google.android.googlequicksearchbox(此为Google语音输入)

com.android.inputmethod.latin(此为Android键盘(AOSP))


每种输入法的包名是固定的,这个操作是用来取得所有的输入法包名,以便判断系统中有哪些输入法可以使用



2、取得已经勾选的输入法,已勾选的输入法可以在系统“设置”里看到:

String enable = Settings.Secure.getString(getContentResolver(),Settings.Secure.ENABLED_INPUT_METHODS);


我的enable内容是:

com.android.inputmethod.latin/.LatinIME;-486540198;-921088104:
com.sohu.inputmethod.sogou/.SogouIME:
com.google.android.googlequicksearchbox/com.google.android.voicesearch.ime.VoiceInputMethodService

可以看到输入法与输入法之间是用:“:”,分号隔开的。

其中

com.android.inputmethod.latin/.LatinIME;-486540198;-921088104
后面的数字是此输入法已经开启的输入语言对应的数字,是一个hash值,可以通过hashCode方法得到。

以上的数字分别代表葡萄牙语和英语,其英文代码分别是:pt_PT、en_US





3、查看所有的输入法和每一个输入法的所有输入语言:

// 取得当前所有的输入法List<InputMethodInfo> infos = imm.getInputMethodList();System.out.println("\n当前输入法数量:" + infos.size());for (InputMethodInfo info : infos){System.out.println("\n输入法包名:" + info.getPackageName());int sum = info.getSubtypeCount();System.out.println("输入法子类型数量:" + sum);for (int i = 0; i < sum; i++){// 取得输入法中包含的每一个子类型final InputMethodSubtype subtype = info.getSubtypeAt(i);// 子类型的语言环境final String locale = subtype.getLocale().toString();System.out.println("\n子类型语言环境:  " + locale + "  ,hashcode:"+ subtype.hashCode());}}

4、勾选输入法,也可以在勾选的同时选定输入法要开启的输入语言:

Settings.Secure.putString(getContentResolver(),Settings.Secure.ENABLED_INPUT_METHODS,"com.android.inputmethod.latin/.LatinIME;-1100561836;1494081088;1244756446;843948332;-486540198;-921088104:"+ "com.sohu.inputmethod.sogou/.SogouIME:"+ "com.google.android.googlequicksearchbox/com.google.android.voicesearch.ime.VoiceInputMethodService");


5、取得当前输入法

String currentInputmethod = Settings.Secure.getString(getContentResolver(),Settings.Secure.DEFAULT_INPUT_METHOD);


我当前的输入法是:

com.sohu.inputmethod.sogou/.SogouIME



6、如果当前输入法是系统输入法,取得系统输入法当前使用的输入语言,注意是系统输入法:

<pre name="code" class="java">String current = 
Settings.Secure.getString(getContentResolver(), Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE)

我当前的系统输入法语言是:

-921088104

代表英文。


7、设置默认输入法:

Settings.Secure.putString(getContentResolver(),Settings.Secure.DEFAULT_INPUT_METHOD,"com.android.inputmethod.latin/.LatinIME");

其中

com.android.inputmethod.latin/.LatinIME

是系统输入法,如果要设置为搜狗,则为:

com.sohu.inputmethod.sogou/.SogouIME



8、设置当前输入法的输入语言,当然,得当前输入法支持该语言才行:

Settings.Secure.putInt(getContentResolver(), Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE,-921088104);


总结,以上方法已足够使用,具体使用时还是需要具体问题具体分析,根据需求去组合出自己的业务逻辑。











0 0