• 前言
  • 相关属性
    • Keyboard
    • Row
    • Key
    • KeyboardView
  • 自定义键盘
    • 布局

      • 字母键盘布局
      • 数字键盘布局
    • 逻辑
    • 回显
    • 备选小键盘

前言

前段时间改造了公司的安全键盘,是基于DialogButton自定义的。也因此借机了解下 Android 平台提供的自定义键盘接口。主要有两个类:KeyboardKeyboardView。很搞笑的是,百度出来自定义Android键盘(与自定义Android输入法不同)的文章千篇一律。

注:这里讲的自定义键盘同公司安全键盘是两种实现方式,不存在泄露公司内部技术的问题!!!

不去吐槽别人,楼主秉持只原创和翻译的作风,输出这第50篇博客。相关属性部分是对照官方文档和Demo实践翻译的,若有瑕疵,请见谅。楼主csdn主页请点击flueky专栏。

相关属性

Keyboard

序号 属性 类型 描述
1 keyHeight dimension/fractional Key高度,区分精确值(dp、px等)和相对值(%、%p)
2 keyWidth dimension/fractional Key宽度,同上
3 horizontalGap dimension/fractional Key水平间隙,同上
4 verticalGap dimension/fractional Key按键间隙(垂直),同上

Row

序号 属性 类型 描述
1 keyHeight dimension/fractional Key高度,区分精确值(dp、px等)和相对值(%、%p)
2 keyWidth dimension/fractional Key宽度,同上
3 horizontalGap dimension/fractional Key水平间隙,同上
4 verticalGap dimension/fractional Key按键间隙(垂直),同上
5 keyboardMode reference 键盘类型,如果该行的类型不符合键盘的类型,将跳过该行。
6 rowEdgeFlags enum 行边界标记,top/bottom,键盘顶(底)部锚点。

Key

序号 属性 类型 描述
1 keyHeight dimension/fractional Key高度,区分精确值(dp、px等)和相对值(%、%p)
2 keyWidth dimension/fractional Key宽度,同上
3 horizontalGap dimension/fractional Key水平间隙,同上
4 verticalGap dimension/fractional Key按键间隙(垂直),同上
5 codes int Key输出符号对应的Unicode值,官方还说支持字转义字符串,不明白。
6 iconPreview reference 弹出回显的icon
7 isModifier boolean 是否功能修饰键,如:Alt/Shift
8 isSticky boolean 是否是开关键
9 isRepeatable boolean 是否允许重复。true表示长按时重复执行。
10 keyEdgeFlags enum Key边缘位置标记,left/right,键盘左(右)边锚点。
11 keyIcon reference 替换label显示在按键上的icon。
12 keyLabel reference 显示在Key上的标签。
13 keyOutputText string Key按下时输出的字符或字符串。
14 popupCharacters string 小键盘显示的字符,用于显示Key候选项。
15 popupKeyboard reference 按键候选小键盘的keyboard布局

KeyboardView

序号 属性 类型 描述
1 keyBackground reference 按键的图像背景,必须包含多个状态的drawable
2 verticalCorrection dimension 补充触摸y坐标的偏移,用于偏差矫正
3 keyPreviewLayout reference 按键按下时预览框的布局
4 keyPreviewOffset dimension 按键按下时预览框的偏移。>0 向下,<0 向上。
5 keyPreviewHeight dimension 按键按下时预览框的高度。
6 keyTextSize dimension 按键文字大小。
7 keyTextColor color 按键文字颜色。
8 labelTextSize dimension 标签文字大小,keylabel有多个字符且keycodes只有一个值时,该属性生效。
9 popupLayout reference 按键候选小键盘的KeyboardView布局。
10 shadowRadius float 按键文字阴影半径
11 shadowColor color 按键文字阴影颜色

自定义键盘

布局

<android.inputmethodservice.KeyboardView
    android:id="@+id/activity_main_keyboard"android:layout_width="match_parent"android:layout_height="wrap_content"android:background="#212121"android:keyBackground="@drawable/key_bg"android:keyTextColor="#dddddd"android:keyTextSize="18sp"android:labelTextSize="18sp"android:paddingBottom="2dp"android:paddingTop="2dp" />

键盘容器视图,Demo中直接放在Activity布局。KeyboardView可以显示不同类型的Keyboard。请区分backgroundkeyBackgroundkeyBackground内容如下:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"><!-- 此处设置key边距 --><item
        android:bottom="2dp"android:left="2dp"android:right="2dp"android:top="2dp"><selector><!-- 按压后图层 --><item android:state_pressed="true"><shape><solid android:color="#565656" /><corners android:radius="5dp" /></shape></item><!-- 正常状态图层 --><item><shape><solid android:color="#383838" /><corners android:radius="5dp" /></shape></item></selector></item>
</layer-list>

字母键盘布局

<?xml version="1.0" encoding="utf-8"?>
<Keyboard xmlns:android="http://schemas.android.com/apk/res/android"android:keyHeight="50dp"android:keyWidth="10%p"><Row android:rowEdgeFlags="top"><Key
            android:keyEdgeFlags="left"android:keyLabel="q" /><Key android:keyLabel="w" /><Key android:keyLabel="e" /><Key android:keyLabel="r" /><Key android:keyLabel="t" /><Key android:keyLabel="y" /><Key android:keyLabel="u" /><Key android:keyLabel="i" /><Key android:keyLabel="o" /><Key
            android:keyEdgeFlags="right"android:keyLabel="p" /></Row><Row><Key
            android:codes="97"android:horizontalGap="5%p"android:keyEdgeFlags="left"android:keyLabel="a" /><Key android:keyLabel="s" /><Key android:keyLabel="d" /><Key android:keyLabel="f" /><Key android:keyLabel="g" /><Key android:keyLabel="h" /><Key android:keyLabel="j" /><Key android:keyLabel="k" /><Key
            android:keyEdgeFlags="right"android:keyLabel="l" /></Row><Row><Key
            android:codes="-1"android:isModifier="true"android:isSticky="true"android:keyEdgeFlags="left"android:keyIcon="@drawable/key_caps_lock_icon"android:keyWidth="15%p" /><Key android:keyLabel="z" /><Key android:keyLabel="x" /><Key android:keyLabel="c" /><Key android:keyLabel="v" /><Key android:keyLabel="b" /><Key android:keyLabel="n" /><Key android:keyLabel="m" /><Key
            android:codes="-5"android:isModifier="true"android:isRepeatable="true"android:keyEdgeFlags="right"android:keyIcon="@drawable/key_delete_icon"android:keyWidth="15%p" /></Row><Row android:rowEdgeFlags="bottom"><Key
            android:codes="-11"android:keyEdgeFlags="left"android:keyLabel="123"android:keyWidth="15%p" /><Key
            android:codes="32"android:isRepeatable="true"android:keyLabel=" "android:keyWidth="70%p" /><Key
            android:codes="-12"android:keyEdgeFlags="right"android:keyLabel="#+="android:keyWidth="15%p" /></Row>
</Keyboard>

效果图如下:

数字键盘布局

<?xml version="1.0" encoding="utf-8"?>
<Keyboard xmlns:android="http://schemas.android.com/apk/res/android"android:keyHeight="50dp"android:keyWidth="33.3%p"><Row><Key
            android:codes="49"android:keyLabel="1" /><Key
            android:codes="50"android:keyLabel="2" /><Key
            android:codes="51"android:keyLabel="3" /></Row><Row><Key
            android:codes="52"android:keyLabel="4" /><Key
            android:codes="53"android:keyLabel="5" /><Key
            android:codes="54"android:keyLabel="6" /></Row><Row><Key
            android:codes="55"android:keyLabel="7" /><Key
            android:codes="56"android:keyLabel="8" /><Key
            android:codes="57"android:keyLabel="9" /></Row><Row><Key
            android:codes="-10"android:keyLabel="ABC" /><Key
            android:codes="48"android:keyLabel="0" /><Key
            android:codes="-12"android:keyLabel="#+=" /></Row>
</Keyboard>

效果图如下:

  1. Key之间的间隙不建议使用horizontalGapverticalGap 设置。有兴趣可以尝试下,设置后会出现什么效果。此处采用drawablepadding属性。
  2. keyLabel属性只有一个字符时,当做输入键,keyLabel有多个字符时,如果codes也有多个值,仍然当做输入键,keyTextSize 值有效;只有一个值时当做功能键。labelTextSize值有效。
  3. codes 属性可以省略,默认使用keyLabel字符的Unicode值。功能键等其他自定义按键的 key code 建议设置为负数。
  4. codes有多个值时,单击取第一个,双击取第二个,三连击取第三个。通常建议该属性值不要超过3个。多个值用逗号分隔。

    逻辑

final Keyboard pinyin26KB = new Keyboard(this, R.xml.pinyin_26);// 字母键盘
final Keyboard numberKB = new Keyboard(this, R.xml.number); // 数字键盘keyboardView.setKeyboard(pinyin26KB); // 设置默认显示字符键盘
keyboardView.setOnKeyboardActionListener(new KeyboardView.OnKeyboardActionListener() {// 按下 key 时执行@Overridepublic void onPress(int primaryCode) {Log.d(TAG, "onPress: "+primaryCode);}// 释放 key 时执行@Overridepublic void onRelease(int primaryCode) {Log.d(TAG, "onRelease: "+primaryCode);}// 点击 key 时执行@Overridepublic void onKey(int primaryCode, int[] keyCodes) {Editable editable = edtInput.getText();int start = edtInput.getSelectionStart();switch (primaryCode) {case Keyboard.KEYCODE_SHIFT:// 设置shift状态然后刷新页面pinyin26KB.setShifted(!pinyin26KB.isShifted());keyboardView.invalidateAllKeys();break;case Keyboard.KEYCODE_DELETE:// 点击删除键,长按连续删除if (editable != null && editable.length() > 0 && start > 0) {editable.delete(start - 1, start);}break;case -10:// 自定义code,切换到拼音键盘keyboardView.setKeyboard(pinyin26KB);break;case -11:// 自定义code,切换到字母键盘keyboardView.setKeyboard(numberKB);break;case -12:// 自定义code// 切换到符号键盘,待实现break;default:// 数值codeif (primaryCode >= 97 && primaryCode <= 97 + 26) {// 按下字母键editable.insert(start, pinyin26KB.isShifted() ? Character.toString((char) (primaryCode - 32)) : Character.toString((char) (primaryCode)));} else {// 其他code值,转字符在输入框中显示editable.insert(start, Character.toString((char) (primaryCode)));}break;}}// 设置了 keyOutputText 属性后执行。@Overridepublic void onText(CharSequence text) {Log.d(TAG, "onText: "+text);}
});

已经定义的功能键code值,如下:

    public static final int KEYCODE_SHIFT = -1;public static final int KEYCODE_MODE_CHANGE = -2;public static final int KEYCODE_CANCEL = -3;public static final int KEYCODE_DONE = -4;public static final int KEYCODE_DELETE = -5;public static final int KEYCODE_ALT = -6;
  1. Shift键需要设置 isStickyisModifier 值为truecodes值为-1。
  2. Delete键需要设置isRepeatableisModifier 值为truecodes值为-5。
  3. 切换键盘的功能键需要自定义上述中未用到的code值,在onKey方法中做好对应的处理。

回显

keyboardView.setPreviewEnabled(true);

打开回显,默认是true。

android:keyPreviewLayout="@layout/preview"
android:keyPreviewHeight="50dp"
android:keyPreviewOffset="-20dp"

在键盘容器中声明以上属性。

备选小键盘

pupop_layout.xml 备选小键盘容器:

<?xml version="1.0" encoding="utf-8"?>
<android.inputmethodservice.KeyboardView xmlns:android="http://schemas.android.com/apk/res/android"android:id="@android:id/keyboardView"android:layout_width="match_parent"android:layout_height="wrap_content"android:background="#212121"android:keyBackground="@drawable/popup_bg"android:keyPreviewHeight="60dp"android:keyPreviewLayout="@layout/preview"android:keyPreviewOffset="-10dp"android:keyTextColor="#dddddd"android:keyTextSize="18sp"android:labelTextSize="18sp" />

pupop.xml 备选小键盘视图:

<?xml version="1.0" encoding="utf-8"?>
<Keyboard xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="wrap_content"android:background="@drawable/popup_bg"android:keyHeight="50dp"><Row><Key android:codes="97" /><Key android:codes="98" /><Key android:codes="99" /></Row></Keyboard>

w键添加备选功能:

<Key
    android:keyLabel="w"android:popupCharacters="123"android:popupKeyboard="@layout/pupop" />

在键盘容器中,指定备选小键盘布局:

android:popupLayout="@layout/pupop_layout"

如果只声明了popupCharacters,没有声明popupLayoutpopupKeyboard,将会使用默认布局。只声明popupLayout没声明popupKeyboardpopupLayout无效。

自定义Android键盘相关推荐

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

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

  2. android+模拟器皮肤,自定义android模拟器皮肤和键盘映射

    我想为Android模拟器创建自己的皮肤.我有照片购买了一些皮肤图像. 有一些额外的按钮,我需要映射,以便点击它时应该生成一个特定的事件. 在我的个性化皮肤文件夹的布局文件看起来是这样的:自定义and ...

  3. Android 自定义软键盘实现 数字九宫格

    前言 最近项目在对接美团外卖功能 实现外面小哥凭取货码取货 对接完功能后 用户反馈 弹出的软键盘 很难输入 数字太小了 大概是下面这种显示方式 需求 组长说 要不搞一个自定义软键盘吧 数字搞大点 方便 ...

  4. Android使用xml自定义软键盘效果(附源码)

    Android使用xml自定义软键盘效果原理: 1,软键盘其实是个控件,使用android.inputmethodserver.KeyboardView类定义. 2,主布局中使用帧布局,当我们需要显示 ...

  5. android自定义系统键盘,Android自定义软键盘

    [实例简介] Android自定义软键盘 [实例截图] [核心代码] keydemo └── keydemo ├── AndroidManifest.xml ├── bin │   ├── class ...

  6. Android自定义安全键盘

    Android自定义安全键盘 Demo地址 主要功能:包含功能:数字+字母组合键盘.纯数字键盘.字符键盘,防止录屏截屏 . 项目主要是通过Recyclerview来进行实现的,项目整体结构使用了建造者 ...

  7. android 自定义车牌键盘(kotlin)

    android 自定义车牌键盘(kotlin) 前言 示例图 实现需求 定义键盘文件 核心代码 视频效果 汉字转化 项目链接 前言 平时停车缴费都要填车牌号码,就想着自己能不能也做个车牌键盘demo. ...

  8. android keyboard颜色,Android基于KeyboardView和Keyboard实现自定义软键盘 自定义键盘背景色...

    Android基于KeyboardView和Keyboard实现自定义软键盘 在一些特别的情况下我们需要去自定义键盘 例如: 银行app的密码输入之类的 笨方法就是直接使用布局写我们的自定义软键盘 但 ...

  9. android 自定义键盘的安全性,自定义安全键盘——仿民生银行

    系统自带键盘和第三方的键盘不管是从性能还是从体验上来说都要胜于我们自己写的,但我们为什么还要去自定义键盘呢?其实就为了安全性,比如用户在输入账户密码,支付密码的时候,防止键盘获取到我们的数据:或者说美 ...

最新文章

  1. 如何用 Python 和 Flask 建立部署一个 Facebook Messenger 机器人
  2. WS-Addressing 了解
  3. caffe之mac下环境搭建
  4. 数据湖分析如何面向对象存储OSS进行优化?
  5. 筒灯智能驱动芯片作用_魅族携8款智能照明新品进军智能家居,剑指行业前三...
  6. 句子中单词首字母大写转换
  7. python json是什么_python json详解
  8. python迷宫最短路径_python实现最短路径的实例方法
  9. linux ftp下载函数函数,FTP下载的函数
  10. oracle udev 多路径,Suse 11下多路径及udev配置
  11. 用C语言编写猴子选大王程序(数据结构)
  12. ES6新特性_const声明常量以及特点---JavaScript_ECMAScript_ES6-ES11新特性工作笔记005
  13. JNDI配置数据库连接
  14. cpp map 获取所有 key_微信小程序获取地理位置和地名的方法
  15. 群晖docker安装青龙面板自动狗东京豆领取
  16. linux 搭建mqtt服务
  17. AKA传奇和亚嵌那些事
  18. 坦克大战2.0,3.0,4.0版本
  19. host管理工具SwitchHosts的使用
  20. 访南京后,回昆山之夜

热门文章

  1. Android开发系统应用程序
  2. 金堂五月花计算机学校招聘,2019年成都市金堂五月花学校招生简介
  3. VS2015使用WTL库的ribbon风格界面的注意事项
  4. 1135:配对碱基链
  5. postgresql计算两点距离
  6. springBoot整合sftp
  7. 0 13 amp 0 17c语言,急等网址跳转大神,无用网址尾巴处理:index.php?id=13amp;amp;rew...
  8. 查看笔记本当前链接Wifi的密码
  9. 万年历的Java代码
  10. localhost 拒绝了我们的连接请求。Swagger