在Android自定义键盘中设置键盘模式

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

我想要有两种模式的行。 一个是正​​常模式。 一个是当用户按下“Sym”button时。 对于每个Keyboard.Row XML中的“Sym”button,我已经指定了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> 

Solutions Collecting From Web of "在Android自定义键盘中设置键盘模式"

好吧自己想清楚了。

没有办法简单地切换键盘模式。 你必须做的是创build2个不同的键盘,并手动切换它们。

这是更新的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> 

然后创build一个integer.xml

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

这里是相关的java代码。 当您创build键盘对象时,您将传递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); 

现在创build一个类variables来跟踪模式。 默认值是R.integer.keyboard_normal

 private int mKeyboardState = R.integer.keyboard_normal; 

现在在onKeyboardActionListner()。onKey方法中,将代码捕获切换模式的键(假设您已经在键盘上创build了一个键)。

  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); } }