系统自带键盘和第三方的键盘不管是从性能还是从体验上来说都要胜于我们自己写的,但我们为什么还要去自定义键盘呢?其实就为了安全性,比如用户在输入账户密码,支付密码的时候,防止键盘获取到我们的数据;或者说美工要求Android的键盘需要和IOS的一样,那我们就得自己去写个键盘了。
效果图:
keyboardView.gif
一:键盘布局
XML布局属性
Keyboard.Key的属性.png
KeyboardView的属性.png
Keyboard.Row的属性.png
ASCII值.png
Key和Row为Keyboard的静态内部类,从上面的图中我们能看到每个属性对应的Description,在这里再简单介绍一下。
Key的xml属性
属性说明
android:codes
表示键Key的标签,字母/符号/数字对应的ASCII值。
android:iconPreview
表示键点击后放大后的View
android:isModifier
按键是否为功能键,例如Alt/Shift/Ctrl键。取值为true或false。
android:isRepeatable
表示长时间按下key键重复执行这个键的操作,如:长按删除键会一直执行删除操作。
android:isSticky
指定按键是否为状态键。如:Shift大小写切换按键,具有两种状态,按下状态和正常状态,取值为true或则false。
android:keyEdgeFlags
指定按键的对齐指令,取值为left或则right。
android:keyIcon
按键图标,如果指定了该值则文本属性无效。
android:keyLabel
代表按键显示的文本内容。
android:keyOutputText
指定按键输出的文本内容,取值为字符串。
android:popupCharacters
表示编辑特殊字符,空格、换行等.。
android:popupKeyboard
表示按键点击预览窗口。
android:horizontalGap
键的水平间隙,当前键的左边的水平间隙。
KeyboardView的xml属性
属性说明
android:background
设置整个键盘的背景色
android:keyPreviewHeight
正在输入时弹出预览框的高度
android:keyPreviewLayout
输入时预览框的布局样式,要求根布局为TextView。
android:keyTextColor
键的字体颜色
android:keyTextSize
键的字体大小
android:labelTextSize
字符串键文本字体大小
android:shadowColor、android:shadowRadius
设置这两个属性可以很好的解决键的字体发虚的问题,设置shadowColor值和键的字体颜色相同。
android:keyBackground
设置键的背景色,可以用drawable中的selector标签设置键的正常状态样式和按下样式
英文键盘
android:horizontalGap="0.0px"
android:keyHeight="7.5%p"
android:keyWidth="8%p"
android:verticalGap="0.0px">
android:codes="113"
android:horizontalGap="1%p"
android:keyEdgeFlags="left"
android:keyLabel="q"
android:keyWidth="8%p"/>
android:codes="119"
android:horizontalGap="2%p"
android:keyLabel="w"
android:keyWidth="8%p"/>
android:codes="101"
android:horizontalGap="2%p"
android:keyLabel="e"
android:keyWidth="8%p"/>
android:codes="114"
android:horizontalGap="2%p"
android:keyLabel="r"
android:keyWidth="8%p"/>
android:codes="116"
android:horizontalGap="2%p"
android:keyLabel="t"
android:keyWidth="8%p"/>
android:codes="121"
android:horizontalGap="2%p"
android:keyLabel="y"
android:keyWidth="8%p"/>
android:codes="117"
android:horizontalGap="2%p"
android:keyLabel="u"
android:keyWidth="8%p"/>
android:codes="105"
android:horizontalGap="2%p"
android:keyLabel="i"
android:keyWidth="8%p"/>
android:codes="111"
android:horizontalGap="2%p"
android:keyLabel="o"
android:keyWidth="8%p"/>
android:codes="112"
android:horizontalGap="2%p"
android:keyEdgeFlags="right"
android:keyLabel="p"
android:keyWidth="8%p"/>
android:codes="97"
android:horizontalGap="6%p"
android:keyEdgeFlags="left"
android:keyLabel="a"
android:keyWidth="8%p"/>
android:codes="115"
android:horizontalGap="2%p"
android:keyLabel="s"
android:keyWidth="8%p"/>
android:codes="100"
android:horizontalGap="2%p"
android:keyLabel="d"
android:keyWidth="8%p"/>
android:codes="102"
android:horizontalGap="2%p"
android:keyLabel="f"
android:keyWidth="8%p"/>
android:codes="103"
android: