目录

  • 1.场景:
  • 2.想法:
  • 3.开始实现:
    • 《一》 在res包下创建xml目录,Keyboard标签来定义键盘布局:
    • 《二》创建IKeyboardView类并继承KeyboardView类,设置键盘布局(数字和字母)
    • 《三》 处理自定义键盘按键的点击事件以及预览,并实现数字和字母键盘(包括大小写)之间的切换
    • 《四》 绑定EditText,并且屏蔽系统键盘(实现点击切换绑定多个EditText)
    • 《五》. 使用
  • 4.KeyboardView,keyboard,Key,Row属性解释:
  • 5.完整代码
  • 6.注意事项
  • 6.结尾

1.场景:

项目中有定制的设备需要放在室外,用户使用时使用系统自带的键盘肯能没有那么方便,所以就需要使用到了自定义键盘

(结尾附上完整代码可以直接使用,不想看过程的可以直接跳过)

2.想法:

封装成一个比较通用的,当成一个View一样来使用


**实现自定义键盘思路:**1. 在res包下创建xml目录,Keyboard标签来定义键盘布局2. 创建IKeyboardView类并继承KeyboardView类,设置键盘布局(数字和字母)3. 处理自定义键盘按键的点击事件以及预览,并实现数字和字母键盘(包括大小写)之间的切换4. 绑定EditText,并且屏蔽系统键盘(实现点击切换绑定多个EditText)5. 使用

3.开始实现:

《一》 在res包下创建xml目录,Keyboard标签来定义键盘布局:

创建keyboard_num.xml文件(数字键盘布局)

<?xml version="1.0" encoding="utf-8"?>
<Keyboard xmlns:android="http://schemas.android.com/apk/res/android"android:keyWidth="10%p"android:keyHeight="7%p"android:horizontalGap="0.0px"android:verticalGap="0.0px"><Row android:verticalGap="1%p"><Keyandroid:codes="113"android:keyLabel="q"android:horizontalGap="1.81%p"></Key><Keyandroid:codes="119"android:keyLabel="w"android:horizontalGap="1.81%p"></Key><Keyandroid:codes="101"android:keyLabel="e"android:horizontalGap="1.81%p"></Key><Keyandroid:codes="114"android:keyLabel="r"android:horizontalGap="1.81%p"></Key><Keyandroid:codes="116"android:keyLabel="t"android:horizontalGap="1.81%p"></Key><Keyandroid:codes="121"android:keyLabel="y"android:horizontalGap="1.81%p"></Key><Keyandroid:codes="117"android:keyLabel="y"android:horizontalGap="1.81%p"></Key><Keyandroid:codes="105"android:keyLabel="i"android:horizontalGap="1.81%p"></Key><Keyandroid:codes="111"android:keyLabel="o"android:horizontalGap="1.81%p"></Key><Keyandroid:codes="112"android:keyLabel="p"android:horizontalGap="1.81%p"></Key></Row><Row android:verticalGap="1%p"><Keyandroid:codes="97"android:keyLabel="a"android:keyWidth="9%p"android:horizontalGap="5.5%p"></Key><Keyandroid:codes="115"android:keyLabel="s"android:keyWidth="9%p"android:horizontalGap="1%p"></Key><Keyandroid:codes="100"android:keyLabel="d"android:keyWidth="9%p"android:horizontalGap="1%p"></Key><Keyandroid:codes="102"android:keyLabel="f"android:keyWidth="9%p"android:horizontalGap="1%p"></Key><Keyandroid:codes="103"android:keyLabel="g"android:keyWidth="9%p"android:horizontalGap="1%p"></Key><Keyandroid:codes="104"android:keyLabel="h"android:keyWidth="9%p"android:horizontalGap="1%p"></Key><Keyandroid:codes="106"android:keyLabel="j"android:keyWidth="9%p"android:horizontalGap="1%p"></Key><Keyandroid:codes="107"android:keyLabel="k"android:keyWidth="9%p"android:horizontalGap="1%p"></Key><Keyandroid:codes="108"android:keyLabel="l"android:keyWidth="9%p"android:horizontalGap="1%p"></Key></Row><Row android:verticalGap="1%p"><Keyandroid:codes="-1"android:keyLabel="大写"android:keyWidth="17%p"android:horizontalGap="1%p"></Key><Keyandroid:codes="122"android:keyLabel="z"android:horizontalGap="1%p"></Key><Keyandroid:codes="120"android:keyLabel="x"android:horizontalGap="1%p"></Key><Keyandroid:codes="99"android:keyLabel="c"android:horizontalGap="1%p"></Key><Keyandroid:codes="118"android:keyLabel="v"android:horizontalGap="1%p"></Key><Keyandroid:codes="98"android:keyLabel="b"android:horizontalGap="1%p"></Key><Keyandroid:codes="110"android:keyLabel="n"android:horizontalGap="1%p"></Key><Keyandroid:codes="109"android:keyLabel="m"android:horizontalGap="1%p"></Key><Keyandroid:codes="-5"android:isRepeatable="true"android:keyWidth="17%p"android:horizontalGap="1%p"></Key></Row><Row><Keyandroid:codes="-2"android:keyLabel="123"android:keyWidth="20%p"android:horizontalGap="1%p"></Key><Keyandroid:codes="32"android:keyLabel="space"android:keyWidth="48%p"android:horizontalGap="5%p"></Key><Keyandroid:codes="-4"android:keyLabel="完成"android:keyWidth="20%p"android:horizontalGap="5%p"></Key></Row>
</Keyboard>

创建keyboard_letter.xml文件(字母键盘布局)

<?xml version="1.0" encoding="utf-8"?>
<Keyboard xmlns:android="http://schemas.android.com/apk/res/android"android:keyWidth="10%p"android:keyHeight="7%p"android:horizontalGap="0.0px"android:verticalGap="0.0px"><Row android:verticalGap="1%p"><Keyandroid:codes="113"android:keyLabel="q"android:horizontalGap="1.81%p"></Key><Keyandroid:codes="119"android:keyLabel="w"android:horizontalGap="1.81%p"></Key><Keyandroid:codes="101"android:keyLabel="e"android:horizontalGap="1.81%p"></Key><Keyandroid:codes="114"android:keyLabel="r"android:horizontalGap="1.81%p"></Key><Keyandroid:codes="116"android:keyLabel="t"android:horizontalGap="1.81%p"></Key><Keyandroid:codes="121"android:keyLabel="y"android:horizontalGap="1.81%p"></Key><Keyandroid:codes="117"android:keyLabel="y"android:horizontalGap="1.81%p"></Key><Keyandroid:codes="105"android:keyLabel="i"android:horizontalGap="1.81%p"></Key><Keyandroid:codes="111"android:keyLabel="o"android:horizontalGap="1.81%p"></Key><Keyandroid:codes="112"android:keyLabel="p"android:horizontalGap="1.81%p"></Key></Row><Row android:verticalGap="1%p"><Keyandroid:codes="97"android:keyLabel="a"android:keyWidth="9%p"android:horizontalGap="5.5%p"></Key><Keyandroid:codes="115"android:keyLabel="s"android:keyWidth="9%p"android:horizontalGap="1%p"></Key><Keyandroid:codes="100"android:keyLabel="d"android:keyWidth="9%p"android:horizontalGap="1%p"></Key><Keyandroid:codes="102"android:keyLabel="f"android:keyWidth="9%p"android:horizontalGap="1%p"></Key><Keyandroid:codes="103"android:keyLabel="g"android:keyWidth="9%p"android:horizontalGap="1%p"></Key><Keyandroid:codes="104"android:keyLabel="h"android:keyWidth="9%p"android:horizontalGap="1%p"></Key><Keyandroid:codes="106"android:keyLabel="j"android:keyWidth="9%p"android:horizontalGap="1%p"></Key><Keyandroid:codes="107"android:keyLabel="k"android:keyWidth="9%p"android:horizontalGap="1%p"></Key><Keyandroid:codes="108"android:keyLabel="l"android:keyWidth="9%p"android:horizontalGap="1%p"></Key></Row><Row android:verticalGap="1%p"><Keyandroid:codes="-1"android:keyLabel="大写"android:keyWidth="17%p"android:horizontalGap="1%p"></Key><Keyandroid:codes="122"android:keyLabel="z"android:horizontalGap="1%p"></Key><Keyandroid:codes="120"android:keyLabel="x"android:horizontalGap="1%p"></Key><Keyandroid:codes="99"android:keyLabel="c"android:horizontalGap="1%p"></Key><Keyandroid:codes="118"android:keyLabel="v"android:horizontalGap="1%p"></Key><Keyandroid:codes="98"android:keyLabel="b"android:horizontalGap="1%p"></Key><Keyandroid:codes="110"android:keyLabel="n"android:horizontalGap="1%p"></Key><Keyandroid:codes="109"android:keyLabel="m"android:horizontalGap="1%p"></Key><Keyandroid:codes="-5"android:isRepeatable="true"android:keyWidth="17%p"android:horizontalGap="1%p"></Key></Row><Row><Keyandroid:codes="-2"android:keyLabel="123"android:keyWidth="20%p"android:horizontalGap="1%p"></Key><Keyandroid:codes="32"android:keyLabel="space"android:keyWidth="48%p"android:horizontalGap="5%p"></Key><Keyandroid:codes="-4"android:keyLabel="完成"android:keyWidth="20%p"android:horizontalGap="5%p"></Key></Row>
</Keyboard>

《二》创建IKeyboardView类并继承KeyboardView类,设置键盘布局(数字和字母)

public class IKeyBoardView extends KeyboardView implements KeyboardView.OnKeyboardActionListener {public IIKeyBoardView(Context context, AttributeSet attrs) {super(context, attrs);init();}public IKeyBoardView(Context context, AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);init();}public IIKeyBoardView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {super(context, attrs, defStyleAttr, defStyleRes);init();}/*** 数字键盘*/private Keyboard keyboardNumber;/*** 字母键盘*/private Keyboard keyboardLetter;//绑定的输入框private EditText mEditText;/*** 是否发生键盘切换*/private boolean changeLetter = false;/*** 是否为大写*/private boolean isCapital = false;private List<Integer> noLists = new ArrayList<>();private int[] arrays = new int[]{Keyboard.KEYCODE_SHIFT, Keyboard.KEYCODE_MODE_CHANGE,Keyboard.KEYCODE_CANCEL, Keyboard.KEYCODE_DONE, Keyboard.KEYCODE_DELETE,Keyboard.KEYCODE_ALT, 32};//初始化private void init(){keyboardNumber = new Keyboard(getContext(), R.xml.keyboard_num);keyboardLetter = new Keyboard(getContext(), R.xml.keyboard_letter);//设置一些不需要预览的键位for (int i = 0; i < arrays.length; i++) {noLists.add(arrays[i]);}//默认使用数字键盘setKeyboard(keyboardNumber);//是否启用预览setPreviewEnabled(true);//键盘动作监听setOnKeyboardActionListener(this);}@Overridepublic void onPress(int primaryCode) {}@Overridepublic void onRelease(int primaryCode) {}@Overridepublic void onKey(int primaryCode, int[] keyCodes) {}@Overridepublic void onText(CharSequence text) {}@Overridepublic void swipeLeft() {}@Overridepublic void swipeRight() {}@Overridepublic void swipeDown() {}@Overridepublic void swipeUp() {}
}

《三》 处理自定义键盘按键的点击事件以及预览,并实现数字和字母键盘(包括大小写)之间的切换

核心代码onKey方法(设置键盘输入)

//在onKey回调中的代码
Editable editable = mEditText.getText();int start = mEditText.getSelectionStart();switch (primaryCode) {case Keyboard.KEYCODE_DELETE://删除if (editable != null && editable.length() > 0 && start > 0) {editable.delete(start - 1, start);}break;case Keyboard.KEYCODE_MODE_CHANGE://字母键盘与数字键盘切换changeKeyBoard(!changeLetter);break;case Keyboard.KEYCODE_DONE://完成changeKeyBoard(!changeLetter);break;case Keyboard.KEYCODE_SHIFT://大小写切换changeCapital(!isCapital);setKeyboard(keyboardLetter);break;default:editable.insert(start, Character.toString((char) primaryCode));break;}

还有几个必要的方法附上

 /*** 切换键盘大小写*/private void changeCapital(boolean b) {isCapital = b;List<Keyboard.Key> lists = keyboardLetter.getKeys();for (Keyboard.Key key : lists) {if (key.label != null && isKey(key.label.toString())) {if (isCapital) {key.label = key.label.toString().toUpperCase();key.codes[0] = key.codes[0] - 32;} else {key.label = key.label.toString().toLowerCase();key.codes[0] = key.codes[0] + 32;}} else if (key.label != null && key.label.toString().equals("小写")) {key.label = "大写";} else if (key.label != null && key.label.toString().equals("大写")) {key.label = "小写";}}}/*** 判断此key是否正确,且存在 * * @param key * @return*/private boolean isKey(String key) {String lowercase = "abcdefghijklmnopqrstuvwxyz";if (lowercase.indexOf(key.toLowerCase()) > -1) {return true;}return false;}/*** 切换键盘类型*/private void changeKeyBoard(boolean b) {changeLetter = b;if (b) {setKeyboard(keyboardLetter);} else {setKeyboard(keyboardNumber);}}/*** 判断是否需要预览Key** @param primaryCode keyCode*/private void canShowPreview(int primaryCode) {if (noLists.contains(primaryCode)) {setPreviewEnabled(false);} else {setPreviewEnabled(true);}}//设置需要绑定的EditViewpublic void setmEditText(EditText mEditText) {this.mEditText = mEditText;}

《四》 绑定EditText,并且屏蔽系统键盘(实现点击切换绑定多个EditText)

这块我屏蔽键盘的方式是比较粗暴的,直接在EditTextView当中设置了屏蔽焦点,这样的同时会让EditTextView丢失光标

public class IEditText extends AppCompatEditText {private IKeyboardView mIKeyboardView;public IEditText(@NonNull Context context) {super(context);init();}public IEditText(@NonNull Context context, @Nullable AttributeSet attrs) {super(context, attrs);init();}public IEditText(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);init();}private void init() {setInputType(InputType.TYPE_NULL);}//绑定键盘public void setmIKeyboardView(IKeyboardView mIKeyboardView) {this.mIKeyboardView = mIKeyboardView;}@Overridepublic boolean onTouchEvent(MotionEvent event) {//按下时绑定当前的EditTextif (event.getAction() == MotionEvent.ACTION_DOWN){if (mIKeyboardView!=null){mIKeyboardView.setEditText(this);}}return super.onTouchEvent(event);}}

《五》. 使用

例如:两个EditTextView一个自定义键盘,因为太简单我就不放出布局代码了(占位置) 就看看咱是如何绑定的

public class MainActivity extends AppCompatActivity {private static final String TAG = "MainActivity:";@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);MainActivityBinding binding = DataBindingUtil.setContentView(this, R.layout.main_activity);//默认绑定一个EditTextViewbinding.viewKeyboard.setEditText(binding.ed1);//点击时切换绑定键盘binding.ed1.setmIKeyboardView(binding.viewKeyboard);binding.ed2.setmIKeyboardView(binding.viewKeyboard);}}

4.KeyboardView,keyboard,Key,Row属性解释:

  • Keyboard
  • Row
  • Key
  • KeyboardView
    ![在这里插入图片描述](https://img-blog.csdnimg.cn/1e5efd3bbb8248af91cf6beb1ab6b913.png

5.完整代码

https://gitee.com/li-weihao1010/keyboard

6.注意事项

在清单文件(AndroidManifest.xml)中,
activity标签下面不要使用该属性
android:hardwareAccelerated=“true”
不然会导致你键盘卡顿以及点击事件错位问题

6.结尾

如果大家觉得有哪里写得有问题,可以告诉我,一起学习
参考链接

Android自定义键盘(KeyboardView)相关推荐

  1. android自定义数字键盘和字母键盘,Android自定义键盘的实现(数字键盘和字母键盘)...

    Android自定义键盘的实现(数字键盘和字母键盘) 发布时间:2020-09-04 03:18:48 来源:脚本之家 阅读:100 作者:浪淘沙xud 在项目中,产品对于输入方式会有特殊的要求,需要 ...

  2. Android 自定义键盘 随机键盘

    之前上传的另外一个自定义键盘,并没有实现键盘弹出的时候,布局向上自动调整.(网络上所有的自定义键盘都没有添加自适应的功能,而且布局没这么好看,上一个例子资源链接为:http://download.cs ...

  3. android 键盘开发demo,Android自定义键盘之中文键盘demo

    [实例简介] Android自定义键盘之中文键盘demo,演示了汉字键盘的实现方法.更详细描述见相关博客. [实例截图] [核心代码] keydemo └── keydemo ├── AndroidM ...

  4. 关于android自定义键盘

    因为百度里面搜索android自定义键盘内容很少,所以就将我在github上找到的好点的键盘(keyboard)源码分享出来: (因为我在源码上修改了,所以图片是我源码修改之后的图片,大家可以到git ...

  5. android 自定义键盘字体大小,android.inputmethodservice.KeyboardView 自定义键盘 字体大小设置...

    KeyboardView 设置自定义键盘上文本的属性,其中字体的设置用:android:keyTextSize    android:labelTextSize 即可实现!! 亲测! android: ...

  6. Android的自定义键盘颜色,android自定义键盘(解决弹出提示的字体颜色问题)

    最近准备要做一个项目,需要用到自定义小键盘来确保安全,而且还需要精确获得用户点击键盘时的落点位置.力度.指尖接触屏幕的面积等参数. 在写自定义键盘的时 最近准备要做一个项目,需要用到自定义小键盘来确保 ...

  7. Android自定义键盘

    支持英文大小写,数字及部分符号输入的自定义键盘 新建类文件 KeyboardUtil package com.sanz.lefangandroid.util; import android.app.A ...

  8. Android自定义键盘的简单实现

    概述 突然发现好多软件都使用了自己定义的软键盘.自己就想着先把这块坑先踩踩把,以后掉坑的时候不至于帅的太惨.言归正传,对于自定义软键盘.需要用到系统提供的两个类:Keyboard和KeyboardVi ...

  9. Android自定义键盘之汉字键盘

    一.软键盘介绍 实现软键盘主要用到了系统的两个类:Keyboard和KeyboardView. Keyboard类源码的介绍是: Listener for virtual keyboard event ...

最新文章

  1. python异常值删除_python删除有异常值
  2. LeetCode 159. Longest Substring with At Most Two Distinct Characters --Java,C++,Python解法
  3. 黄仁勋的“数据梦” 英伟达豪掷69亿美金虎口夺食
  4. 好看的论文千篇一律,有趣的Github项目万里挑一!
  5. 【CSS3】CSS3文本相关属性大全
  6. 5 个给 Linux 新手的最佳包管理器
  7. sql SELECT时的with(nolock)选项说明
  8. 计算机上找不到运行程序怎么办,电脑联网时显示找不到应用程序怎么办
  9. 5种方式实现 Java 单例模式
  10. CO-PA: 获利能力分析配置和业务实践
  11. axure 鼠标样式_Axure菜单导航怎么添加鼠标选中变色的特效?
  12. 7-13 寻找大富翁 (25 分)
  13. 计算机网口速率修改,win7系统修改无线网卡连接速率的操作方法
  14. 《项目管理问题分析与解决方案小册》(持续更新中)
  15. 黑马程序员—String类常用方法
  16. [问题]假定网络中的路由器B的路由表有如下的项目(这三列分别表示“目的网络”、“距离”和“下一跳路由器”):
  17. Java 模板引擎主流,freemaker ,thymeleaf (技术的介绍,重要是理解)(学成在线day04)
  18. springboot微软文本转语音(texttospeach) java实现微软文本转语音
  19. 计算机应用技术学教程,最新计算机应用技术基础教程 科技大学教育读本.pdf
  20. oracle集群服务创建表空间,Oracle数据库集群添加表空间操作规范

热门文章

  1. 西北大学 中北大学计算机对比,东北、西北、东南、西南、中北、中南,最容易被忽视的中北大学!...
  2. Android 开发高手课 课后练习(1 ~ 5)
  3. ajax:php数组数据类型
  4. ubuntu wps缺少字体_一个字体:系统风汜霰汜源圆
  5. MuMu模拟器网络连接异常如何处理?
  6. ocr 哪个好 外文_OCR软件哪个好?
  7. 打游戏计算机内存不足,玩游戏内存不足? 教你轻松几步升级电脑内存
  8. google play aab上传PAD的使用流程
  9. python 自动生成数字_python – 使用yaml自动生成配置和具有数字...
  10. 人生被彻底改变时,我还以为那只是寻常的一天