最近在做物流的项目,需要输入车牌号,传统的输入体验并不好,来回切换还要手打,便有了自定义键盘的想法。
其实这个车牌需求并不多见,物流、租车、停车相关的会常见一些,好在有前辈已经铺好路了
这个比较详细:https://blog.csdn.net/dgs960825/article/details/50344743

1.在res文件夹下新建xml文件夹,新建两个文件:number_or_letters.xml(数字和大写字母)、province_abbreviation.xml(省会、直辖市、特别行政区简称)。

number_or_letters.xml

<?xml version="1.0" encoding="UTF-8"?>
<Keyboard android:keyWidth="10%p" android:keyHeight="8%"android:horizontalGap="0.0px" android:verticalGap="0.0px"xmlns:android="http://schemas.android.com/apk/res/android"><Row android:verticalGap="1%p"><Key android:codes="49" android:keyLabel="1"android:horizontalGap="1%p" android:keyWidth="8%p"android:keyEdgeFlags="left"  /><Key android:codes="50" android:keyLabel="2"android:horizontalGap="2%p" android:keyWidth="8%p" /><Key android:codes="51" android:keyLabel="3"android:horizontalGap="2%p" android:keyWidth="8%p" /><Key android:codes="52" android:keyLabel="4"android:horizontalGap="2%p" android:keyWidth="8%p" /><Key android:codes="53" android:keyLabel="5"android:horizontalGap="2%p" android:keyWidth="8%p" /><Key android:codes="54" android:keyLabel="6"android:horizontalGap="2%p" android:keyWidth="8%p" /><Key android:codes="55" android:keyLabel="7"android:horizontalGap="2%p" android:keyWidth="8%p" /><Key android:codes="56" android:keyLabel="8"android:horizontalGap="2%p" android:keyWidth="8%p" /><Key android:codes="57" android:keyLabel="9"android:horizontalGap="2%p" android:keyWidth="8%p" /><Key android:codes="48" android:keyLabel="0"android:horizontalGap="2%p" android:keyWidth="8%p"android:keyEdgeFlags="right"/></Row><Row android:verticalGap="1%p"><Key android:codes="81" android:keyLabel="Q"android:horizontalGap="1%p" android:keyWidth="8%p"android:keyEdgeFlags="left" /><Key android:codes="87" android:keyLabel="W"android:horizontalGap="2%p" android:keyWidth="8%p" /><Key android:codes="69" android:keyLabel="E"android:horizontalGap="2%p" android:keyWidth="8%p" /><Key android:codes="82" android:keyLabel="R"android:horizontalGap="2%p" android:keyWidth="8%p" /><Key android:codes="84" android:keyLabel="T"android:horizontalGap="2%p" android:keyWidth="8%p" /><Key android:codes="89" android:keyLabel="Y"android:horizontalGap="2%p" android:keyWidth="8%p" /><Key android:codes="85" android:keyLabel="U"android:horizontalGap="2%p" android:keyWidth="8%p" /><Key android:codes="73" android:keyLabel="I"android:horizontalGap="2%p" android:keyWidth="8%p" /><Key android:codes="79" android:keyLabel="O"android:horizontalGap="2%p" android:keyWidth="8%p" /><Key android:codes="80" android:keyLabel="P"android:horizontalGap="2%p" android:keyWidth="8%p"android:keyEdgeFlags="right" /></Row><Row android:verticalGap="1%p"><Key android:codes="65" android:keyLabel="A"android:horizontalGap="6%p" android:keyWidth="8%p"android:keyEdgeFlags="left"/><Key android:codes="83" android:keyLabel="S"android:horizontalGap="2%p" android:keyWidth="8%p" /><Key android:codes="68" android:keyLabel="D"android:horizontalGap="2%p" android:keyWidth="8%p" /><Key android:codes="70"  android:keyLabel="F"android:horizontalGap="2%p" android:keyWidth="8%p" /><Key android:codes="71" android:keyLabel="G"android:horizontalGap="2%p" android:keyWidth="8%p" /><Key android:codes="72"  android:keyLabel="H"android:horizontalGap="2%p" android:keyWidth="8%p" /><Key android:codes="74" android:keyLabel="J"android:horizontalGap="2%p" android:keyWidth="8%p" /><Key android:codes="75" android:keyLabel="K"android:horizontalGap="2%p" android:keyWidth="8%p" /><Key android:codes="76"  android:keyLabel="L"android:horizontalGap="2%p" android:keyWidth="8%p"android:keyEdgeFlags="right" /></Row><Row ><Key android:codes="-1"android:keyEdgeFlags="left" android:keyLabel="省份"android:horizontalGap="1%p" android:keyWidth="13%p"android:isModifier="true" android:isSticky="true"  /><Key android:codes="90" android:keyLabel="Z"android:horizontalGap="2%p" android:keyWidth="8%p"/><Key android:codes="88" android:keyLabel="X"android:horizontalGap="2%p" android:keyWidth="8%p" /><Key android:codes="67" android:keyLabel="C"android:horizontalGap="2%p" android:keyWidth="8%p" /><Key android:codes="86" android:keyLabel="V"android:horizontalGap="2%p" android:keyWidth="8%p" /><Key android:codes="66" android:keyLabel="B"android:horizontalGap="2%p" android:keyWidth="8%p" /><Key android:codes="78" android:keyLabel="N"android:horizontalGap="2%p" android:keyWidth="8%p"/><Key android:codes="77" android:keyLabel="M"android:horizontalGap="2%p" android:keyWidth="8%p"/><Key  android:codes="-3"android:keyEdgeFlags="right" android:keyLabel="删除"android:horizontalGap="2%p" android:keyWidth="13%p"android:isRepeatable="false" /></Row></Keyboard>

province_abbreviation.xml

<?xml version="1.0" encoding="UTF-8"?>
<Keyboard
    android:keyWidth="10%p" android:keyHeight="8%p"android:horizontalGap="0.0px" android:verticalGap="0.0px"xmlns:android="http://schemas.android.com/apk/res/android"><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:keyEdgeFlags="left" android:keyLabel="ABC"android:horizontalGap="4%p" android:keyWidth="15%p"android:isModifier="true" android:isSticky="true"  /><Key android:codes="29756" android:keyLabel="琼"android:horizontalGap="2%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:keyLabel="删除"android:horizontalGap="2%p" android:keyWidth="15%p"android:isRepeatable="false" /></Row></Keyboard>

2.新建键盘工具类KeyboardUtil(初始化、交互、显示和隐藏)

package yechaoa.com.platenumberkeyboard;import android.app.Activity;
import android.content.Context;
import android.inputmethodservice.Keyboard;
import android.inputmethodservice.KeyboardView;
import android.text.Editable;
import android.text.InputType;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;/*** Created by yechao on 2018/5/15/015.* Describe :*/
public class KeyboardUtil {private Activity mActivity;private KeyboardView mKeyboardView;private EditText mEdit;/*** 省份简称键盘*/private Keyboard provinceKeyboard;/*** 数字与大写字母键盘*/private Keyboard numberKeyboard;KeyboardUtil(Activity activity, EditText edit) {mActivity = activity;mEdit = edit;provinceKeyboard = new Keyboard(activity, R.xml.province_abbreviation);numberKeyboard = new Keyboard(activity, R.xml.number_or_letters);mKeyboardView = (KeyboardView) activity.findViewById(R.id.keyboard_view);mKeyboardView.setKeyboard(provinceKeyboard);mKeyboardView.setEnabled(true);mKeyboardView.setPreviewEnabled(false);mKeyboardView.setOnKeyboardActionListener(listener);}private KeyboardView.OnKeyboardActionListener listener = new KeyboardView.OnKeyboardActionListener() {@Overridepublic void swipeUp() {}@Overridepublic void swipeRight() {}@Overridepublic void swipeLeft() {}@Overridepublic void swipeDown() {}@Overridepublic void onText(CharSequence text) {}@Overridepublic void onRelease(int primaryCode) {}@Overridepublic void onPress(int primaryCode) {}@Overridepublic void onKey(int primaryCode, int[] keyCodes) {Editable editable = mEdit.getText();int start = mEdit.getSelectionStart();//判定是否是中文的正则表达式 [\\u4e00-\\u9fa5]判断一个中文 [\\u4e00-\\u9fa5]+多个中文String reg = "[\\u4e00-\\u9fa5]";if (primaryCode == -1) {// 省份简称与数字键盘切换if (mEdit.getText().toString().matches(reg)) {changeKeyboard(true);}} else if (primaryCode == -3) {if (editable != null && editable.length() > 0) {//没有输入内容时软键盘重置为省份简称软键盘if (editable.length() == 1) {changeKeyboard(false);}if (start > 0) {editable.delete(start - 1, start);}}} else {editable.insert(start, Character.toString((char) primaryCode));// 判断第一个字符是否是中文,是,则自动切换到数字软键盘if (mEdit.getText().toString().matches(reg)) {changeKeyboard(true);}}}};/*** 指定切换软键盘 isNumber false表示要切换为省份简称软键盘 true表示要切换为数字软键盘*/private void changeKeyboard(boolean isNumber) {if (isNumber) {mKeyboardView.setKeyboard(numberKeyboard);} else {mKeyboardView.setKeyboard(provinceKeyboard);}}/*** 软键盘展示状态*/public boolean isShow() {return mKeyboardView.getVisibility() == View.VISIBLE;}/*** 软键盘展示*/public void showKeyboard() {int visibility = mKeyboardView.getVisibility();if (visibility == View.GONE || visibility == View.INVISIBLE) {mKeyboardView.setVisibility(View.VISIBLE);}}/*** 软键盘隐藏*/public void hideKeyboard() {int visibility = mKeyboardView.getVisibility();if (visibility == View.VISIBLE) {mKeyboardView.setVisibility(View.INVISIBLE);}}/*** 禁掉系统软键盘*/public void hideSoftInputMethod() {mActivity.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);int currentVersion = android.os.Build.VERSION.SDK_INT;String methodName = null;if (currentVersion >= 16) {// 4.2methodName = "setShowSoftInputOnFocus";} else if (currentVersion >= 14) {// 4.0methodName = "setSoftInputShownOnFocus";}if (methodName == null) {mEdit.setInputType(InputType.TYPE_NULL);} else {Class<EditText> cls = EditText.class;Method setShowSoftInputOnFocus;try {setShowSoftInputOnFocus = cls.getMethod(methodName, boolean.class);setShowSoftInputOnFocus.setAccessible(true);setShowSoftInputOnFocus.invoke(mEdit, false);} catch (NoSuchMethodException e) {mEdit.setInputType(InputType.TYPE_NULL);e.printStackTrace();} catch (IllegalAccessException e) {e.printStackTrace();} catch (IllegalArgumentException e) {e.printStackTrace();} catch (InvocationTargetException e) {e.printStackTrace();}}}}

3.Activity引用

package yechaoa.com.platenumberkeyboard;import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;public class MainActivity extends AppCompatActivity {private KeyboardUtil keyboardUtil;private EditText mEditText;@SuppressLint("ClickableViewAccessibility")@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);mEditText = findViewById(R.id.edit_text);mEditText.setOnTouchListener(new View.OnTouchListener() {@Overridepublic boolean onTouch(View view, MotionEvent event) {if (keyboardUtil == null) {keyboardUtil = new KeyboardUtil(MainActivity.this, mEditText);keyboardUtil.hideSoftInputMethod();keyboardUtil.showKeyboard();} else {keyboardUtil.showKeyboard();}return false;}});mEditText.addTextChangedListener(new TextWatcher() {@Overridepublic void afterTextChanged(Editable s) {Log.i("字符变换后", "afterTextChanged");}@Overridepublic void beforeTextChanged(CharSequence s, int start, int count, int after) {Log.i("字符变换前", s + "-" + start + "-" + count + "-" + after);}@Overridepublic void onTextChanged(CharSequence s, int start, int before, int count) {Log.i("字符变换中", s + "-" + "-" + start + "-" + before + "-" + count);}});}@Overridepublic boolean onKeyDown(int keyCode, KeyEvent event) {if (keyCode == KeyEvent.KEYCODE_BACK) {if (keyboardUtil.isShow()) {keyboardUtil.hideKeyboard();} else {finish();}}return false;}}

4.相关属性

android:keyBackground=”@drawable/selector_key”
* 按键的背景颜色

android:shadowColor=”#FFFFFF”
android:shadowRadius=”0.0”
* 不加这两个属性 文字会出现模糊

android:keyTextColor=”#000”
* 字体颜色

android:keyTextSize=”18sp”
* 字体大小

android:keyIcon=”@drawable/ic_delete”
* 按键上的图标

android:codes=”20140”
* 输出的内容 对照ASCII表

android:keyLabel=”京”
* 按键上显示的内容

android:horizontalGap=”0px”
* 水平方向的间隙

android:verticalGap=”0px”
* 垂直反向的间隙

android:keyEdgeFlags=”right”
* 按键的对齐方式

github:https://github.com/yechaoa/PlateNumberKeyBoard

Android 自定义车牌键盘相关推荐

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

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

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

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

  3. Android自定义安全键盘

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

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

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

  5. android自动软键盘,Android自定义软键盘

    MyKeyboard Android自定义键盘的使用 实现步骤 第一步: 1.新建一个xml文件夹放在res目录下面,然后新建xml文件:money_keyboard.xml 2.然后在XML文件中添 ...

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

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

  7. 【uniapp前端组件】自定义车牌键盘

    自定义车牌输入键盘–车牌键盘 简介 本组件根据自定义万能键盘(数字键盘.身份证键盘.带小数点数字键盘.车牌键盘)升级而来,老组件代码有点看不懂了,哈哈哈哈.另外数字键盘.身份证键盘.小数点数字键盘un ...

  8. Android自定义软键盘输入法,隐藏系统输入法显示光标的实现

    android实现自定义软键盘,先上图看效果,效果基本上是仿ios输入法实现的 这里是实现隐藏系统输入法,同时让EditText能获取光标的代码部分(通过反射调用): <span style=& ...

  9. Android 自定义软键盘实现

    module链接:https://download.csdn.net/download/meixi_android/10652565 compile project(':edlibrary') , ' ...

  10. Android 自定义手势键盘

    自定义手势键盘 手势键盘有三种状态,初始状态.点击状态和错误状态,分别以下列三个图片显示. 2. 数据类CircleArea CircleArea类用来记录手势键盘的信息. static class ...

最新文章

  1. c++ const 关键字 学习笔记
  2. Python进阶8——字典与散列表,字符串编解码
  3. Python sort()和sorted()
  4. /opt/hbase/conf 中不能启动hbase_浅谈Hbase在用户画像上的应用
  5. Codeforces Round #694 Div. 2
  6. 基于阿里云 MaxCompute 构建企业云数据仓库CDW
  7. GDAL_GRID插值Y轴反向的问题
  8. 微信内置浏览器调起外部浏览器打开指定网页连接
  9. iOS程序-UIScrollView的基本使用
  10. OneDrive更换要同步文件夹
  11. 39个SEO经典案例
  12. JSP乱码(Tomcat和Weblogic)
  13. 第三模块 商务写作的“5步法”
  14. 股权比例的几个临界点
  15. 小码哥CRM项目(二)p20~p35
  16. 熊绎:我看软件工程师的职业规划(转载)
  17. react 调用微信jsdk扫一扫
  18. 【基础入门题026】佩尔数列Pell(n)
  19. 【Hive】内置函数—日期函数
  20. 计算机无法打开声音,驱动正常但是电脑没声音怎么办-电脑自学网

热门文章

  1. 细说PHP电子书pdf下载
  2. 使用Java根据约定格式生成MySQL建表语句
  3. 学习爬虫第二天:继续爬公司内部的通讯录
  4. u盘pe安装深度linux系统教程,深度U盘装机大师_深度u盘启动盘安装深度系统
  5. C语言版RPG角色生成器
  6. MOOC下载器的文档整理
  7. centos7 虚拟机系统磁盘扩容
  8. 极域电子教室忘记密码或无法卸载怎么办
  9. js判断是否微信浏览器
  10. 4900款网红pr调色lr预设ps滤镜达芬奇lut电影视频素材