效果图:

步骤:
1.继承键盘类,写一个MyKeyboardView ,备用。

public class MyKeyboardView extends KeyboardView {private Context context;public MyKeyboardView(Context context, AttributeSet attrs) {super(context, attrs);}public void setContext(Context context) {this.context = context;}@Overridepublic void onDraw(Canvas canvas) {super.onDraw(canvas);}}

2.在res下新建一个xml文件夹,新建一个xml,用来描绘键盘的样式。

<?xml version="1.0" encoding="utf-8"?>
<Keyboard xmlns:android="http://schemas.android.com/apk/res/android"android:horizontalGap="0dp"android:keyHeight="60dp"android:keyWidth="25%p"android:verticalGap="0dp"><Row><Key
            android:codes="49"android:keyLabel="1" /><Key
            android:codes="50"android:keyLabel="2" /><Key
            android:codes="51"android:keyLabel="3" /><Key
            android:codes="9997"android:isRepeatable="true"android:keyEdgeFlags="right"android:keyHeight="180dp"android:keyLabel="完成" /></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="88"android:keyLabel="X" /><Key
            android:codes="48"android:keyLabel="0" /><Key
            android:codes="-5"android:keyLabel="回删" /><Key
            android:codes="9995"android:keyLabel="清除" /></Row>
</Keyboard>

3.新建一个自定义控件类,真正实现键盘类的书写。

public class CustomKeyboard {private final Context context;private EditText mEdittext;private MyKeyboardView mKeyboardView;private Keyboard mKeyboard;public CustomKeyboard(Context context, MyKeyboardView keyboardView, EditText editText) {this.context = context;this.mEdittext = editText;mKeyboard = new Keyboard(context, R.xml.keyboard);mKeyboardView = keyboardView;mKeyboardView.setContext(context);mKeyboardView.setKeyboard(mKeyboard);mKeyboardView.setPreviewEnabled(false);mKeyboardView.setOnKeyboardActionListener(actionListener);}private KeyboardView.OnKeyboardActionListener actionListener = new KeyboardView.OnKeyboardActionListener() {@Overridepublic void onPress(int primaryCode) {}@Overridepublic void onRelease(int primaryCode) {}@Overridepublic void onKey(int primaryCode, int[] keyCodes) {Editable editable = mEdittext.getText();int index = mEdittext.getSelectionStart();//光标位置switch (primaryCode) {case Keyboard.KEYCODE_DELETE://回退if (editable != null && editable.length() > 0) {if (index > 0) {editable.delete(index - 1, index);}}break;case 9995://重输mEdittext.setText("");break;case 9994://左移if (index > 0) {mEdittext.setSelection(index - 1);}break;case 9996://右移if (index < mEdittext.length()) {mEdittext.setSelection(index + 1);}break;case 9997://完成hideKeyboard();break;default:editable.insert(index, Character.toString((char) primaryCode));break;}}@Overridepublic void onText(CharSequence text) {}@Overridepublic void swipeLeft() {}@Overridepublic void swipeRight() {}@Overridepublic void swipeDown() {}@Overridepublic void swipeUp() {}};public void showKeyboard() {if (mKeyboardView.getVisibility() != View.VISIBLE) {mKeyboardView.setVisibility(View.VISIBLE);}}public void hideKeyboard() {if (mKeyboardView.getVisibility() == View.VISIBLE) {mKeyboardView.setVisibility(View.GONE);}}public boolean isShowKeyboard() {return mKeyboardView.getVisibility() == View.VISIBLE;}

4.在xml中加入键盘。

    <com.ds.widget.MyKeyboardViewandroid:id="@+id/customKeyboard"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_alignParentBottom="true"android:layout_gravity="bottom"android:background="#CBCBCB"android:focusable="true"android:focusableInTouchMode="true"android:keyBackground="@drawable/bg_keyboard_btn"android:keyTextColor="#000000"android:keyTextSize="31dp"android:labelTextSize="23.04sp"android:shadowRadius="0"android:visibility="gone" />

附加:为了让键盘拥有分隔的效果,需求给键盘添加keyBackground,代码如下:
//bg_keyboard_btn.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"><item android:state_pressed="true" android:drawable="@drawable/keys_pressed_shape"/><item android:drawable="@drawable/keys_normal_shape"/>
</selector>

//keys_normal_shape.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"android:shape="rectangle"><corners android:topLeftRadius="5dp"android:topRightRadius="5dp"android:bottomLeftRadius="3dp"android:bottomRightRadius="3dp"/><gradient
        android:startColor="#fff"android:endColor="#eee"android:angle="90"/><stroke android:color="#CBCBCB"android:dashWidth="2dip"android:width="1dp"/>
</shape>

//keys_pressed_shape.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"android:shape="rectangle"><corners
        android:bottomLeftRadius="3dp"android:bottomRightRadius="3dp"android:topLeftRadius="5dp"android:topRightRadius="5dp" /><gradient
        android:angle="90"android:endColor="#eee"android:startColor="#fff" /><stroke
        android:width="1dp"android:color="#eee"android:dashWidth="2dip" />
</shape>

5.在java文件中添加相应的代码。

        //1 屏蔽掉系统默认输入法if (Build.VERSION.SDK_INT <= 10) {cl_idcard.setInputType(InputType.TYPE_NULL);} else {getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);try {Class<EditText> cls = EditText.class;Method setShowSoftInputOnFocus = cls.getMethod("setShowSoftInputOnFocus", boolean.class);setShowSoftInputOnFocus.setAccessible(true);setShowSoftInputOnFocus.invoke(cl_idcard, false);} catch (Exception e) {e.printStackTrace();}}//2 初试化键盘MyKeyboardView keyboardView = (MyKeyboardView) findViewById(R.id.customKeyboard);customKeyboard = new CustomKeyboard(NoCardLiveActivity.this, keyboardView, cl_idcard);if (cl_idcard.isSelected()) {customKeyboard.showKeyboard();} else customKeyboard.hideKeyboard();cl_idcard.setOnTouchListener(new View.OnTouchListener() {@Overridepublic boolean onTouch(View v, MotionEvent event) {customKeyboard.showKeyboard();InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);imm.hideSoftInputFromWindow(cl_idcard.getWindowToken(), 0);return false;}});

以上为基本的代码了。

Android 自定义键盘控件_身份证号码输入相关推荐

  1. Android 自定义组合控件小结

    Android 自定义组合控件小结 引言 接触Android UI开发的这段时间以来,对自定义组合控件有了一定的了解,为此小结一下,本文小结内容主要讨论的是如何使用Android SDK提供的布局和控 ...

  2. android自定义table,Android 自定义表格控件

    Android 自定义表格控件 发布时间:2018-08-20 17:07, 浏览次数:487 , 标签: Android 1.简介 tabview是一款开源表格控件,可以通过xml属性设置行列数.设 ...

  3. Android自定义时间控件不可选择未来时间

    本文出自:http://blog.csdn.net/dt235201314/article/details/78718066 Android自定义时间控件选择开始时间到结束时间 Android自定义时 ...

  4. Android自定义组合控件--EditText和Button组合成带有清空EditText内容功能的复合控件

    目标:实现EditText和Button组合成带有清空EditText内容功能的复合控件,可以通过代码设置自定义控件的相关属性. 实现效果为: (1)在res/layout目录下编写自定义组合控件的布 ...

  5. android 自定义switch控件,Android中switch自定义样式

    android 原生开关按钮控件 Switch 提供样式自定义方式,可供我们修改为适合我们开发使用的样式控件,自定义样式过程如下: 自定义switch切换drawable 新建swith_thumb. ...

  6. android 自定义view控件,Android 自定义View——自定义View控件

    Android给我们提供了大量的View控件,但这还是远远满足不了我们的要求,有时候开发所需要的控件形式是在Android提供的控件中是不存在,这就需要我们自己去定义一个.那么如何自定义控件? 学习自 ...

  7. java自定义日历控件_【无私分享】修订版干货!!!一个炫酷的自定义日历控件,摆脱日历时间选择烦恼,纯福利~...

    可能不少的小伙伴都有看楼主昨天发的自定义日历控件,虽然实现功能不多,但也还算将就吧. 但是看了的小伙伴就很心急了,说楼主上传到gitHub的东西有问题,楼主下载来看了看,基本都没问题吧,没弄好的小伙伴 ...

  8. android身高控件_RuleView Android 自定义标尺控件(选择身高、体重等) @codeKK Android开源站...

    尺子刻度 -- 自定义 view 自定义 view 学习(第一章) 1.自定义刻度尺控件 在我们想要获取用户的身高体重等信息时,直接让他们输入显然不够友好偶然看到一款 App 用了类似刻度尺的界面让用 ...

  9. android 自定义ImageView控件实现圆形图片-适用于用户头像

    android开发中常常涉及到一种情况,就是将用户上传的图片以圆形样式显示,但是用户上传的图片可以有直角.圆角.正方形等多种不确定样式,这时就用到了自定义ImageView控件,在安卓客户端使接收到的 ...

最新文章

  1. java 同步转并行_Java线程与并行编程(二)
  2. px word 表格宽度_word怎样批量修改表格的宽度(2)
  3. 【深入Java虚拟机】之一:Java内存区域与内存溢出
  4. leetcode45 跳跃游戏II 秒杀所有答案
  5. html5外链代码,html5关于外链嵌入页面通信问题
  6. 最常被利用的三大 API 漏洞:是什么、为什么、如何阻止?
  7. 1键将 Python2 代码自动转化为 Python3
  8. 【MapReduce】编程指导思想
  9. 一文解读元学习研究进展
  10. cvPyrSegmentation() 图像金字塔分割
  11. Bilibili拜年祭启发的小小探索
  12. 如何提高项目管理效率
  13. [CF891D]Sloth
  14. python 格林威治时间转换为标准时间格式
  15. 2022北京马拉松,特步助力阿奴拜克-库弯大幅PB夺冠
  16. 2021大厂Android面试经验,经典好文
  17. Python Class 05-字符串
  18. 使用eve-ng中的cisco路由器实现DMVPN
  19. ASP.NET 权限管理 页面静态化 OA系统培训 三层架构
  20. Java新生代垃圾收集器

热门文章

  1. hive复合数据类型查表使用 以及控制语句 case when、if
  2. IT和物联网监控专家丨上海道宁为您带来适用于各大行业和规模的企业提供监控解决方案——PRTG
  3. Python(三)数字日期和时间
  4. XD鞋子页面设计(下)
  5. Android屏幕校准
  6. # 腾讯数据库TcaplusDB十年厚积薄发,已是参天大树
  7. 18 矩阵——矩阵的秩、行阶梯形矩阵与秩、行列式与秩、特征值与秩、二次型与秩、矩阵秩的计算、关于秩的常用结论
  8. 测试工作方法与思想分享之
  9. 千人互动,18 位业界大咖,2022 开放原子全球开源峰会龙蜥专区总结来了
  10. windows10+arch linux双系统 uefi启动