安卓键盘加上数字_Android 自定义数字键盘实现方法

自定义数字键盘

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值