在IME和Android输入法之间进行切换
16lz
2021-01-23
publicvoidswitchInputMethod(ActivitymyActivity,EditTextinputText){
try
{
//Getlistofinputmethods
List<InputMethodInfo>InputMethods=((InputMethodManager)myActivity.getSystemService(Context.INPUT_METHOD_SERVICE)).getEnabledInputMethodList();
StringnameIME=InputMethods.get(3).getServiceName();
Log.i("DemoError","SWITCHINGTO:"+nameIME);
StringNewInputMethodName=InputMethods.get(3).getId();//Pickthefirstinputmethodtoswitchto
Log.i("DemoError","SWITCHINGTO:"+NewInputMethodName);
StringcurInputMethodId=Settings.Secure.getString(myActivity
.getContentResolver(),Settings.Secure.DEFAULT_INPUT_METHOD);
Log.i("DemoError","CURRENTIME:"+curInputMethodId);
//Solution1(X)
//switchInputMethodisamethodofandroid.inputmethodservice.InputMethodService
// switchInputMethod(NewInputMethodName);//Thisthrowsanerror
//Solution2(OK)
Settings.Secure.putString(myActivity.getContentResolver(),
Settings.Secure.DEFAULT_INPUT_METHOD,NewInputMethodName);
//Solution3(OK)
if(myActivity.checkCallingOrSelfPermission(
android.Manifest.permission.WRITE_SECURE_SETTINGS)
!=0){
//PERMISSION_GRANTED==0
Log.i("DemoError","myActivityrequirespermission"
+android.Manifest.permission.WRITE_SECURE_SETTINGS);
}
((InputMethodManager)myActivity.getSystemService(Context.INPUT_METHOD_SERVICE))
.setInputMethod(null,NewInputMethodName);
//ShowSoftInputKeyboard
((InputMethodManager)myActivity.getSystemService(Context.INPUT_METHOD_SERVICE))
.showSoftInput(inputText,0);
}catch(Exceptione){
Log.i("DemoError",e.getMessage());
}
}
try
{
//Getlistofinputmethods
List<InputMethodInfo>InputMethods=((InputMethodManager)myActivity.getSystemService(Context.INPUT_METHOD_SERVICE)).getEnabledInputMethodList();
StringnameIME=InputMethods.get(3).getServiceName();
Log.i("DemoError","SWITCHINGTO:"+nameIME);
StringNewInputMethodName=InputMethods.get(3).getId();//Pickthefirstinputmethodtoswitchto
Log.i("DemoError","SWITCHINGTO:"+NewInputMethodName);
StringcurInputMethodId=Settings.Secure.getString(myActivity
.getContentResolver(),Settings.Secure.DEFAULT_INPUT_METHOD);
Log.i("DemoError","CURRENTIME:"+curInputMethodId);
//Solution1(X)
//switchInputMethodisamethodofandroid.inputmethodservice.InputMethodService
// switchInputMethod(NewInputMethodName);//Thisthrowsanerror
//Solution2(OK)
Settings.Secure.putString(myActivity.getContentResolver(),
Settings.Secure.DEFAULT_INPUT_METHOD,NewInputMethodName);
//Solution3(OK)
if(myActivity.checkCallingOrSelfPermission(
android.Manifest.permission.WRITE_SECURE_SETTINGS)
!=0){
//PERMISSION_GRANTED==0
Log.i("DemoError","myActivityrequirespermission"
+android.Manifest.permission.WRITE_SECURE_SETTINGS);
}
((InputMethodManager)myActivity.getSystemService(Context.INPUT_METHOD_SERVICE))
.setInputMethod(null,NewInputMethodName);
//ShowSoftInputKeyboard
((InputMethodManager)myActivity.getSystemService(Context.INPUT_METHOD_SERVICE))
.showSoftInput(inputText,0);
}catch(Exceptione){
Log.i("DemoError",e.getMessage());
}
}
更多相关文章
- 代码中设置drawableleft
- android 3.0 隐藏 系统标题栏
- Android开发中activity切换动画的实现
- Android(安卓)学习 笔记_05. 文件下载
- Android中直播视频技术探究之—摄像头Camera视频源数据采集解析
- 技术博客汇总
- android 2.3 wifi (一)
- AndRoid Notification的清空和修改
- Android中的Chronometer