美文网首页
获取android切换后的输入法状态

获取android切换后的输入法状态

作者: 夏广成 | 来源:发表于2016-07-15 16:31 被阅读192次

最近产品提了这么一个需求。当用户在editText中开始输入时,如果输入的是英文,就只能显示八位,如果输入的是拼音,当用户还没有点击汉字的时候,可以输入超过八位,当用户点击汉字要输入的汉字后,就只显示前八位。
首先看了一下小米的情况,小米默认的输入法,则是当用户处于中文输入时,输入的拼音暂时是放在软键盘的上面,而非输入框中,这样就不存在拼音在输入框中超过八位的情况。
再看一下华为,华为的呢,swype输入法处于中文状态时,拼音则显示在了editText输入框中,只有当用户点击软键盘上的汉字时,才会更换输入框中的拼音为汉字。
首先考虑的是,如何能够拿到用户软键盘的输入法切换状态,也就是中文状态还是英文状态。

第一步:获取输入法服务

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

第二步:获取所有输入法包名

// 取得当前所有的输入法
List<InputMethodInfo> infos = imm.getInputMethodList();
for (InputMethodInfo info : infos){
    System.out.println("输入法包名:" + info.getPackageName());
        //com.android.inputmethod.latin(此为Android键盘(AOSP))
        //com.sohu.inputmethod.sogou(此为搜狗)
}

或者取得已勾选的输入法包名

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

第三步 遍历所有的输入法包名和输入法语言

//取得当前所有的输入法
        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());
            }
        }

第四步取得当前输入法的语言

Settings.Secure.getString(getContentResolver(),Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE) 

第五步设置默认输入法

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

第六步设置默认输入法语言

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

问题是,面对华为的swype输入法,返回的始终只有一个"312418802"无论我怎么切换中英文,返回的都是这个312418802.

 输入法包名:com.nuance.swype.emui
09-21 12:12:54.586 13065-13065/com.cn.stepcounter I/System.out: 输入法子类型数量:55
09-21 12:12:54.586 13065-13065/com.cn.stepcounter I/System.out: 子类型语言环境:    ,hashcode:312418802
09-21 12:12:54.586 13065-13065/com.cn.stepcounter I/System.out: 子类型语言环境:  en_US  ,hashcode:1802354150
09-21 12:12:54.586 13065-13065/com.cn.stepcounter I/System.out: 子类型语言环境:  af  ,hashcode:-1111321705
09-21 12:12:54.586 13065-13065/com.cn.stepcounter I/System.out: 子类型语言环境:  ar  ,hashcode:-1100239453
09-21 12:12:54.586 13065-13065/com.cn.stepcounter I/System.out: 子类型语言环境:  be  ,hashcode:-1083616075
09-21 12:12:54.586 13065-13065/com.cn.stepcounter I/System.out: 子类型语言环境:  bg  ,hashcode:-1081769033
09-21 12:12:54.586 13065-13065/com.cn.stepcounter I/System.out: 子类型语言环境:  ca  ,hashcode:-1058681008
09-21 12:12:54.586 13065-13065/com.cn.stepcounter I/System.out: 子类型语言环境:  cs  ,hashcode:-1042057630
09-21 12:12:54.586 13065-13065/com.cn.stepcounter I/System.out: 子类型语言环境:  da  ,hashcode:-1030051857
09-21 12:12:54.586 13065-13065/com.cn.stepcounter I/System.out: 子类型语言环境:  de  ,hashcode:-1026357773
09-21 12:12:54.586 13065-13065/com.cn.stepcounter I/System.out: 子类型语言环境:  el  ,hashcode:-991263975
09-21 12:12:54.586 13065-13065/com.cn.stepcounter I/System.out: 子类型语言环境:  en_GB  ,hashcode:1385846179

相关文章

网友评论

      本文标题:获取android切换后的输入法状态

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