简体   繁体   English

以编程方式更改android上的物理键盘(Android Studio)

[英]change physical keyboard on android programmatically (Android Studio)

I want to change the physical keyboard layout to Hebrew when using my keyboard in a programmatic way. 我想以编程方式使用键盘时将物理键盘布局更改为希伯来语。 Right now when I press on the physical keyboard it inputs English characters. 现在,当我按下物理键盘时,它会输入英文字符。 I'm developing my app for Android lollipop and higher. 我正在为Android棒棒糖及更高版本开发我的应用程序。

OK after some research I found out a solution for this problem - I had to implement the KeyEvent.Callback interface, and override the onKeyDown method. 经过一番研究后,我找到了解决此问题的方法-我必须实现KeyEvent.Callback接口,并重写onKeyDown方法。 This method inputs me the key code that was pressed, and I have to translate it into the character in the language I want to write in. This is what the code looks like: 该方法输入了我按下的键代码,我必须将其转换为我要编写的语言中的字符。代码如下所示:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    Toast.makeText(getApplicationContext(),"Keycode="+keyCode,Toast.LENGTH_LONG).show();
    handleKey('ש');
    return true;

}
public void handleKey(int keyCode)
{
    InputConnection inputConnection = getCurrentInputConnection();

    if (inputConnection != null) {

        switch(keyCode) {
            case Keyboard.KEYCODE_DELETE :
                CharSequence selectedText = inputConnection.getSelectedText(0);

                if (TextUtils.isEmpty(selectedText)) {
                    inputConnection.deleteSurroundingText(1, 0);
                } else {
                    inputConnection.commitText("", 1);
                }

                break;
            default :
                char code = (char) keyCode;
                inputConnection.commitText(String.valueOf(code), 1);
        }
    }

Here a link to where I found this solution on Google's documentation: https://developer.android.com/training/keyboard-input/commands 这是我在Google文档中找到该解决方案的链接: https : //developer.android.com/training/keyboard-input/commands

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android:是否可以通过编程方式打开或关闭物理键盘? - Android: Is there any way to turn on-off the physical keyboard programmatically? 如何在Android中以编程方式禁用/启用物理键盘? - How do I programmatically disable/enable the physical keyboard in Android? 如何在 Android Studio 的模拟器上启用物理键盘? (曾经工作) - How enable physical keyboard on Emulator in Android Studio? (Used to work) 如何重新映射物理键盘,或将Android OS更改为另一种语言 - How to remap physical keyboard, or to change Android OS to another language 如何在 android 中更改(物理)键盘语言?(除了 SHIFT-SPACE) - How to change the (physical) keyboard language in android?(Besides SHIFT-SPACE) 无法通过 Android 仿真器物理键盘更改语言 - Cannot change language through the Android emulator physical keyboard 有没有办法以编程方式更改android中的键盘布局? - Is there a way to programmatically change the keyboard layout in android? 如何获取此对话框以编程方式更改键盘 - How to get this dialog to change the keyboard android programmatically 三星android 8和8.1-以编程方式更改键盘 - samsung android 8 and 8.1 - change keyboard programmatically Android:inputType none和物理键盘 - Android: inputType none and physical keyboard
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM