4

我最近遇到了 Android 键盘视图的问题。

我想要实现的是一个不错的键盘,我可以在其中添加以像素为单位的键间距,其余空间将由键共享,具体取决于我对它们的加权方式。

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

4

1 回答 1

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="◐"等等。

我花了一些时间寻找打开 IME 菜单的方法,所以这里是: 如何在 Android 中设置/调用新的输入法

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

于 2013-02-05T03:31:55.157 回答