如何在Android软键盘中添加切换语言功能? [英] How to add switch language functionality in the Android soft keyboard?

查看:275
本文介绍了如何在Android软键盘中添加切换语言功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Android软键盘应用程序当前具有英语,并且我正在对其进行修改以向其中添加另一种语言.我几乎已经完成了新语言的布局并手动添加了字母,因为该语言尚未包含在Android中.新语言还具有与SHIFT键一起显示的键.我正在努力解决两种语言之间的切换功能:英语和新增语言.

The Android Soft Keyboard application currently has English language, and I am modifying it to add another language into it. I am almost done with layout of new language and adding alphabets manually as the language is not included in the Android yet. The new language also has keys which appears with SHIFT key. I am struggling to fix the switching functionality between two languages: English and new added.

我可以通过硬编码解决此问题:使用按钮更改布局(xml),然后反之亦然,但是我知道这不是正确的方法,因为有switch功能.

I can fix this by hard coding: to change the layout (xml) with button and then do again vice versa, but I know this is not the right approach as there is switch functionality.

我正在提供相关代码.如果需要更多代码,请发表评论.

I am providing the related code. Please comment if need more code to provide.

public void onKey(int primaryCode, int[] keyCodes) {
    if (isWordSeparator(primaryCode)) {
        // Handle separator
        if (mComposing.length() > 0) {
            commitTyped(getCurrentInputConnection());
        }
        sendKey(primaryCode);
        updateShiftKeyState(getCurrentInputEditorInfo());
    } else if (primaryCode == Keyboard.KEYCODE_DELETE) {
        handleBackspace();
    } else if (primaryCode == Keyboard.KEYCODE_SHIFT) {
        handleShift();
    } else if (primaryCode == Keyboard.KEYCODE_CANCEL) {
        handleClose();
        return;
    } else if (primaryCode == LatinKeyboardView.KEYCODE_LANGUAGE_SWITCH) {
        handleLanguageSwitch();
        return;
    } else if (primaryCode == LatinKeyboardView.KEYCODE_OPTIONS) {
        // Show a menu or something'
    } else if (primaryCode == Keyboard.KEYCODE_MODE_CHANGE
            && mInputView != null) {
        Keyboard current = mInputView.getKeyboard();
        if (current == mSymbolsKeyboard || current == mSymbolsShiftedKeyboard) {
            setLatinKeyboard(mQwertyKeyboard);
        } else {
            setLatinKeyboard(mSymbolsKeyboard);
            mSymbolsKeyboard.setShifted(false);
        }
    }
}

该应用程序基于以下样本.

推荐答案

我终于找到了解决方案.我在上述代码(问题中的代码)之后添加了以下代码,以在两种语言之间切换:

I finally figured out the solution. I added the following code right after the above code (code in the question) to switch between languages:

else if (primaryCode == 10000) {
    Keyboard current = mInputView.getKeyboard();
    current = mQwertyNewKeyboard;
    mInputView.setKeyboard(current);

//Switch to qwerty (English Main)
}else if (primaryCode == 10001) {
    Keyboard current = mInputView.getKeyboard();
    current = mQwertyKeyboard;
    mInputView.setKeyboard(current);

//Switch to qwertyNewShift
}else if (primaryCode == 10002) {
    Keyboard current = mInputView.getKeyboard();
    current = mQwertyNewKeyboardShift;
    mInputView.setKeyboard(current);
}

在每种语言的布局(xml)文件中,我创建了一个切换按钮,并相应地设置了primaryCode.

And in the layout (xml) file of each language, I created a switch button and set the primaryCode accordingly.

<Key android:codes="10001" android:keyIcon="@drawable/sym_keyboard_language_switch" android:keyWidth="10%p"/>

这篇关于如何在Android软键盘中添加切换语言功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆