Android 自定义键盘控件_身份证号码输入
2017-09-18 10:31
393 查看
效果图:
步骤:
1.继承键盘类,写一个MyKeyboardView ,备用。
2.在res下新建一个xml文件夹,新建一个xml,用来描绘键盘的样式。
3.新建一个自定义控件类,真正实现键盘类的书写。
4.在xml中加入键盘。
附加:为了让键盘拥有分隔的效果,需求给键盘添加keyBackground,代码如下:
//bg_keyboard_btn.xml
//keys_normal_shape.xml
//keys_pressed_shape.xml
5.在java文件中添加相应的代码。
以上为基本的代码了。
步骤:
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; } @Override public void onDraw(Canvas canvas) { sup 4000 er.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() { @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; case 9997://完成 hideKeyboard(); 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; }
4.在xml中加入键盘。
<com.ds.widget.MyKeyboardView android: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() { @Override public 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 自定义的数字键盘 支持随意拖动 和稳定的字符输入的控件
- android自定义密码输入键盘控件
- android自定义键盘 限制车牌号码输入
- Android 平板中 自定义键盘(popuwindow) 居于屏幕左下方 仿微信的密码输入界面
- android 输入法挤压屏幕、android输入键盘覆盖了屏幕控件的解决办法
- android 输入法出现挤压屏幕、android输入键盘覆盖了屏幕控件的解决办法
- 教你写一个Android可快速复用的小键盘输入控件
- Android 自定义Dialog 里面有EditText控件点击关闭输入法失效的问题
- Android自定义支付宝输入软键盘
- Android-->自定义模版EditText(多用于手机号码,银行卡号等输入)
- android-- 简单自定义安全键盘(只能输入字母,数字,部分符号)
- Android自定义控件之我的电话小键盘
- Android UI设计系列之自定义ViewGroup打造通用的关闭键盘小控件ImeObserverLayout(9)
- Android自定义类似支付宝密码输入的控件
- Android进阶——自定义View之继承系统控件实现自带删除按钮动画效果和软键盘自动悬浮于文本框下方
- ANDROID 输入法出现挤压屏幕、ANDROID输入键盘覆盖了屏幕控件的解决办法
- Android自定义类似支付宝密码输入的控件
- iOS无法使用自定义密码输入控件(自带键盘)
- Android Fragment 中自定义键盘如何调整布局,不挡住界面控件
- android 点击空白处收起键盘和自定义EditText实现右下角计数控件