目录
1.场景:
项目中有定制的设备需要放在室外,用户使用时使用系统自带的键盘肯能没有那么方便,所以就需要使用到了自定义键盘
(结尾附上完整代码可以直接使用,不想看过程的可以直接跳过)
2.想法:
封装成一个比较通用的,当成一个View一样来使用
**实现自定义键盘思路:**
1. 在res包下创建xml目录,Keyboard标签来定义键盘布局
2. 创建IKeyboardView类并继承KeyboardView类,设置键盘布局(数字和字母)
3. 处理自定义键盘按键的点击事件以及预览,并实现数字和字母键盘(包括大小写)之间的切换
4. 绑定EditText,并且屏蔽系统键盘(实现点击切换绑定多个EditText)
5. 使用
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=