Android 基于身份证号的自定义键盘
上图上代码

public class MainActivity extends AppCompatActivity {
EditText writebankcard_mobileedit;
CustomKeyboard mCustomKeyboard;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
writebankcard_mobileedit = (EditText) findViewById(R.id.writebankcard_mobileedit);
//1 屏蔽掉系统默认输入法
if (Build.VERSION.SDK_INT <= 10) {
writebankcard_mobileedit.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(writebankcard_mobileedit, false);
} catch (Exception e) {
e.printStackTrace();
}
}
//2 初试化键盘
MyKeyboardView keyboardView = (MyKeyboardView) findViewById(R.id.customKeyboard);
mCustomKeyboard = new CustomKeyboard(MainActivity.this, keyboardView, writebankcard_mobileedit);
mCustomKeyboard.showKeyboard();
writebankcard_mobileedit.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
mCustomKeyboard.showKeyboard();
return false;
}
});
}
//物理返回键
@Override
public void onBackPressed() {
if (mCustomKeyboard.isShowKeyboard()){
mCustomKeyboard.hideKeyboard();
}else {
finish();
}
}
}
public class CustomKeyboard {
private EditText mEdittext;
private MyKeyboardView mKeyboardView;
private Keyboard mKeyboard;
public CustomKeyboard(Context context, MyKeyboardView keyboardView, EditText editText) {
this.mEdittext = editText;
mKeyboard = new Keyboard(context, R.xml.keyboard);//从xml中加载自定义的键盘
mKeyboardView = keyboardView;
mKeyboardView.setContext(context);
mKeyboardView.setKeyboard(mKeyboard);
mKeyboardView.setPreviewEnabled(false);
mKeyboardView.setOnKeyboardActionListener(actionListener);
}
private KeyboardView.OnKeyboardActionListener actionListener = new KeyboardView.OnKeyboardActionListener() {
@Override
public void onPress(int primaryCode) {
}
@Override
public void onRelease(int primaryCode) {
}
@Override
public 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;
default:
editable.insert(index, Character.toString((char) primaryCode));
break;
}
}
@Override
public void onText(CharSequence text) {
}
@Override
public void swipeLeft() {
}
@Override
public void swipeRight() {
}
@Override
public void swipeDown() {
}
@Override
public 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;
}
}
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;
}
@Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
/* List<Keyboard.Key> keys = getKeyboard().getKeys();
for(Keyboard.Key key: keys) {
if(key.label.equals("delete"))
resetOKBtn(key, canvas);
}*/
}
/**
* 绘制OK键的点9图
* @author Song
* @param key
* @param canvas
*/
private void resetOKBtn(Keyboard.Key key, Canvas canvas) {
//将OK键重新绘制
/* Drawable npd = (Drawable) context.getResources().getDrawable(R.mipmap.icon_number_del);
npd.setBounds(key.x, key.y + 1, key.x + key.width, key.y + key.height + 1);
npd.draw(canvas);*/
}
}
键盘点击变色
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_pressed="true" >
<color android:color="#d1d5db"/>
</item>
<item android:state_pressed="false" >
<color android:color="#ffffff"/>
</item>
</selector>
光标颜色
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<size android:width="2px" />
<solid android:color="#000000" />
</shape>
键盘
<?xml version="1.0" encoding="utf-8"?>
<Keyboard xmlns:android="http://schemas.android.com/apk/res/android"
android:horizontalGap="3px"
android:verticalGap="3px"
android:keyHeight="50dp"
android:keyWidth="33.33%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="88"
android:keyLabel="X"
/>
<Key
android:codes="48"
android:keyLabel="0" />
<Key
android:codes="-5"
android:keyIcon="@mipmap/icon_number_del"/>
</Row>
</Keyboard>
demo地址 https://github.com/huanyi0723/VipKeyboardTest
Android 基于身份证号的自定义键盘的更多相关文章
- 银行卡号、电话号、身份证号 EditText 自定义格式的输入框
package com.yidian.AddSpaceEditText;import android.text.Editable;import android.text.InputFilter;imp ...
- 基于struts2框架-自定义身份证号验证器
自定义拦截器的步骤: 1.定义一个验证器的类: > 自定义的验证器都需要实现 Validator接口. > 可以选择继承 ValidatorSupport 或 FieldValidato ...
- KeyboardDemo - Android身份证号、车牌号快捷输入键盘
Android身份证号.车牌号快捷输入键盘 项目地址 Github 键盘部分在 keyboard module 中 键盘与EditText绑定参照 MainActivity
- 移动开发首页业界资讯移动应用平台技术专题 输入您要搜索的内容 基于Java Socket的自定义协议,实现Android与服务器的长连接(二)
在阅读本文前需要对socket以及自定义协议有一个基本的了解,可以先查看上一篇文章<基于Java Socket的自定义协议,实现Android与服务器的长连接(一)>学习相关的基础知识点. ...
- android自定义键盘(解决弹出提示的字体颜色问题)
最近准备要做一个项目,需要用到自定义小键盘来确保安全,而且还需要精确获得用户点击键盘时的落点位置.力度.指尖接触屏幕的面积等参数. 在写自定义键盘的时候,用到了国内网上的一些代码,出处是 向先人致敬! ...
- Laravel 中自定义 手机号和身份证号验证
首先在 Providers\AppServiceProvider.php 文件中自定义 手机号和身份证号验证 // AppServiceProvider.php 文件 <?php namespa ...
- Android 正则表达式验证手机号、姓名(包含少数民族)、身份证号
最近项目中新增的功能,需要对手机号.姓名.身份证号等一些信息进行验证,最好的方法是通过正则表达式来验证,网上查了一些资料,写了这几个工具方法. 1.验证手机号 规则:第一位只能是1,第二位为3-8中的 ...
- Android 使用正则表达式验证身份证号是否符合规则
我国当前的身份证号分为三种: 一.15位身份证号 二.18位身份证号(前17位位数字,最后一位为字母x) 三.18为身份证号(18位都是数字) 具体验证请参考下面代码: /** * 验证身份证号是否符 ...
- bootstrap-validator基本使用(自定义验证身份证号和手机号)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8&quo ...
随机推荐
- 软件工程课堂练习——找出1-n中1出现的个数
题目:给定一个十进制的正整数,写下从1开始,到N的所有整数,然后数一下其中出现“1”的个数. 要求:写一个函数 f(N) ,返回1 到 N 之间出现的 “1”的个数.例如 f(12) = 5. 在3 ...
- 团队Alpha冲刺(六)
目录 组员情况 组员1(组长):胡绪佩 组员2:胡青元 组员3:庄卉 组员4:家灿 组员5:凯琳 组员6:翟丹丹 组员7:何家伟 组员8:政演 组员9:黄鸿杰 组员10:刘一好 组员11:何宇恒 展示 ...
- struts2 action json(还有servlet的)
http://yshjava.iteye.com/blog/1333104 留着 版权声明:本文为博主原创文章,未经博主允许不得转载.
- 常用的比较器:实现方式Compareable和Comparator
class Dog{ int size; int weight; public Dog(int s, int w){ size = s; weight = w; } } 目的:对于Dog对象作为元素所 ...
- 第5章 Linux 常用网络指令
网络参数设定使用的指令 手动/自动设定与启动/关闭 IP 参数: ifconfig, ifup, ifdown ifconfig :查询.设定网络卡与 IP 网域等相关参数:ifup, ifdown: ...
- 1106C程序语法树
- 使用ResourceBundle 类读取 src 下的 xxx.properties 文件
之前要读取 src 下的 .properties 文件都是使用的类加载器,加载类路径下的资源文件当做一个流来处理,load 到一个 Properties 对象上. jdbc.properties 代码 ...
- JMeter脚本强化之检查点
上一篇讲述了对脚本做参数化的两种方法,并对参数化设置结果做了简单的验证,就是通过添加断言.本篇将详细一点介绍怎么使用断言做文本检查,或者叫做设置检查点. 首先来看看下面的三个图,这三个图是用查看结果树 ...
- 第156天:canvas(三)
一.变形 1.1 translate translate(x, y) 用来移动 canvas 的原点到指定的位置 translate方法接受两个参数.x 是左右偏移量,y 是上下偏移量,如右图 ...
- bzoj2699 更新
题意 对于一个数列A[1..N],一种寻找最大值的方法是:依次枚举A[2]到A[N],如果A[i]比当前的A[1]值要大,那么就令A[1]=A[i],最后A[1]为所求最大值.假设所有数都在范围[1, ...