弹出自定义键盘中按下的键 [英] Make popup of the key pressed in a customized keyboard

查看:106
本文介绍了弹出自定义键盘中按下的键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Keyboardview拥有自己的键盘.

I'm using keyboardview to have my own keyboard.

效果很好,但是我不能像安卓键盘那样放大按键的效果

Works fine, but I cannot do the effect of enlarging the key pressed, as does the Android keyboard

这些是使用的部分

<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.但是没有成功.

I've tried with onKey method, also from the XML. But without success.

推荐答案

仅显示放大的预览,默认情况下KeyboardView应该这样做.您不想设置popupKeyboard值,因为这是长时间按下时会显示的特殊迷你键盘.

For just showing the enlarged previews, KeyboardView should be doing it by default. You don't want to set a popupKeyboard value as this is for a special mini keyboard that shows on long presses.

我假设您正在遵循本教程.请注意第3.3节中的这些行:

I assume you were following this tutorial. Take note of these lines in section 3.3:

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

相反,将其设置为true.

完整解决方案

在活动的布局中:

<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.

The important properties here are keyPreviewLayout, keyPreviewOffset and keyPreviewHeight.

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>

在您的活动代码中

    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"键:

您可能还会发现检出

You may also find it useful to check out the KeyboardView source code.

这篇关于弹出自定义键盘中按下的键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆