通过自定义android键盘实现车牌号输入法

本文介绍如何在Android应用中实现车牌号输入法,限制用户输入特定字符,如省份汉字简称、大写字母和数字。通过自定义Keyboard和KeyboardView,详细讲解键盘布局的设置,并提供代码示例。
摘要由CSDN通过智能技术生成

前言

很多的移动应用中经常需要限定用户输入特定的字符,比如吱妇保,微信钱包等在输入支付密码的时候就是直接调出的纯数字键盘并且不允许用户切换为非数字键盘,这在一定程度上方便了前端数据校验同时也有很好的用户体验.那么如果有比数字键盘更复杂的输入法我们该怎么实现呢?

车牌号输入法

最近我在项目中遇到的一个车牌输入法的问题,需要限定用户第一个字符只能是34个省份汉字简称,第二位为大写字母,余下5位为数字+字母大写的形势,整个界面如图:
Drawing

Drawing

Keyboard和KeyboardView类

android.inputmethodservice.Keyboard对象即代表了一个虚拟键盘,通过加载res/xml/下的.xml键盘布局文件来初始化一个虚拟键盘,其源码如下:

/**
 * Loads an XML description of a keyboard and stores the attributes of the keys. A keyboard
 * consists of rows of keys.
 * <p>The layout file for a keyboard contains XML that looks like the following snippet:</p>
 * <Keyboard
 *         android:keyWidth="%10p"
 *         android:keyHeight="50px"
 *         android:horizontalGap="2px"
 *         android:verticalGap="2px">
 *     <Row android:keyWidth="32px">
 *         <Key android:keyLabel="A">
 *         ...
 *     </Row>
 *     ...
 * </Keyboard>
 * @attr ref android.R.styleable#Keyboard_keyWidth
 * @attr ref android.R.styleable#Keyboard_keyHeight
 * @attr ref android.R.styleable#Keyboard_horizontalGap
 * @attr ref android.R.styleable#Keyboard_verticalGap
 */
public class Keyboard 
Keyboard属性

该段源码摘自SDK21 注释android:keyWidth=”%10p”可能有谬误,正确的应为android:keyWidth=”10%p”

android:keyWidth=”%10p” –>每个按键宽度
android:keyHeight=”50px” –>每个按键高度
android:horizontalGap=”2px” –>按键水平间隙
android:verticalGap=”2px” –>按键垂直间隙
Keyboard类下还有Keyboard.Row和Keyboard.Key两个内部类,分别代表了按键的一行和单个按键,<Keyboard>元素中的属性也都使用与<Row>和<Key>元素

汉字省份简称布局

res/xml/province_short_keyboard.xml

<?xml version="1.0" encoding="UTF-8"?>
<Keyboard
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:horizontalGap="4px"
    android:keyHeight="40dp"
    android:keyWidth="10.7%p"
    android:verticalGap="4px">
    <Row>
        <Key
            android:codes="0"
            android:keyEdgeFlags="left"
            android:keyLabel="京" />
        <Key
            android:codes="1"
            android:keyLabel="津" />
        <Key
            android:codes="2"
            android:keyLabel="冀" />
        <Key
            android:codes="3"
            android:keyLabel="鲁" />
        <Key
            android:codes="4"
            android:keyLabel="晋" />
        <Key
            android:codes="5"
            android:keyLabel="蒙" />
        <Key
            android:codes="6"
            android:keyLabel="辽" />
        <Key
            android:codes="7"
            android:keyLabel="吉" />
        <Key
            android:codes="8"
            android:keyEdgeFlags="right"
            android:keyLabel="黑" />
    </Row>
    <Row>
        <Key
            android:codes="9"
            android:keyEdgeFlags="left"
            android:keyLabel="沪" />
        <Key
            android:codes="10"
            android:keyLabel="苏" />
        <Key
            android:codes="11"
            android:keyLabel="浙" />
        <Key
            android:codes="12"
            android:keyLabel="皖" />
        <Key
            android:codes="13"
            android:keyLabel="闽" />
        <Key
            android:codes="14"
            android:keyLabel="赣" />
        <Key
            android:codes="15"
            android:keyLabel="豫" />
        <Key
            android:codes="16"
            android:keyLabel="鄂" />
        <Key
            android:codes="17"
            android:keyEdgeFlags="right"
            android:keyLabel="湘" />
    </Row>
    <Row>
        <Key
            android:codes="18"
            android:keyEdgeFlags="left"
            android:keyLabel="粤"
            />
        <Key
            android:codes="19"
            android:keyLabel="桂" />
        <Key
            android:codes="20"
            android:keyLabel="渝" />
        <Key
            android:codes="21"
            android:keyLabel="川" />
        <Key
            android:codes="22"
            android:keyLabel="贵" />
        <Key
            android:codes="23"
            android:keyLabel="云" />
        <Key
            android:codes="24"
            android:keyLabel="藏" />
        <Key
            android:codes="25"
            android:keyLabel="陕" />
        <Key
            android:codes="26"
            android:keyLabel="甘"
            android:keyEdgeFlags="right"
            />
    </Row>
    <Row android:rowEdgeFlags="bottom" android:keyWidth="14.28%p">
        <Key
            android:codes="27"
            android:keyLabel="青"
            andro
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值