【发布时间】:2015-05-23 20:19:54
【问题描述】:
我很好奇如何在某些按钮或按键上实现以下覆盖,如下图所示?我正在实现自定义键盘...同样需要这个东西。
编辑:
所以,我使用 Gridview 实现了一个键盘,如下所示。
现在,我正在尝试在默认键盘中添加一些覆盖(点击时)。
谢谢:)
【问题讨论】:
-
可爱的问题我想要一些新的东西
我很好奇如何在某些按钮或按键上实现以下覆盖,如下图所示?我正在实现自定义键盘...同样需要这个东西。
编辑:
所以,我使用 Gridview 实现了一个键盘,如下所示。
现在,我正在尝试在默认键盘中添加一些覆盖(点击时)。
谢谢:)
【问题讨论】:
您正在寻找的是“关键预览”
我假设您正在使用KeyboardView 创建您的自定义键盘。您可以通过调用setPreviewEnabled(boolean previewEnabled) 来启用密钥预览,它应该是这样的:mKeyboardView.setPreviewEnabled(true);
编辑:
我认为link 将在您的实施中为您提供帮助,并更详细地解释我的尝试。
首先你为键盘创建一个布局,一般它只包含一个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 代码中,您可以扩展您的 keyboardView,或者如果它包含在片段或活动布局中,则通过其 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),你将不得不花更多的时间来弄清楚。无论如何,源代码是一个好的开始。
【讨论】: