gpt4 book ai didi

android - 弹出自定义键盘中按下的键

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:02:47 39 4
gpt4 key购买 nike

我正在使用 keyboardview 来拥有自己的键盘。

工作正常,但我无法像 Android 键盘那样实现放大按键的效果

zoom image

这些是使用的部分

<android.inputmethodservice.KeyboardView
android:id="@+id/keyboardview"
style="@style/Widget.KeyboardView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:focusable="true"
android:focusableInTouchMode="true"
android:shadowRadius="0"
android:visibility="visible" />

.

   <style name="Widget.KeyboardView" parent="android:Widget.KeyboardView">
<item name="android:background">@drawable/naranja</item>
<item name="android:keyBackground">@drawable/naranja</item>
<item name="android:keyTextSize">40sp</item>
<item name="android:keyTextColor">#d35400</item>
<item name="android:keyPreviewLayout">@layout/keyboard_popup_keyboard</item>
<item name="android:keyPreviewOffset">12dp</item>
<item name="android:keyPreviewHeight">80dp</item>
<item name="android:popupLayout">@layout/keyboard_popup_keyboard</item>
</style>

.

XML/弹出窗口:

   <?xml version="1.0" encoding="utf-8"?>
<Keyboard xmlns:android="http://schemas.android.com/apk/res/android"
android:keyWidth="10%p"
android:keyHeight="10%p">
</Keyboard>

     <Key
android:codes="81"
android:keyEdgeFlags="left"
android:keyLabel="Q"
android:popupCharacters="Q"
android:popupKeyboard="@xml/popup" />

etc...

我尝试过使用 onKey 方法,同样来自 XML。但没有成功。

最佳答案

为了仅显示放大的预览,KeyboardView 应该默认执行此操作。您不想设置 popupKeyboard 值,因为这是用于在长按时显示的特殊迷你键盘。

我假设您正在关注 this tutorial .请注意第 3.3 节中的这些行:

// Do not show the preview balloons
mKeyboardView.setPreviewEnabled(false);

而是将其设置为 true

完整的解决方案

在您的 Activity 布局中:

<android.inputmethodservice.KeyboardView
android:id="@+id/keyboardview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:focusable="true"
android:focusableInTouchMode="true"
android:keyPreviewOffset="12dp"
android:keyPreviewLayout="@layout/kbpreview"
android:visibility="visible" />

这里的重要属性是keyPreviewLayoutkeyPreviewOffsetkeyPreviewHeight

layout/kbpreview.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:gb="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:background="@color/red"
android:textSize="30dp" />

xml/kb.xml

<?xml version="1.0" encoding="utf-8"?>
<Keyboard xmlns:android="http://schemas.android.com/apk/res/android"
android:keyWidth="12.50%p"
android:keyHeight="10%p" >

<Row>
<Key android:codes="55" android:keyLabel="7" android:keyEdgeFlags="left" />
<Key android:codes="56" android:keyLabel="8" />
<Key android:codes="57" android:keyLabel="9" />
<Key android:codes="65" android:keyLabel="A" android:horizontalGap="6.25%p" />
<Key android:codes="66" android:keyLabel="B" />
<Key android:codes="55006" android:keyLabel="CLR" android:keyEdgeFlags="right"/>
</Row>

<!-- and whatever else... -->

</Keyboard>

在您的 Activity 代码中

    Keyboard mKeyboard = new Keyboard(this, R.xml.kb);

// Lookup the KeyboardView
KeyboardView mKeyboardView = (KeyboardView) findViewById(R.id.keyboardview);

// Attach the keyboard to the view
mKeyboardView.setKeyboard(mKeyboard);

// Key listener required
mKeyboardView.setOnKeyboardActionListener(myListener);

结果

按下“5”键:

enter image description here

您可能还会发现查看 KeyboardView source code 很有用.

关于android - 弹出自定义键盘中按下的键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25911409/

39 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com