Android自定义键盘(KeyboardView)

本文介绍了如何在Android中实现自定义键盘,包括创建XML布局定义键盘、封装KeyboardView、处理按键事件、切换键盘布局以及与EditText交互。详细步骤涵盖了从布局设计到事件处理的全过程,提供了一个通用的自定义键盘组件的实现方法。
摘要由CSDN通过智能技术生成

1.场景:

项目中有定制的设备需要放在室外,用户使用时使用系统自带的键盘肯能没有那么方便,所以就需要使用到了自定义键盘

(结尾附上完整代码可以直接使用,不想看过程的可以直接跳过)

2.想法:

封装成一个比较通用的,当成一个View一样来使用


**实现自定义键盘思路:**
 1. 在res包下创建xml目录,Keyboard标签来定义键盘布局
 2. 创建IKeyboardView类并继承KeyboardView,设置键盘布局(数字和字母)
 3. 处理自定义键盘按键的点击事件以及预览,并实现数字和字母键盘(包括大小写)之间的切换
 4. 绑定EditText,并且屏蔽系统键盘(实现点击切换绑定多个EditText5. 使用

3.开始实现:

《一》 在res包下创建xml目录,Keyboard标签来定义键盘布局:

创建keyboard_num.xml文件(数字键盘布局)

<?xml version="1.0" encoding="utf-8"?>
<Keyboard xmlns:android="http://schemas.android.com/apk/res/android"
    android:keyWidth="10%p"
    android:keyHeight="7%p"
    android:horizontalGap="0.0px"
    android:verticalGap="0.0px">
    <Row android:verticalGap="1%p">
        <Key
            android:codes="113"
            android:keyLabel="q"
            android:horizontalGap="1.81%p">

        </Key>
        <Key
            android:codes="119"
            android:keyLabel="w"
            android:horizontalGap="1.81%p">

        </Key>
        <Key
            android:codes="101"
            android:keyLabel="e"
            android:horizontalGap="1.81%p">

        </Key>
        <Key
            android:codes="114"
            android:keyLabel="r"
            android:horizontalGap="1.81%p">

        </Key>
        <Key
            android:codes="116"
            android:keyLabel="t"
            android:horizontalGap="1.81%p">

        </Key>
        <Key
            android:codes="121"
            android:keyLabel="y"
            android:horizontalGap="1.81%p">

        </Key>
        <Key
            android:codes="117"
            android:keyLabel="y"
            android:horizontalGap="1.81%p">

        </Key>
        <Key
            android:codes="105"
            android:keyLabel="i"
            android:horizontalGap="1.81%p">

        </Key>
        <Key
            android:codes="111"
            android:keyLabel="o"
            android:horizontalGap="1.81%p">

        </Key>
        <Key
            android:codes="112"
            android:keyLabel="p"
            android:horizontalGap="1.81%p">

        </Key>
    </Row>
    <Row android:verticalGap="1%p">
        <Key
            android:codes="97"
            android:keyLabel="a"
            android:keyWidth="9%p"
            android:horizontalGap="5.5%p">

        </Key>
        <Key
            android:codes="115"
            android:keyLabel="s"
            android:keyWidth="9%p"
            android:horizontalGap="1%p">

        </Key>
        <Key
            android:codes="100"
            android:keyLabel="d"
            android:keyWidth="9%p"
            android:horizontalGap="1%p">

        </Key>
        <Key
            android:codes="102"
            android:keyLabel="f"
            android:keyWidth="9%p"
            android:horizontalGap="1%p">

        </Key>
        <Key
            android:codes="103"
            android:keyLabel="g"
            android:keyWidth="9%p"
            android:horizontalGap="1%p">

        </Key>
        <Key
            android:codes="104"
            android:keyLabel="h"
            android:keyWidth="9%p"
            android:horizontalGap="1%p">

        </Key>
        <Key
            android:codes="106"
            android:keyLabel="j"
            android:keyWidth="9%p"
            android:horizontalGap="1%p">

        </Key>
        <Key
            android:codes="107"
            android:keyLabel="k"
            android:keyWidth="9%p"
            android:horizontalGap="1%p">

        </Key>
        <Key
            android:codes="108"
            android:keyLabel="l"
            android:keyWidth="9%p"
            android:horizontalGap="1%p">

        </Key>
    </Row>
    <Row android:verticalGap="1%p">
        <Key
            android:codes="-1"
            android:keyLabel="大写"
            android:keyWidth="17%p"
            android:horizontalGap=
Android 中,自定义键盘需要实现一个继承自 InputMethodService 的服务,这个服务会在用户打开软键盘时被调用。下面是一些步骤来创建自定义键盘: 1. 创建一个新的 Android 项目,并在 AndroidManifest.xml 文件中声明一个新的服务: ```xml <service android:name=".CustomKeyboard" android:label="Custom Keyboard" android:permission="android.permission.BIND_INPUT_METHOD"> <meta-data android:name="android.view.im" android:resource="@xml/method" /> </service> ``` 上面的代码声明了一个名为 CustomKeyboard 的服务,并将其与 android.view.im 绑定。在 res/xml 目录下创建一个名为 method.xml 的文件,用于指定 CustomKeyboard 的布局和行为: ```xml <?xml version="1.0" encoding="utf-8"?> <input-method xmlns:android="http://schemas.android.com/apk/res/android" android:settingsActivity=".SettingsActivity" android:imeSubtypeLocale="en_US" android:imeSubtypeMode="keyboard" > </input-method> ``` 上面的代码指定了键盘的设置活动、语言环境和子类型模式。 2. 创建 CustomKeyboard 类,并继承 InputMethodService。在这个类中,你需要重写一些回调方法,例如 onCreateInputView()、onKeyDown() 和 onStartInputView() 等。这些方法将决定键盘的外观和行为。 ```java public class CustomKeyboard extends InputMethodService implements KeyboardView.OnKeyboardActionListener { private KeyboardView keyboardView; private Keyboard keyboard; @Override public View onCreateInputView() { keyboardView = (KeyboardView) getLayoutInflater().inflate(R.layout.keyboard, null); keyboard = new Keyboard(this, R.xml.qwerty); keyboardView.setKeyboard(keyboard); keyboardView.setOnKeyboardActionListener(this); return keyboardView; } @Override public void onStartInputView(EditorInfo info, boolean restarting) { super.onStartInputView(info, restarting); keyboardView.setPreviewEnabled(false); } @Override public void onKey(int primaryCode, int[] keyCodes) { InputConnection ic = getCurrentInputConnection(); switch (primaryCode) { case Keyboard.KEYCODE_DELETE: ic.deleteSurroundingText(1, 0); break; case Keyboard.KEYCODE_SHIFT: // do something break; default: char c = (char) primaryCode; ic.commitText(String.valueOf(c), 1); } } } ``` 上面的代码创建了一个名为 CustomKeyboard 的类,并在 onCreateInputView() 方法中设置了键盘的布局和行为。在 onStartInputView() 方法中,我们禁用了键盘预览功能。在 onKey() 方法中,我们检查按下的键码并执行相应的操作。 3. 创建键盘布局。在 res/xml 目录下创建一个名为 qwerty.xml 的文件,用于指定键盘布局: ```xml <?xml version="1.0" encoding="utf-8"?> <Keyboard xmlns:android="http://schemas.android.com/apk/res/android" android:keyWidth="10%p" android:keyHeight="60dp" android:horizontalGap="0px" android:verticalGap="0px" android:keyEdgeFlags="left"> <Row> <Key android:keyLabel="q" android:keyEdgeFlags="left"/> <Key android:keyLabel="w"/> <Key android:keyLabel="e"/> <Key android:keyLabel="r"/> <Key android:keyLabel="t"/> <Key android:keyLabel="y"/> <Key android:keyLabel="u"/> <Key android:keyLabel="i"/> <Key android:keyLabel="o"/> <Key android:keyLabel="p" android:keyEdgeFlags="right"/> </Row> <Row> <Key android:keyLabel="a" android:keyEdgeFlags="left"/> <Key android:keyLabel="s"/> <Key android:keyLabel="d"/> <Key android:keyLabel="f"/> <Key android:keyLabel="g"/> <Key android:keyLabel="h"/> <Key android:keyLabel="j"/> <Key android:keyLabel="k"/> <Key android:keyLabel="l android:keyEdgeFlags="right"/> </Row> <Row> <Key android:keyLabel="shift" android:horizontalGap="10%p" android:keyWidth="20%p" android:keyEdgeFlags="left" android:isModifier="true" android:isSticky="true"/> <Key android:keyLabel="z"/> <Key android:keyLabel=""/> <Key android:keyLabel="c"/> <Key android:keyLabel="v"/> <Key android:keyLabel="b"/> <Key android:keyLabel="n"/> <Key android:keyLabel="m"/> <Key android:keyLabel="delete" android:keyWidth="20%p" android:keyEdgeFlags="right" android:icon="@drawable/ic_delete"/> </Row> <Row> <Key android:keyLabel="123" android:keyEdgeFlags="left" android:keyWidth="20%p"/> <Key android:keyLabel=" " android:keyWidth="40%p"/> <Key android:keyLabel="return" android:keyWidth="20%p" android:keyEdgeFlags="right"/> </Row> </Keyboard> ``` 上面的代码指定了一个基本的 QWERTY 键盘布局,包含字母、数字和删除键。 4. 运行应用程序并测试自定义键盘。在测试键盘时,你需要在 Android 设备的输入法设置中激活你的自定义键盘。 以上就是创建自定义键盘的基本步骤,你可以根据需要修改键盘的布局和行为。
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值