Java tutorial
package com.smapley.vehicle.utils; import android.app.Activity; import android.content.Context; import android.inputmethodservice.Keyboard; import android.inputmethodservice.KeyboardView; import android.inputmethodservice.KeyboardView.OnKeyboardActionListener; import android.support.v4.content.ContextCompat; import android.view.View; import android.view.WindowManager; import android.widget.EditText; import com.smapley.vehicle.R; import org.apache.commons.lang3.ArrayUtils; import org.apache.commons.lang3.StringUtils; import java.lang.reflect.Method; /** * Created by wuzhixiong on 2017/5/20. */ public class LicenseKeyboardUtil { private KeyboardView keyboardView; private Keyboard k1;// ? private Keyboard k2;// ? private String provinceShort[]; private String letterAndDigit[]; private EditText edits[]; private int currentEditText = 0;//?EditText public LicenseKeyboardUtil(Activity ctx, EditText edits[]) { this.edits = edits; k1 = new Keyboard(ctx, R.xml.province_short_keyboard); k2 = new Keyboard(ctx, R.xml.lettersanddigit_keyboard); keyboardView = (KeyboardView) ((Activity) ctx).findViewById(R.id.set_keyboard); keyboardView.setKeyboard(k1); keyboardView.setEnabled(true); //true,popup?<key>android:popupCharacters="" keyboardView.setPreviewEnabled(false); //? keyboardView.setOnKeyboardActionListener(listener); provinceShort = new String[] { "", "", "", "?", "", "", "", "?", "", "", "?", "", "", "", "", "", "", "", "", "", "?", "?", "", "", "?", "", "", "?", "?", "", "", "", "?", "?" }; letterAndDigit = new String[] { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "Q", "W", "E", "R", "T", "Y", "U", "I", "O", "P", "A", "S", "D", "F", "G", "H", "J", "K", "L", "Z", "X", "C", "V", "B", "N", "M" }; if (ArrayUtils.isNotEmpty(edits)) { for (int i = 0; i < edits.length; i++) { setSoftKeyboradHide(ctx, edits[i]); edits[i].setCursorVisible(false); int finalI = i; edits[i].setOnFocusChangeListener(new View.OnFocusChangeListener() { @Override public void onFocusChange(View v, boolean hasFocus) { if (hasFocus) { currentEditText = finalI; if (currentEditText < 1) { //?? keyboardView.setKeyboard(k1); } else { keyboardView.setKeyboard(k2); } showKeyboard(); v.setBackgroundColor(ContextCompat.getColor(ctx, R.color.cyan2)); } else { v.setBackgroundColor(ContextCompat.getColor(ctx, R.color.gray3)); } } }); } } } private OnKeyboardActionListener listener = new 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) { if (primaryCode == 112) { //xml112 edits[currentEditText].setText("");//?EditText""currentEditText-1 currentEditText--; if (currentEditText < 1) { //?? keyboardView.setKeyboard(k1); } if (currentEditText < 0) { currentEditText = 0; } edits[currentEditText].requestFocus(); } else if (primaryCode == 66) { //xml?66 hideKeyboard(); } else { // if (currentEditText == 0) { //currentEditText==0??, // ?,EditText // ??? //currentEditText+1 edits[0].setText(provinceShort[primaryCode]); currentEditText = 1; //?? keyboardView.setKeyboard(k2); } else { //?? if (currentEditText == 1 && !letterAndDigit[primaryCode].matches("[A-Z]{1}")) { return; } edits[currentEditText].setText(letterAndDigit[primaryCode]); currentEditText++; if (currentEditText > 6) { currentEditText = 6; } } edits[currentEditText].requestFocus(); } } }; public void setData(String string) { if (StringUtils.isNotEmpty(string) && ArrayUtils.isNotEmpty(edits) && string.length() >= edits.length) { for (int i = 0; i < edits.length; i++) { edits[i].setText(string.substring(i, i + 1)); } } } public String getData() { StringBuilder sb = new StringBuilder(); if (ArrayUtils.isNotEmpty(edits)) { for (EditText editText : edits) { sb.append(editText.getText().toString()); } } return sb.toString(); } private void setSoftKeyboradHide(Activity ctx, EditText edit) { ctx.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN); try { Class<EditText> cls = EditText.class; String method_setSoftInputOnFocus = "setShowSoftInputOnFocus"; // method_setSoftInputOnFocus = "setSoftInputOnFocus"; Method setShowSoftInputOnFocus = cls.getMethod(method_setSoftInputOnFocus, boolean.class); setShowSoftInputOnFocus.setAccessible(false); setShowSoftInputOnFocus.invoke(edit, false); } catch (Exception e) { e.printStackTrace(); } } /** * */ public void showKeyboard() { int visibility = keyboardView.getVisibility(); if (visibility == View.GONE || visibility == View.INVISIBLE) { keyboardView.setVisibility(View.VISIBLE); } } /** * ?? */ public void hideKeyboard() { int visibility = keyboardView.getVisibility(); if (visibility == View.VISIBLE) { keyboardView.setVisibility(View.INVISIBLE); } if (ArrayUtils.isNotEmpty(edits)) { for (EditText editext : edits) { editext.clearFocus(); } } } public boolean isShow() { int visibility = keyboardView.getVisibility(); if (visibility == View.VISIBLE) { return true; } else { return false; } } }