切换手机的输入法

切换手机的输入法
  public static void switchInputMethod(Context context){
        try
        {
            //Get list of input methods
            List<InputMethodInfo> InputMethods=((InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE)).getEnabledInputMethodList();

            String NewInputMethodName=InputMethods.get(0).getId(); //Pick the first input method to switch to
            Log.i("Demo Error", "SWITCHING TO: "+NewInputMethodName);
            String curInputMethodId = Settings.Secure.getString(context
                    .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
            Log.i("Demo Error", "CURRENT IME: "+curInputMethodId);  
            Settings.Secure.putString(context.getContentResolver(),
                    Settings.Secure.DEFAULT_INPUT_METHOD, NewInputMethodName);

            if (context.checkCallingOrSelfPermission(
                    android.Manifest.permission.WRITE_SECURE_SETTINGS)
                    != PackageManager.PERMISSION_GRANTED) {
                // PERMISSION_GRANTED == 0
                Log.i("Demo Error", "myActivity requires permission "
                        + android.Manifest.permission.WRITE_SECURE_SETTINGS);
            }

            ((InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE))
                    .setInputMethod(null, NewInputMethodName);

        } catch(Exception e) {
            Log.i("Demo Error", e.getMessage());
        }
    }






注:参考资料:http://blog.csdn.net/zhangyongfeiyong/article/details/54691507

1、获取手机可以使用的输入法列表

 List<InputMethodInfo> InputMethods=((InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE)).getEnabledInputMethodList();

  for(int i=0;i<InputMethods.size();i++){
               InputMethodInfo inputMethod = InputMethods.get(i);
               System.out.println("input method "+i+" ...pacageName ..."+inputMethod.getPackageName());
               System.out.println("input method " + i + " ...component ..." + inputMethod.getComponent());
           }

2、获取手机中的输入法列表

 InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        List<InputMethodInfo> list = inputMethodManager.getInputMethodList();
        System.out.println("集合是......................"+list.toString());
        inputMethodManager.showInputMethodPicker();

3、获取手机中默认的输入法

  public String getDefaultInputMethod() {
        String defaultInputMethod = null;
        String inputComponent = Settings.Secure.getString(getContentResolver(),
                Settings.Secure.DEFAULT_INPUT_METHOD);
        if (!inputComponent.isEmpty()) {
            System.out.println("zyf input method is : " + inputComponent);
            // 获取的结果是:com.sohu.inputmethod.sogouoem/.SogouIME,故需要截取一下字符串
            defaultInputMethod = inputComponent.substring(0, inputComponent.indexOf("/"));
        }
        return defaultInputMethod;
    }


4、设置手机的输入法

  public static void switchInputMethod(Context context) {
        try {
            //Get list of input methods
            System.out.println("switchInputMethod.........................");
            List<InputMethodInfo> InputMethods = ((InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE)).getEnabledInputMethodList();
           
            String NewInputMethodName = "";
            for (int i = 0; i < InputMethods.size(); i++) {
                InputMethodInfo inputMethod = InputMethods.get(i);
                NewInputMethodName = inputMethod.getId();
                System.out.println("input method " + i + " ...id ..." + inputMethod.getId());
            }
         
            String curInputMethodId = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
            Settings.Secure.putString(context.getContentResolver(),
                    Settings.Secure.DEFAULT_INPUT_METHOD, NewInputMethodName);
            if (context.checkCallingOrSelfPermission(android.Manifest.permission.WRITE_SECURE_SETTINGS) != PackageManager.PERMISSION_GRANTED) {
                // PERMISSION_GRANTED == 0
                Log.i("Demo Error", "myActivity requires permission "
                        + android.Manifest.permission.WRITE_SECURE_SETTINGS);
            }
            ((InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE))
                    .setInputMethod(null, iInputMethodName
);
        } catch (Exception e) {
            Log.i("Demo Error", e.getMessage());
        }
    }







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值