android仿支付寶輸入車牌號
阿新 • • 發佈:2018-11-23
這個是iOS的效果圖,差異不大,樓主主攻OC,見諒
需要用到的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%" 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>
KeyboardUtil.java
public class KeyboardUtil { private Activity mActivity; private KeyboardView mKeyboardView; private EditText mEdit; /** * 省份簡稱鍵盤 */ private Keyboard provinceKeyboard; /** * 數字與大寫字母鍵盤 */ private Keyboard numberKeyboard; public 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() { @Override public void swipeUp() { } @Override public void swipeRight() { } @Override public void swipeLeft() { } @Override public void swipeDown() { } @Override public void onText(CharSequence text) { } @Override public void onRelease(int primaryCode) { } @Override public void onPress(int primaryCode) { } @Override public 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() { if(mKeyboardView == null){ return false; } 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.2 methodName = "setShowSoftInputOnFocus"; } else if (currentVersion >= 14) { // 4.0 methodName = "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(); } } } }
使用:
licese = (EditText) findViewById(R.id.licese); licese.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View view, MotionEvent event) { if (keyboardUtil == null) { keyboardUtil = new KeyboardUtil(JumpScanActivity.this, licese); keyboardUtil.hideSoftInputMethod(); keyboardUtil.showKeyboard(); } else { keyboardUtil.showKeyboard(); } return false; } });