Android自定义键盘方向符号/ qwerty

时间:2015-05-06 22:40:27

标签: android android-softkeyboard sample

我使用android 示例软键盘作为学习项目。我已经添加了第二种语言,到目前为止一切正常。然而,有一个问题,我发现很难解决。在改变设备从纵向到横向的方向时,我希望无论键盘布局如何都能保持不变。逻辑是:

 // on orientation or configuration change
if (currentKeyboard==mQwertyKeyboard) {
          // keep as is
            mCurKeyboard = mQwertyKeyboard ;   
 }else if (currentKeyboard == mSymbolsKeyboard) {
          // keep as is
            mCurKeyboard = mSymbolsKeyboard;
}else if (currentKeyboard == mSymbolsShiftedKeyboard) {
          // keep as is
            mCurKeyboard = mSymbolsShiftedKeyboard ; 
 }

我试图在以下方面实现这一点:

@Override
public void onConfigurationChanged(Configuration newConfig) {...}

在Softkeyboard.java中,但没有结果。 提前谢谢。

1 个答案:

答案 0 :(得分:0)

我遇到了同样的问题 - 我使用onConfigurationChanged(Configuration newConfig)和一些侧面编码

解决了这个问题

如果删除它,我的代码会改变方向,但我的键盘不会调整大小。由于我喜欢父级的重新调整大小功能,我创建了一个包含所有键盘的数组,在更改方向后,我会更新数组,以便键盘阵列具有正确的大小。

@Override
public void onConfigurationChanged(Configuration newConfig) {

    int currentKeyboard = 0;
    boolean isShifted = kv.isShifted();
    for (int i = 0; i < keyboard.length; i++) {
        if(kv.getKeyboard().equals(keyboard[i])){
            currentKeyboard = i;
            break;
        }
    }

    super.onConfigurationChanged(newConfig);

    initializeKeyboardArray();
    setKeyboard(keyboard[currentKeyboard]);
    kv.setShifted(isShifted);

}

private void initializeKeyboardArray(){
    keyboard = new Keyboard[7];
    keyboard[ARABIC] = arabic;
    keyboard[ARABIC_SHIFT] = arabicShift;
    keyboard[ARABIC_SYMBOLS] = arabicSymbols;
    keyboard[ARABIC_SYMBOLS_SHIFT] = arabicSymbolsShift;
    keyboard[QWERTY] = qwerty;
    keyboard[QWERTY_SYMBOLS] = qwertySymbols;
    keyboard[QWERTY_SYMBOLS_SHIFT] = qwertySymbolsShift;
}