12

我使用 Android 键盘类创建了一个自定义键盘。

我想有两种模式的行。一种是普通模式。一种是当用户按下“Sym”按钮时。对于 XML 中“Sym”按钮的每个 Keyboard.Row,我指定了 android:keyboardMode="@+id/sym"。

现在,当我运行它时,只显示未指定 android:keyboardMode 的行。这是预期的,也是文档所说的。

我的问题是如何在我的代码中设置模式,以便呈现带有 android:keyboardMode="@+id/sym" 的行?

<Row>
    <Key android:codes="113"    android:keyLabel="q" />
    <Key android:codes="119"    android:keyLabel="w" />
    <Key android:codes="101"    android:keyLabel="e" />
    <Key android:codes="114"    android:keyLabel="r" />
    <Key android:codes="116"    android:keyLabel="t" />
    <Key android:codes="121"    android:keyLabel="y" />
    <Key android:codes="117"    android:keyLabel="u" />
    <Key android:codes="105"    android:keyLabel="i" />
    <Key android:codes="111"    android:keyLabel="o" />
    <Key android:codes="112"    android:keyLabel="p" />
    <Key android:codes="-5"    android:keyIcon="@drawable/keyboard_backspace_icon" android:keyWidth="91dp" />
</Row>
<Row android:keyboardMode="@+id/sym">
    <Key android:codes="113"    android:keyLabel="+" />
    <Key android:codes="119"    android:keyLabel="_" />
    <Key android:codes="101"    android:keyLabel="=" />
    <Key android:codes="114"    android:keyLabel="%" />
    <Key android:codes="116"    android:keyLabel="^" />
    <Key android:codes="121"    android:keyLabel="|" />
    <Key android:codes="117"    android:keyLabel="&lt;" />
    <Key android:codes="105"    android:keyLabel=">" />
    <Key android:codes="111"    android:keyLabel="[" />
    <Key android:codes="112"    android:keyLabel="]" />
    <Key android:codes="-5"    android:keyIcon="@drawable/keyboard_backspace_icon" android:keyWidth="91dp" />
</Row>
4

1 回答 1

21

好的,我自己想通了。

没有任何方法可以简单地切换键盘模式。您需要做的是创建 2 个不同的键盘并手动在它们之间切换。

这是更新后的 XML 的样子。请注意,您希望在两个关键字中使用的行不应包含标志 android:keyboardMode。

<!-- this is the row that shows in both modes -->
<Row android:keyWidth="51dp">
    <Key android:codes="49"    android:keyLabel="1"  />
    <Key android:codes="50"    android:keyLabel="2" />
    <Key android:codes="51"    android:keyLabel="3"  />
    <Key android:codes="52"    android:keyLabel="4"  />
    <Key android:codes="53"    android:keyLabel="5" />
    <Key android:codes="54"    android:keyLabel="6" />
    <Key android:codes="55"    android:keyLabel="7"  />
    <Key android:codes="56"    android:keyLabel="8" />
    <Key android:codes="57"    android:keyLabel="9" />
    <Key android:codes="48"    android:keyLabel="0"/>        
</Row>

<!-- this is the normal mode -->  
<Row  android:keyboardMode="@integer/keyboard_normal">
    <Key android:codes="113"    android:keyLabel="q" />
    <Key android:codes="119"    android:keyLabel="w" />
    <Key android:codes="101"    android:keyLabel="e" />
    <Key android:codes="114"    android:keyLabel="r" />
    <Key android:codes="116"    android:keyLabel="t" />
    <Key android:codes="121"    android:keyLabel="y" />
    <Key android:codes="117"    android:keyLabel="u" />
    <Key android:codes="105"    android:keyLabel="i" />
    <Key android:codes="111"    android:keyLabel="o" />
    <Key android:codes="112"    android:keyLabel="p" />
    <Key android:codes="-5"    android:keyIcon="@drawable/keyboard_backspace_icon" android:keyWidth="91dp" />
</Row>

<!-- this is the symbol mode-->
<Row  android:keyboardMode="@integer/keyboard_symbol">
    <Key android:codes="113"    android:keyLabel="+" />
    <Key android:codes="119"    android:keyLabel="_" />
    <Key android:codes="101"    android:keyLabel="=" />
    <Key android:codes="114"    android:keyLabel="%" />
    <Key android:codes="116"    android:keyLabel="^" />
    <Key android:codes="121"    android:keyLabel="|" />
    <Key android:codes="117"    android:keyLabel="&lt;" />
    <Key android:codes="105"    android:keyLabel=">" />
    <Key android:codes="111"    android:keyLabel="[" />
    <Key android:codes="112"    android:keyLabel="]" />
    <Key android:codes="-5"    android:keyIcon="@drawable/keyboard_backspace_icon" android:keyWidth="91dp" />
</Row>

然后创建一个 integer.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <integer name="keyboard_symbol">1</integer>
    <integer name="keyboard_normal">0</integer>
</resources>

这是相关的java代码。当您创建键盘对象时,您传递了keyboard_normal 或keyboard_symbol。

 normalKeyBoard = new Keyboard(activity, R.id.board, R.integer.keyboard_normal);
 symbolKeyBoard = new Keyboard(activity, R.id.board, R.integer.keyboard_symbol);

现在创建一个类变量来跟踪模式。默认值为 R.integer.keyboard_normal

private int mKeyboardState = R.integer.keyboard_normal;

现在在您的 onKeyboardActionListner().onKey 方法中放置代码以捕获切换模式的键(假设您已经在键盘中创建了一个)。

        if( primaryCode== Keyboard.KEYCODE_MODE_CHANGE) {
            if(mKeyboardView != null) {
                if(mKeyboardState == R.integer.keyboard_normal){
                    //change to symbol keyboard
                    if(symbolKeyBoard== null){
                        symbolKeyBoard = new Keyboard(mHostActivity, R.xml.hexkbd, R.integer.keyboard_symbol);
                    }

                    mKeyboardView.setKeyboard(symbolKeyBoard);
                    mKeyboardState = R.integer.keyboard_symbol;
                } else {
                    if(normalKeyBoard== null){
                        normalKeyBoard = new Keyboard(mHostActivity, R.xml.hexkbd, R.integer.keyboard_normal);
                    }

                    mKeyboardView.setKeyboard(normalKeyBoard);
                    mKeyboardState = R.integer.keyboard_normal;
                }
                //no shifting
                mKeyboardView.setShifted(false);
            }
        }
于 2013-04-23T16:47:10.660 回答