Android 以编程方式切换键盘配置文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11036435/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Switch keyboard profile programmatically
提问by rajankz
Is there any way that we can switch installed keyboards programmatically (without going to the settings section manually)?
有什么方法可以以编程方式切换已安装的键盘(无需手动进入设置部分)?
My requirement is that the user is presented with all the keyboards which are installed on the phone and gets a chooser dialog to switch to the one wishes?
我的要求是向用户提供安装在手机上的所有键盘,并获得一个选择器对话框以切换到一个愿望?
(basically we want to cut down the step to transfer him to the settings page)
(基本上我们想减少将他转移到设置页面的步骤)
回答by Robert
This piece of code will fulfill your requirements:
这段代码将满足您的要求:
InputMethodManager imeManager = (InputMethodManager) getApplicationContext().getSystemService(INPUT_METHOD_SERVICE);
imeManager.showInputMethodPicker();
As Commonsware points out in his answer, there is no way to do this behind the user's back.
正如 Commonsware 在他的回答中指出的那样,没有办法在用户背后这样做。
回答by kpninja12
If your app has system privileges, and has the permission
如果你的应用有系统权限,并且有权限
<uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS" />
you can programatically enable the keyboard and set it as the current keyboard by making it the default keyboard WITHOUT USER KNOWLEDGE OR INTERVENTION!
您可以通过编程方式启用键盘并将其设置为当前键盘,方法是将其设为默认键盘,无需用户知识或干预!
//get the old default keyboard in case you want to use it later, or keep it enabled
String oldDefaultKeyboard = Settings.Secure.getString(resolver, Setting.Secure.DEFAULT_INPUT_METHOD);
//enable your keyboard
Settings.Secure.putString(resolver, Settings.Secure.ENABLED_INPUT_METHODS, "com.my.keyboard/.full.path");
//set your keyboard as the new default keyboard
Settings.Secure.putString(resolver, Settings.Secure.DEFAULT_INPUT_METHOD, "com.my.keyboard/.full.path");
You can enable multiple keyboards (such as the default keyboard and your own) by providing a list of keyboards to the ENABLED_INPUT_METHODS
, separated by ':'. See docs
您可以通过向 提供键盘列表来启用多个键盘(例如默认键盘和您自己的键盘)ENABLED_INPUT_METHODS
,以 ':' 分隔。查看文档
You can verify your keyboard's full package and path ID by invoking ime list -a
through adb shell
您可以通过调用ime list -a
来验证键盘的完整包和路径 IDadb shell
回答by Alexander Malakhov
If you have rooted device, you can use /system/bin/ime
utility.
如果您有 root 设备,则可以使用/system/bin/ime
实用程序。
List all installed input methods: # ime list -a
列出所有已安装的输入法: # ime list -a
Set google's keyboard as default:# ime set com.google.android.inputmethod.latin/com.android.inputmethod.latin.LatinIME
将 google 的键盘设置为默认值:# ime set com.google.android.inputmethod.latin/com.android.inputmethod.latin.LatinIME
On Java side use Runtime.getRuntime().exec(...).
在 Java 端使用Runtime.getRuntime().exec(...)。
回答by CommonsWare
Is there any way that we can switch installed keyboards programmatically (without going to the settings section)?
有什么方法可以以编程方式切换已安装的键盘(无需进入设置部分)?
Fortunately, no, for security reasons. If an app could dictate what input method editor is used, malware would change the input method editor to their keylogger.
幸运的是,没有,出于安全原因。如果应用程序可以决定使用哪种输入法编辑器,恶意软件会将输入法编辑器更改为他们的键盘记录器。
回答by Andevel
InputMethodManager imeManager = (InputMethodManager) getApplicationContext().getSystemService(INPUT_METHOD_SERVICE);
imeManager.showInputMethodPicker();
This code will prompt user to change default keyboard
此代码将提示用户更改默认键盘
回答by Darkman
import android.content.Intent;
import android.view.inputmethod.InputMethodManager;
// To enable keyboard
startActivity(new Intent("android.settings.INPUT_METHOD_SETTINGS"));
// To activate the keyboard
InputMethodManager imeManager = (InputMethodManager)
getApplicationContext().getSystemService(INPUT_METHOD_SERVICE);
imeManager.showInputMethodPicker();