原标题:Android 自定义控件 - 仿支付宝数字键盘
简介
在一些带有支付功能的 App 中,输入的密码一般只能是纯数字,虽然我们可以指定 EditText 输入框只能输入数字,但是为了提供用户的使用体验,我们往往更倾向于使用自定义的纯数字键盘。
本文效果:
实现步骤:
集成系统的 KeyBoardView 类,在初始化时初始化键盘布局,设置 KeyBoard 对象。
实现 OnKeyboardActionListener 接口,处理按键交互事件。
根据需求绘制按键背景和按键图标。
设置监听器,将输入的内容回调给调用方。
键盘布局
在 res/xml/ 目录下创建 xml 文件:key_password_number.xml
android:horizontalGap="1dp"
android:keyHeight="9%p"
android:keyWidth="33.3333%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="55"
android:keyLabel="7"/>
android:codes="56"
android:keyLabel="8"/>