自定义数字键盘
1.键盘布局
horizontalGap:按键间的水平间隔
keyHeight:按键高度以%或者%p结尾
keyWidth:按键宽度,”33.33333%p”
verticalGap:按键间的垂直间隔
codes:可以是系统给的固定值也可以是自定义的值
android:horizontalGap="1dp"
android:keyHeight="8%p"
android:keyWidth="33.33333%p"
android:verticalGap="1dp">
android:codes="49"
android:keyLabel="1">
android:codes="50"
android:keyLabel="2">
android:codes="51"
android:keyLabel="3">
android:codes="52"
android:keyLabel="4">
android:codes="53"
android:keyLabel="5">
android:codes="54"
android:keyLabel="6">
android:codes="53"
android:keyLabel="7">
android:codes="54"
android:keyLabel="8">
android:codes="55"
android:keyLabel="9">
android:codes="-10"
android:keyLabel="">
android:codes="48"
android:keyLabel="0">
android:codes="-5"
android:keyIcon="@color/colorPrimary">
2.继承keyboardview
这里主要就是3个点:1、获取按键布局(setkeyboard)2、绘制空白键跟删除键(onDraw)3、设置回调(OnKeyPressListener)。
删除按钮我做了一点处理,因为直接使用获取的dp(intrinsicWidth)的话图片会比较大,不是很好看,为了协调一点我宽高各自除了个6,然后计算了上下左右的边距(widthInterval,heightInterval),再进行的绘制。
这里用了自定义的属性展示在这里:
NumKeyView的代码
public class NumKeyView extends KeyboardView implements OnKeyboardActionListener {
//用于区分左下角空白按键,(要与xml里设置的数值相同)
private int KEYCODE_EMPTY=-10;
//删除按键背景图片
private Drawable mDeleteDrawable;
//最下面两个灰色的按键(空白按键跟删除按键)
private int mBgColor;
public NumKeyView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context,attrs,0);
}
public Num