Android 切换输入法

 

Code segment of project "zMyLayout_Jiang"

try
{
	//Get list of input methods
	List<InputMethodInfo> InputMethods=((InputMethodManager)myActivity.getSystemService(Context.INPUT_METHOD_SERVICE)).getEnabledInputMethodList();
	String nameIME = InputMethods.get(0).getServiceName();
	Log.i("Demo Error", "SWITCHING TO: "+nameIME);
	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(myActivity
		.getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
	 Log.i("Demo Error", "CURRENT IME: "+curInputMethodId);
	// Solution 1 (X)
	// switchInputMethod is a method of android.inputmethodservice.InputMethodService
//	switchInputMethod(NewInputMethodName); //This throws an error
	// Solution 2 (OK)
	Settings.Secure.putString(myActivity.getContentResolver(),
	Settings.Secure.DEFAULT_INPUT_METHOD, NewInputMethodName);
	// Solution 3 (OK)
	if (myActivity.checkCallingOrSelfPermission(
		android.Manifest.permission.WRITE_SECURE_SETTINGS)
                        != 0) {
                	// PERMISSION_GRANTED == 0
			Log.i("Demo Error", "myActivity requires permission "
				+ android.Manifest.permission.WRITE_SECURE_SETTINGS);
	}
	((InputMethodManager)myActivity.getSystemService(Context.INPUT_METHOD_SERVICE))
		.setInputMethod(null, NewInputMethodName);
				
	// Show SoftInput Keyboard
	EditText inputText = (EditText)myActivity.findViewById(R.id.inputText);
	((InputMethodManager)myActivity.getSystemService(Context.INPUT_METHOD_SERVICE))
		.showSoftInput(inputText, 0);
} catch(Exception e) {
	Log.i("Demo Error", e.getMessage());
}

 

 

 

你可能感兴趣的:(exception,android,String,service,input,输入法)