以编程方式切换键盘配置文件

问题描述 投票:0回答:5

有什么方法可以让我们以编程方式切换已安装的键盘(无需手动进入设置部分)?

我的要求是向用户展示手机上安装的所有键盘,并获得一个选择器对话框以切换到想要的键盘?

(基本上我们想减少把他转到设置页面的步骤)

android android-softkeyboard
5个回答
43
投票

这段代码将满足您的要求:

InputMethodManager imeManager = (InputMethodManager) getApplicationContext().getSystemService(INPUT_METHOD_SERVICE);
imeManager.showInputMethodPicker();

正如 Commonsware 在他的回答中指出的那样,没有办法在用户背后执行此操作。


18
投票

如果您的应用有系统权限,并且有权限

<uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS" />

您可以通过编程方式启用键盘并将其设置为默认键盘,将其设置为当前键盘无需用户知识或干预!

//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");

您可以通过向

ENABLED_INPUT_METHODS
提供键盘列表(以“:”分隔)来启用多个键盘(例如默认键盘和您自己的键盘)。请参阅文档

您可以通过调用

ime list -a
adb shell

来验证键盘的完整包和路径 ID

15
投票

如果您有 root 设备,则可以使用

/system/bin/ime
实用程序。

列出所有已安装的输入法:

# ime list -a

将谷歌键盘设置为默认:

# ime set com.google.android.inputmethod.latin/com.android.inputmethod.latin.LatinIME

在 Java 端使用 Runtime.getRuntime().exec(...).


5
投票

有什么方法可以让我们以编程方式切换已安装的键盘(无需进入设置部分)?

幸运的是,出于安全原因,没有。如果应用程序可以指定使用什么输入法编辑器,则恶意软件会将输入法编辑器更改为其键盘记录器。


2
投票
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();
© www.soinside.com 2019 - 2024. All rights reserved.