切换手机的输入法
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());
}
}