java 以编程方式更改键盘输入语言

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36261166/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 01:10:08  来源:igfitidea点击:

Change Keyboard Input Language Programmatically

javaandroidkeyboard

提问by Hunain Usman

I am developing an application in which i need to allow the user to change the input keys shown in the default keyboard, upon request or by default, for example, i may prompt the user at the beginning to select the default language and after that, whenever the default keyboard is used, the app always displays the keys of the keyboard the selected language,

我正在开发一个应用程序,在该应用程序中,我需要允许用户根据请求或默认情况更改默认键盘中显示的输入键,例如,我可能会在开始时提示用户选择默认语言,然后,每当使用默认键盘时,应用程序始终显示所选语言的键盘键,

I know this is possible, because in default keyboard app, when multiple input methods are selected, then long pressing the spacebar allows to change the input methods at runtime, if this is possible then my requirement is also possible...

I know this is possible, because in default keyboard app, when multiple input methods are selected, then long pressing the spacebar allows to change the input methods at runtime, if this is possible then my requirement is also possible...

I dont want to prompt for default keyboard like following:

我不想提示输入默认键盘,如下所示:

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

I dont want to change the locale and restart my activity all the time like:

我不想更改语言环境并一直重新启动我的活动,例如:

Resources res = getBaseContext().getResources();
// Change locale settings in the app.
DisplayMetrics dm = res.getDisplayMetrics();
android.content.res.Configuration conf = res.getConfiguration();
conf.locale = new Locale("ru".toLowerCase());
res.updateConfiguration(conf, dm);
Log.i("inside onStart","after ever");   
setContentView(R.layout.activity_main);

I just want to show the keyboard input in my desired language.

我只想以我想要的语言显示键盘输入。

采纳答案by Hunain Usman

After doing some research here and there found the answer, first of all you have to create a custom keyboard View which extends keyboardView and in it create static key value variable like

在这里和那里做了一些研究之后找到了答案,首先你必须创建一个自定义键盘视图,它扩展了键盘视图,并在其中创建静态键值变量,如

static final int KEYCODE_LANGUAGE_SWITCH_ENG = -102;
static final int KEYCODE_LANGUAGE_SWITCH_URDU = -103;

after that in your IME class where you have implemented the inputMethodService, create the keyboards inside the onInitializeInterface override function. like

之后,在实现 inputMethodService 的 IME 类中,在 onInitializeInterface 覆盖函数中创建键盘。喜欢

mSymbolsKeyboard = new Keyboard(this, R.xml.qwerty2);
mEngQwertyKeyboard = new Keyboard(this, R.xml.eng_qwerty);

after this add these final static keys in the onKey override function as switch cases, and in the cases set the keyboards accordingly:

在此之后,在 onKey 覆盖函数中添加这些最终静态键作为开关案例,并在这些案例中相应地设置键盘:

setKeyboard(mEngQwertyKeyboard);

回答by Kalrav J Parsana

You can change keyboard without user notification only and only if your app is running as a System app for security reasons.

您可以在没有用户通知的情况下更改键盘,并且仅当您的应用程序出于安全原因作为系统应用程序运行时。

You need to give Android permission first in your app's AndroidManifest.xml

您需要先在应用程序的 AndroidManifest.xml 中授予 Android 权限

"android.permission.WRITE_SECURE_SETTINGS"

Then you need to determine id of your keyboard.

然后你需要确定你的键盘的 id。

-> To know id, you need to keep your keyboard default from setting menu manually then put this print somewhere,

-> 要知道 id,您需要手动从设置菜单中保留键盘默认值,然后将此打印放在某处,

System.out.println(Settings.Secure.getString(getContentResolver(),Settings.Secure.DEFAULT_INPUT_METHOD));

Once you print id and you know your keyboard id you can do as per below ( I have changed my default keyboard to Japanese )

打印 id 并知道键盘 id 后,您可以按照以下操作(我已将默认键盘更改为日语)

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

//imeManager.showInputMethodPicker(); //This is to see available keyboards.
imeManager.setInputMethod(null,"jp.co.omronsoft.openwnn/.OpenWnnJAJP");

Enjoy !!

享受 !!