Android从0到完整项目(9)输入键盘

自定义键盘
自定义View实现支付密码的输入

自定义系统键盘

核心:KeyboardView 与自定义键盘文件 xml一起使用实现

  province_keyboard = new Keyboard(mContext, R.xml.province_abbreviation);
        number_keyboar = new Keyboard(mContext, R.xml.number_or_letters);

xml 部分代码:

<?xml version="1.0" encoding="utf-8"?>
<Keyboard
    xmlns:android="http://schemas.android.com/apk/res/android"
        android:keyWidth="10%p" android:keyHeight="8%p"
        android:horizontalGap="0.0px" android:verticalGap="0.0px"

      >

    <Row android:verticalGap="1%p"  >
        <Key android:codes="20140" android:keyLabel="京"
             android:horizontalGap="1%p" android:keyWidth="8%p"
             android:keyEdgeFlags="left"  />
        <Key android:codes="27941" android:keyLabel="津"
             android:horizontalGap="2%p" android:keyWidth="8%p" />
        <Key android:codes="20864" android:keyLabel="冀"
             android:horizontalGap="2%p" android:keyWidth="8%p" />
        <Key android:codes="40065" android:keyLabel="鲁"
             android:horizontalGap="2%p" android:keyWidth="8%p" />
        <Key android:codes="26187" android:keyLabel="晋"
             android:horizontalGap="2%p" android:keyWidth="8%p" />
        <Key android:codes="33945" android:keyLabel="蒙"
             android:horizontalGap="2%p" android:keyWidth="8%p" />
        <Key android:codes="36797" android:keyLabel="辽"
             android:horizontalGap="2%p" android:keyWidth="8%p" />
        <Key android:codes="21513" android:keyLabel="吉"
             android:horizontalGap="2%p" android:keyWidth="8%p" />
        <Key android:codes="40657" android:keyLabel="黑"
             android:horizontalGap="2%p" android:keyWidth="8%p" />
        <Key android:codes="27818" android:keyLabel="沪"
             android:horizontalGap="2%p" android:keyWidth="8%p"
             android:keyEdgeFlags="right"/>
    </Row>

    <Row android:verticalGap="1%p">
        <Key android:codes="33487" android:keyLabel="苏"
             android:horizontalGap="1%p" android:keyWidth="8%p"
             android:keyEdgeFlags="left" />
        <Key android:codes="27993" android:keyLabel="浙"
             android:horizontalGap="2%p" android:keyWidth="8%p" />
        <Key android:codes="30358" android:keyLabel="皖"
             android:horizontalGap="2%p" android:keyWidth="8%p" />
        <Key android:codes="38397" android:keyLabel="闽"
             android:horizontalGap="2%p" android:keyWidth="8%p" />
        <Key android:codes="36195" android:keyLabel="赣"
             android:horizontalGap="2%p" android:keyWidth="8%p" />
        <Key android:codes="35947" android:keyLabel="豫"
             android:horizontalGap="2%p" android:keyWidth="8%p" />
        <Key android:codes="37122" android:keyLabel="鄂"
             android:horizontalGap="2%p" android:keyWidth="8%p" />
        <Key android:codes="28248" android:keyLabel="湘"
             android:horizontalGap="2%p" android:keyWidth="8%p" />
        <Key android:codes="31908" android:keyLabel="粤"
             android:horizontalGap="2%p" android:keyWidth="8%p" />
        <Key android:codes="26690" android:keyLabel="桂"
             android:horizontalGap="2%p" android:keyWidth="8%p"
             android:keyEdgeFlags="right" />
    </Row>

    <Row android:verticalGap="1%p">
        <Key android:codes="28189" android:keyLabel="渝"
             android:horizontalGap="11%p" android:keyWidth="8%p"
             android:keyEdgeFlags="left"/>
        <Key android:codes="24029" android:keyLabel="川"
             android:horizontalGap="2%p" android:keyWidth="8%p" />
        <Key android:codes="36149" android:keyLabel="贵"
             android:horizontalGap="2%p" android:keyWidth="8%p" />
        <Key android:codes="20113"  android:keyLabel="云"
             android:horizontalGap="2%p" android:keyWidth="8%p" />
        <Key android:codes="34255" android:keyLabel="藏"
             android:horizontalGap="2%p" android:keyWidth="8%p" />
        <Key android:codes="38485"  android:keyLabel="陕"
             android:horizontalGap="2%p" android:keyWidth="8%p" />
        <Key android:codes="29976" android:keyLabel="甘"
             android:horizontalGap="2%p" android:keyWidth="8%p" />
        <Key android:codes="38738"  android:keyLabel="青"
            android:horizontalGap="2%p" android:keyWidth="8%p"
            android:keyEdgeFlags="right" />
    </Row>

    <Row >
        <Key android:codes="-1" android:keyLabel="隐藏"
            android:keyWidth="13%p" android:keyEdgeFlags="left"
             android:isModifier="true" android:isSticky="true"  />
        <Key android:codes="29756"  android:keyLabel="琼"
             android:horizontalGap="8%p" android:keyWidth="8%p"/>
        <Key android:codes="26032" android:keyLabel="新"
             android:horizontalGap="2%p" android:keyWidth="8%p" />
        <Key android:codes="23425" android:keyLabel="宁"
             android:horizontalGap="2%p" android:keyWidth="8%p" />
        <Key android:codes="28207" android:keyLabel="港"
             android:horizontalGap="2%p" android:keyWidth="8%p" />
        <Key android:codes="28595" android:keyLabel="澳"
             android:horizontalGap="2%p" android:keyWidth="8%p" />
        <Key android:codes="21488" android:keyLabel="台"
             android:horizontalGap="2%p" android:keyWidth="8%p"/>
        <Key  android:codes="-3"
             android:keyEdgeFlags="right" android:keyIcon="@drawable/remove_edittext_data"
             android:horizontalGap="8%p" android:keyWidth="13%p"
             android:isRepeatable="true" />
    </Row>

</Keyboard>  

支付自定义键盘是 上下两个自定义View来实现的。比较简单 不多写了。

代码不定时更新 https://github.com/chinaltz/JustAndroid

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容