我使用Android键盘类创建了自定义键盘。
我希望有两种模式的行。一种是正常模式。一个是当用户按下“Sym”按钮时。对于每个Keyboard.Row对于XML中的“Sym”按钮,我指定了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="<" />
<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>
答案 0 :(得分:18)
好吧我自己想出来了。
没有办法简单地切换键盘模式。您需要做的是创建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="<" />
<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对象时,您将传递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);
}
}