安卓键盘按键叠加问题

13

我想知道如何在按钮或按键按下时实现以下覆盖效果,如下图所示? 我正在实现自定义的键盘...希望能够实现这个效果。

enter image description here

编辑:

所以,我使用GridView实现了一个类似于下面的键盘。

enter image description here

现在,我正在尝试像默认键盘那样添加一些覆盖层(在单击时)。

谢谢 :)


可爱的问题,我想要尝试一些新的东西。 - Sonu Kumar
2个回答

6
你要找的是“按键预览”功能。我猜你正在使用KeyboardView创建自定义键盘。你可以通过调用setPreviewEnabled(boolean previewEnabled)启用按键预览,应该像这样:mKeyboardView.setPreviewEnabled(true); 编辑:
我认为这个链接将有助于你的实现,并更详细地解释了我所说的内容。
首先,你需要为键盘创建一个布局,通常只包含一个keyboardView:
<?xml version="1.0" encoding="UTF-8"?>
    <android.inputmethodservice.KeyboardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/keyboard"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:keyPreviewLayout ="@layout/preview" />

然后您为预览创建另一个布局:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:background="#ffff00"   
    android:textStyle="bold"
    android:textSize="30sp">    
</TextView>

之后,您可以为自己的键盘设计类似以下的外壳:
<Keyboard xmlns:android="http://schemas.android.com/apk/res/android"
    android:keyWidth="10%p"
    android:horizontalGap="0px"
    android:verticalGap="0px"  
    android:keyHeight="60dp">
    <Row>
        <Key android:codes="49" android:keyLabel="1" android:keyEdgeFlags="left"/>
        <Key android:codes="50" android:keyLabel="2"/>
        <Key android:codes="51" android:keyLabel="3"/>
    </Row>
    <Row>
        <Key android:codes="52" android:keyLabel="4"/>
        <Key android:codes="53" android:keyLabel="5"/>
        <Key android:codes="54" android:keyLabel="6"/>
   </Row>
   <Row>
        <Key android:codes="55" android:keyLabel="7"/>
        <Key android:codes="56" android:keyLabel="8"/>
        <Key android:codes="57" android:keyLabel="9"/>
   </Row>
</Keyboard>

最后,在您的Java代码中,您可以通过填充键盘视图或通过其ID获取它(如果它包含在片段或活动布局中)。然后将您设计的键盘设置到键盘视图中。

kv = (KeyboardView)getLayoutInflater().inflate(R.layout.keyboard, null);
keyboard = new Keyboard(this, R.xml.numeric);
kv.setKeyboard(keyboard);
kv.setOnKeyboardActionListener(this);

祝你好运。


我没有使用键盘视图。我使用视图创建了自己的小键盘。但是为了在按键时创造出更好的用户体验,我想实现一种类似于覆盖层的东西来处理普通视图的按键事件。谢谢。 - sagarpatidar
我不明白你使用视图的意思!你能详细说明一下你是如何创建自定义键盘的吗? 通常情况下,您需要创建一个带有行和键的XML键盘,一个用于键盘的XML布局,以及另一个用于预览的布局。 - MAB
MAB,添加了更多细节。 - sagarpatidar

1
使用PopupWindow实现按键预览功能(实际上我在我的上一个IME项目中也是这样做的)。
我建议您使用KeyboardView的子类,因为它提供了许多函数来使事情变得容易。每次按下key,都会回调onPressed,然后您就知道哪个被按下了。并且深入源代码KeyboardView,您可以学习从类中放置popupWindow的位置。
只需要一点努力。但是如果您的类没有派生自KeyboardView(例如使用GridView),您将不得不花更多时间来弄清楚。无论如何,源代码都是一个好的开始。

听起来不错,PopupWindow,我会试试这个。谢谢。 - sagarpatidar
请查看:http://stackoverflow.com/questions/9350654/popup-keyboard-issue-in-android-when-column-limits-are-specified - Sonu Kumar

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接