2013-02-05 18 views
4

我最近遇到了Android键盘视图的问题。Android KeyboardView无法正常调整

我想要实现的是一个体面的键盘,我可以在其中添加像素的键距,其余空间将由键共享,具体取决于我如何加权。

我还发现百分比宽度不准确(行结尾变化了+ -6px)。

+0

我试图把键盘在一个正方形视图上屏幕右侧横向模式但经过。一些布局改变,它弄乱了关键尺寸,使它们在widt中变得巨大H。这是因为它假设将适合整个屏幕水平。我试图强制invalidate(),invalidateAllKeys()和requestLayout()并没有成功。你有什么想法? – WindRider

+0

自从我做了这个哈哈以来,这已经过去了...所以键盘的第一个布局很好,但后来的不是?第一个和第二个布局之间的哪些变化导致第二个布局失败? – AStupidNoob

+0

哦,现在我记得我(大约)在一年前解决了这个问题(6px),百分比大小如25%p,50%p,但是导致了其他问题(上述)。我现在正在试验你的解决方案。我的情况更简单 - 我需要一个带有一些双倍大小按钮的方形数字键盘。感觉很高兴看到有人遇到同样可怕的问题。大声笑:)我会再写信告诉结果。 – WindRider

回答

2

每个键都有一个宽度,通常是64像素。对于一个半正常宽度的键我会分配32px。对于双倍我给它128px。等等

键盘然后通过称为“fixKeyboard”的函数运行,该函数决定每个键的宽度。你可以告诉它每行有多少个64px宽度的按键,它会将按键缩放到适合的大小。 (即keysPerStandardRow ==键盘多少像素宽是/ 64PX)

private void fixKeyboard(Keyboard k, int keysPerStandardRow) 
{ 
    List<Key> keys = k.getKeys(); 
    int dw = GlobalHelperFunctions.getDisplay(this).getWidth(); 
    int ttly = 0; 
    int divisor = 64 * keysPerStandardRow; 
    int ttl_weights = 0; 
    for (Key key : keys) 
    { 
     //See below for the deal with 424242 
     int weight = key.width + (key.gap == 424242 ? 0 : key.gap); 
     if (key.gap == 424242) 
      key.gap = 0; 
     else 
      key.gap = (ttl_weights + key.gap) * dw/divisor - ttl_weights * dw/divisor; 
     key.width = (ttl_weights + key.width) * dw/divisor - ttl_weights * dw/divisor; 
     if (key.y != ttly) 
     { 
      ttl_weights = 0; 
      ttly = key.y; 
     } 
     key.x = ttl_weights * dw/divisor; 
     ttl_weights += weight; 
    } 
} 

... 

static public Display GlobalHelperFunctions.getDisplay(Context c) 
{ 
    if (c != null) 
    { 
     WindowManager wm = (WindowManager)c.getSystemService(Context.WINDOW_SERVICE); 
     if (wm != null) 
     { 
      return wm.getDefaultDisplay(); 
     } 
    } 
    return null; 
} 

唉,这不是故事的结束。当你把它交给XML文件的时候,键盘视图/键盘似乎决定了键盘的宽度。如果由于某种原因,你已经改变了我的键,android会将它们缩放到其预先计算的盒子中。显然,我不想要这个。所以我就这样做了:我只是通过在每个键(424242px)之间定义一个巨大的横向间隙来强制它具有最大可能的宽度,然后在运行fixKeyboard时将其重置为0。

由于我使用这种方法,您可以通过简单地不使用触发器号码来轻松定义间隙!这里是我的QWERTY键盘,你可以使用,如果你想作为一个例子:

<?xml version="1.0" encoding="utf-8"?> 
<Keyboard xmlns:android="http://schemas.android.com/apk/res/android" 
     android:keyHeight="80px" 
     android:horizontalGap="424242px" 
     android:verticalGap="2px" > 
    <Row android:keyWidth="64px"> 
     <Key android:keyLabel="1" android:keycode="KEYCODE_1"/> 
     <Key android:keyLabel="2" android:keycode="KEYCODE_2"/> 
     <Key android:keyLabel="3" android:keycode="KEYCODE_3"/> 
     <Key android:keyLabel="4" android:keycode="KEYCODE_4"/> 

     <Key android:keyLabel="5" android:keycode="KEYCODE_5"/> 
     <Key android:keyLabel="6" android:keycode="KEYCODE_6"/> 
     <Key android:keyLabel="7" android:keycode="KEYCODE_7"/> 
     <Key android:keyLabel="8" android:keycode="KEYCODE_8"/> 

     <Key android:keyLabel="9" android:keycode="KEYCODE_9" /> 
     <Key android:keyLabel="0" android:keycode="KEYCODE_0"/> 
     <Key android:keyIcon="@android:drawable/ic_input_delete" android:keyOutputText="◁" android:keyWidth="128px" android:codes="0x25C1" android:isRepeatable="true"/> 
    </Row> 
    <Row android:keyWidth="64px"> 
     <Key android:keyLabel="q" android:keycode="KEYCODE_Q" /> 
     <Key android:keyLabel="w" android:keycode="KEYCODE_W"/> 
     <Key android:keyLabel="e" android:keycode="KEYCODE_E" /> 
     <Key android:keyLabel="r" android:keycode="KEYCODE_R" /> 

     <Key android:keyLabel="t" android:keycode="KEYCODE_T"/> 
     <Key android:keyLabel="y" android:keycode="KEYCODE_Y" /> 
     <Key android:keyLabel="u" android:keycode="KEYCODE_U"/> 
     <Key android:keyLabel="i" android:keycode="KEYCODE_I" /> 

     <Key android:keyLabel="o" android:keycode="KEYCODE_O" /> 
     <Key android:keyLabel="p" android:keycode="KEYCODE_P" /> 
     <Key android:keyLabel="・"/> 
     <Key android:keyIcon="@android:drawable/ic_menu_preferences" android:keyOutputText="●" android:codes="0x25CF"/> 
    </Row> 
    <Row android:keyWidth="64px"> 
     <Key android:keyLabel=" " android:keyHeight="0px" android:keyWidth="0px" android:horizontalGap="32px"/> 
     <Key android:keyLabel="a" android:keycode="KEYCODE_A"/> 
     <Key android:keyLabel="s" android:keycode="KEYCODE_S"/> 
     <Key android:keyLabel="d" android:keycode="KEYCODE_D"/> 

     <Key android:keyLabel="f" android:keycode="KEYCODE_F" /> 
     <Key android:keyLabel="g" android:keycode="KEYCODE_G"/> 
     <Key android:keyLabel="h" android:keycode="KEYCODE_H"/> 
     <Key android:keyLabel="j" android:keycode="KEYCODE_J" /> 

     <Key android:keyLabel="k" android:keycode="KEYCODE_K" /> 
     <Key android:keyLabel="l" android:keycode="KEYCODE_L" /> 
     <Key android:keyLabel="ENTER" android:keyOutputText="◒" android:keyWidth="96px"/> 
     <Key android:keyLabel="カタ" android:keyOutputText="◎" android:codes="0x25CE"/> 
    </Row> 
    <Row android:keyWidth="64px"> 
     <Key android:keyLabel="「" /> 
     <Key android:keyLabel="z" android:keycode="KEYCODE_Z"/> 
     <Key android:keyLabel="x" android:keycode="KEYCODE_X"/> 
     <Key android:keyLabel="c" android:keycode="KEYCODE_C"/> 

     <Key android:keyLabel="v" android:keycode="KEYCODE_V" /> 
     <Key android:keyLabel="b" android:keycode="KEYCODE_B"/> 
     <Key android:keyLabel="n" android:keycode="KEYCODE_N"/> 
     <Key android:keyLabel="m" android:keycode="KEYCODE_M"/> 

     <Key android:keyLabel="、"/> 
     <Key android:keyLabel="。"/> 
     <Key android:keyLabel="⇧" android:keycode="KEYCODE_SHIFT_RIGHT" android:isModifier="true"/> 
     <Key android:keyLabel="HW" android:keyOutputText="◍" android:codes="0x25CD"/> 
    </Row> 
    <Row android:keyWidth="64px"> 
     <Key android:keyLabel=" " android:keyWidth="704px"/> 
     <Key android:keyLabel="ひら" android:keyOutputText="◐" android:codes="0x25D0"/> 
    </Row> 
</Keyboard> 

一些重要的事情:做一个缺口前的关键,我只是增加了一个0宽度关键。 <Key android:keyLabel=" " android:keyHeight="0px" android:keyWidth="0px" android:horizontalGap="32px"/>

另一件事:对于控制键(像打开的IME菜单中的一个,我已经使用特殊字符,以提醒代码:android:keyOutputText="◐"

我花了一段时间来搜索方式打开输入法菜单,所以这里的如何: How to set/call an new input method in Android

HTH,所有的代码是公共领域

相关问题