我想知道如何在按钮或按键按下时实现以下覆盖效果,如下图所示? 我正在实现自定义的键盘...希望能够实现这个效果。
编辑:
所以,我使用GridView实现了一个类似于下面的键盘。
现在,我正在尝试像默认键盘那样添加一些覆盖层(在单击时)。
谢谢 :)
KeyboardView
创建自定义键盘。你可以通过调用setPreviewEnabled(boolean previewEnabled)启用按键预览,应该像这样:mKeyboardView.setPreviewEnabled(true);
编辑:<?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);
祝你好运。
PopupWindow
实现按键预览功能(实际上我在我的上一个IME项目中也是这样做的)。KeyboardView
的子类,因为它提供了许多函数来使事情变得容易。每次按下key
,都会回调onPressed
,然后您就知道哪个被按下了。并且深入源代码KeyboardView
,您可以学习从类中放置popupWindow
的位置。KeyboardView
(例如使用GridView
),您将不得不花更多时间来弄清楚。无论如何,源代码都是一个好的开始。