android 中输入车牌的键盘
完美的键盘输入。妈妈再也不用担心我的键盘了
核心代码:
PpKeyBoardView 中设置键盘中特殊键盘的样式:主要是在public void onDraw(Canvas canvas) 方法中重新绘制
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.inputmethodservice.Keyboard;
import android.inputmethodservice.Keyboard.Key;
import android.inputmethodservice.KeyboardView;
import android.util.AttributeSet;
import java.util.List;
public class PpKeyBoardView extends KeyboardView {
private Context mContext;
private int rightType = 1;// 右下角
private int heightPixels;
private float density;
private static Keyboard mKeyBoard;
public PpKeyBoardView(Context context, AttributeSet attrs) {
super(context, attrs);
this.mContext = context;
heightPixels = mContext.getResources().getDisplayMetrics().heightPixels;
density = mContext.getResources().getDisplayMetrics().density;
}
public PpKeyBoardView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
this.mContext = context;
heightPixels = mContext.getResources().getDisplayMetrics().heightPixels;
density = mContext.getResources().getDisplayMetrics().density;
}
/**
* 重新画一些按键
*/
@Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
mKeyBoard = KeyboardUtil.getKeyBoardType();
List<Key> keys = mKeyBoard.getKeys();
for (Key key : keys) {
// 数字键盘的处理
// if (mKeyBoard.equals(KeyboardUtil.numKeyboard)) {
// initRightType(key);
// drawNumSpecialKey(key, canvas);
// } else
if (mKeyBoard.equals(KeyboardUtil.abcKeyboard)) {
drawABCSpecialKey(key, canvas);
} else if (mKeyBoard.equals(KeyboardUtil.symbolKeyboard)) {
drawSymbolSpecialKey(key, canvas);
} else if(mKeyBoard.equals(KeyboardUtil.abcKeyboardLast)){
drawABCSpecialKey(key, canvas);
}
}
}
//字母键盘特殊处理背景
private void drawABCSpecialKey(Key key, Canvas canvas) {
//TODO 待添加特殊处理
if (key.codes[0] == -5) {
drawKeyBackground(R.drawable.btn_keyboard_key_delete, canvas, key);
drawText(canvas, key);
}
if (key.codes[0] == -1) {
drawKeyBackground(R.drawable.btn_keyboard_key_shift, canvas, key);
drawText(canvas, key);
}
if (key.codes[0] == 123123 || key.codes[0] == 789789) {
drawKeyBackground(R.drawable.btn_keyboard_key_123, canvas, key);
drawText(canvas, key);
}
if (key.codes[0] == 32) {
drawKeyBackground(R.drawable.btn_keyboard_key_space, canvas, key);
}
if(key.label!=null && ("I".equalsIgnoreCase(key.label.toString()) || "O".equalsIgnoreCase(key.label.toString()))){
drawKeyBackground(R.drawable.btn_keyboard_key_123, canvas, key);
drawText(canvas, key);
}
}
//标点键盘特殊处理背景
private void drawSymbolSpecialKey(Key key, Canvas canvas) {
//TODO 待添加特殊处理
if (key.codes[0] == 123123 ||
key.codes[0] == 456456) {
drawKeyBackground(R.drawable.btn_keyboard_key_change, canvas, key);
drawText(canvas, key);
}
if (key.codes[0] == -5) {
drawKeyBackground(R.drawable.btn_keyboard_key_delete, canvas, key);
}
}
private void drawKeyBackground(int drawableId, Canvas canvas, Key key) {
Drawable npd = (Drawable) mContext.getResources().getDrawable(
drawableId);
int[] drawableState = key.getCurrentDrawableState();
if (key.codes[0] != 0) {
npd.setState(drawableState);
}
npd.setBounds(key.x, key.y, key.x + key.width, key.y
+ key.height);
npd.draw(canvas);
}
public int getRightType() {
return this.rightType;
}
private void drawText(Canvas canvas, Key key) {
Rect bounds = new Rect();
Paint paint = new Paint();
paint.setTextAlign(Paint.Align.CENTER);
if (key.label!=null && ("I".equalsIgnoreCase(key.label.toString()) || "O".equalsIgnoreCase(key.label.toString()))) {
paint.setTextSize(50);
} else {
paint.setTextSize(44);
}
paint.setAntiAlias(true);
// paint.setTypeface(Typeface.DEFAULT_BOLD);
paint.setColor(Color.BLACK);
if (mKeyBoard.equals(KeyboardUtil.abcKeyboard)||mKeyBoard.equals(KeyboardUtil.abcKeyboardLast)) {
if (key.label != null) {
paint.setColor(mContext.getResources().getColor(R.color.color_3c3c3c));
paint.getTextBounds(key.label.toString(), 0, key.label.toString()
.length(), bounds);
canvas.drawText(key.label.toString(), key.x + (key.width / 2),
(key.y + key.height / 2) + bounds.height() / 2, paint);
}
} else if (mKeyBoard.equals(KeyboardUtil.symbolKeyboard)) {
paint.setColor(mContext.getResources().getColor(R.color.color_3c3c3c));
paint.getTextBounds(key.label.toString(), 0, key.label.toString()
.length(), bounds);
canvas.drawText(key.label.toString(), key.x + (key.width / 2),
(key.y + key.height / 2) + bounds.height() / 2, paint);
}
}
}
KeyboardUtil。 主要处理了输入键盘的判断。
public void onKey(int primaryCode, int[] keyCodes)方法中判断了输入的键盘产生的各种事件
package com.ziyeyouhu.library;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.inputmethodservice.Keyboard;
import android.inputmethodservice.Keyboard.Key;
import android.inputmethodservice.KeyboardView.OnKeyboardActionListener;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.InputType;
import android.text.Selection;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;
public class KeyboardUtil {
private Context mContext;
private int widthPixels;
private Activity mActivity;
private PpKeyBoardView keyboardView;
public static Keyboard abcKeyboard;// 字母键盘
public static Keyboard abcKeyboardLast; // 字母键盘最后一位
public static Keyboard symbolKeyboard;// 汉字键盘
public static Keyboard keyboard;//提供给keyboardView 进行画
public boolean isupper = false;// 是否大写
public boolean isShow = false;
InputFinishListener inputOver;
KeyBoardStateChangeListener keyBoardStateChangeListener;
private View layoutView;
private View keyBoardLayout;
// 开始输入的键盘状态设置
private static int inputType = 7;// 默认
public static final int INPUTTYPE_ABC = 6;// 一般的abc
public static final int INPUTTYPE_ABC_LAST = 8 ; // abc最后一位汉字的
public static final int INPUTTYPE_SYMBOL = 7;// 省份键盘
public static final int KEYBOARD_SHOW = 1;
public static final int KEYBOARD_HIDE = 2;
private EditText ed;
private Handler mHandler;
private Handler showHandler;
// private ScrollView sv_main;
private View root_view;
private int scrollTo = 0;
private KeyboardUtil mKeyboardUtil;
private TextView keyboard_tips_tv;
private static final float TIPS_MARGIN_W = 0.0407f;
private View inflaterView;
private ImageView mIVClose;
public static final int PLATE_GENERAL_length = 7;
public static final int PLATE_NEW_ENERGY_LENGTH = 8;
int editLength = PLATE_GENERAL_length;
private int editType;
/**
* 最新构造方法,现在都用这个
*
* @param ctx
* @param rootView rootView 需要是LinearLayout,以适应键盘
*/
public KeyboardUtil(Context ctx, RelativeLayout rootView, ScrollView scrollView) {
this.mContext = ctx;
this.mActivity = (Activity) mContext;
widthPixels = mContext.getResources().getDisplayMetrics().widthPixels;
initKeyBoardView(rootView);
initScrollHandler(rootView, scrollView);
mKeyboardUtil = this;
}
// 车牌位数 普通车7位 新能源车8位
public void setEditType(int editType) {
this.editType = editType;
if(editType == 0){
this.editLength = PLATE_GENERAL_length;
}else {
this.editLength = PLATE_NEW_ENERGY_LENGTH;
}
}
private void setEditLength(int editLength) {
this.editLength = editLength;
}
/**
* 弹框类,用这个
*
* @param view 是弹框的inflaterView
*/
public KeyboardUtil(View view, Context ctx, RelativeLayout root_View, ScrollView scrollView) {
this(ctx, root_View, scrollView);
this.inflaterView = view;
}
//设置监听事件
public void setInputOverListener(InputFinishListener listener) {
this.inputOver = listener;
}
public static Keyboard getKeyBoardType() {
return keyboard;
}
private void initKeyBoardView(RelativeLayout rootView) {
LayoutInflater inflater = LayoutInflater.from(mContext);
keyBoardLayout = inflater.inflate(R.layout.input, null);
keyBoardLayout.setVisibility(View.GONE);
keyBoardLayout.setBackgroundColor(mActivity.getResources().getColor(R.color.product_list_bac));
initLayoutHeight((LinearLayout) keyBoardLayout);
this.layoutView = keyBoardLayout;
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);//与父容器的左侧对齐
keyBoardLayout.setLayoutParams(lp);//设置布局参数
rootView.addView(keyBoardLayout);
if (keyBoardLayout != null && keyBoardLayout.getVisibility() == View.VISIBLE)
Log.d("KeyboardUtil", "visible");
}
public void initLayoutHeight(LinearLayout layoutView) {
LinearLayout.LayoutParams keyboard_layoutlLayoutParams = (LinearLayout.LayoutParams) layoutView
.getLayoutParams();
RelativeLayout TopLayout = (RelativeLayout) layoutView.findViewById(R.id.keyboard_view_top_rl);
mIVClose = (ImageView) layoutView.findViewById(R.id.iv_close);
mIVClose.setOnClickListener(new finishListener());
// keyboard_tips_tv = (TextView) layoutView.findViewById(R.id.keyboard_tips_tv);
// TextView keyboard_view_finish = (TextView) layoutView.findViewById(R.id.keyboard_view_finish);
// setMargins(keyboard_tips_tv, (int) (widthPixels * TIPS_MARGIN_W), 0, 0, 0);
// keyboard_tips_tv.setVisibility(View.VISIBLE);
// setMargins(keyboard_view_finish, 0, 0, (int) (widthPixels * TIPS_MARGIN_W), 0);
// keyboard_view_finish.setOnClickListener(new finishListener());
if (keyboard_layoutlLayoutParams == null) {
int height = (int) (mActivity.getResources().getDisplayMetrics().heightPixels * SIZE.KEYBOARY_H);
layoutView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
} else {
keyboard_layoutlLayoutParams.height = (int) (mActivity.getResources().getDisplayMetrics().heightPixels * SIZE.KEYBOARY_H);
}
LinearLayout.LayoutParams TopLayoutParams = (LinearLayout.LayoutParams) TopLayout
.getLayoutParams();
if (TopLayoutParams == null) {
int height = (int) (mActivity.getResources().getDisplayMetrics().heightPixels * SIZE.KEYBOARY_T_H);
TopLayout.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, height));
} else {
TopLayoutParams.height = (int) (mActivity.getResources().getDisplayMetrics().heightPixels * SIZE.KEYBOARY_T_H);
}
}
private void setMargins(View view, int left, int top, int right, int bottom) {
if (view.getLayoutParams() instanceof RelativeLayout.LayoutParams) {
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) view
.getLayoutParams();
layoutParams.setMargins(left, top, right, bottom);
} else if (view.getLayoutParams() instanceof LinearLayout.LayoutParams) {
LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) view
.getLayoutParams();
layoutParams.setMargins(left, top, right, bottom);
}
}
public boolean setKeyBoardCursorNew(EditText edit) {
this.ed = edit;
boolean flag = false;
InputMethodManager imm = (InputMethodManager) mContext
.getSystemService(Context.INPUT_METHOD_SERVICE);
boolean isOpen = imm.isActive();// isOpen若返回true,则表示输入法打开
if (isOpen) {
// ((InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(mActivity.getCurrentFocus().getWindowToken(),InputMethodManager.HIDE_NOT_ALWAYS);
if (imm.hideSoftInputFromWindow(edit.getWindowToken(), 0))
flag = true;
}
// act.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) {
edit.setInputType(InputType.TYPE_NULL);
} else {
Class<EditText> cls = EditText.class;
Method setShowSoftInputOnFocus;
try {
setShowSoftInputOnFocus = cls.getMethod(methodName,
boolean.class);
setShowSoftInputOnFocus.setAccessible(true);
setShowSoftInputOnFocus.invoke(edit, false);
} catch (NoSuchMethodException e) {
edit.setInputType(InputType.TYPE_NULL);
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
return flag;
}
public void hideSystemKeyBoard() {
InputMethodManager imm = (InputMethodManager) mContext
.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(keyBoardLayout.getWindowToken(), 0);
}
public void hideAllKeyBoard() {
hideSystemKeyBoard();
hideKeyboardLayout();
}
public boolean getKeyboardState() {
return this.isShow;
}
public EditText getEd() {
return ed;
}
//初始化滑动handler
@SuppressLint("HandlerLeak")
private void initScrollHandler(View rootView, ScrollView scrollView) {
// this.sv_main = scrollView;
this.root_view = rootView;
mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if (msg.what == ed.getId()) {
// if (sv_main != null)
// sv_main.smoothScrollTo(0, scrollTo);
}
}
};
}
//滑动监听
private void keyBoardScroll(final EditText editText, int scorllTo) {
this.scrollTo = scorllTo;
ViewTreeObserver vto_bighexagon = root_view.getViewTreeObserver();
vto_bighexagon.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
Message msg = new Message();
msg.what = editText.getId();
mHandler.sendMessageDelayed(msg, 500);
// // 防止多次促发
root_view.getViewTreeObserver()
.removeGlobalOnLayoutListener(this);
}
});
}
//设置一些不需要使用这个键盘的edittext,解决切换问题
public void setOtherEdittext(EditText... edittexts) {
for (EditText editText : edittexts) {
editText.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP) {
//防止没有隐藏键盘的情况出现
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
hideKeyboardLayout();
}
}, 300);
ed = (EditText) v;
hideKeyboardLayout();
}
return false;
}
});
}
}
class finishListener implements View.OnClickListener {
@Override
public void onClick(View v) {
hideKeyboardLayout();
}
}
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) {
if (ed == null)
return;
Editable editable = ed.getText();
// if (editable.length()>=20)
// return;
int start = ed.getSelectionStart();
int end = ed.getSelectionEnd();
String temp = editable.subSequence(0, start) + text.toString() + editable.subSequence(start, editable.length());
ed.setText(temp);
Editable etext = ed.getText();
Selection.setSelection(etext, start + 1);
}
@Override
public void onRelease(int primaryCode) {
if ((primaryCode == Keyboard.KEYCODE_SHIFT)) {
keyboardView.setPreviewEnabled(true);
}
}
@Override
public void onPress(int primaryCode) {
// if (inputType == KeyboardUtil.INPUTTYPE_NUM_ABC ||
// inputType == KeyboardUtil.INPUTTYPE_NUM ||
// inputType == KeyboardUtil.INPUTTYPE_NUM_POINT ||
// inputType == KeyboardUtil.INPUTTYPE_NUM_FINISH ||
// inputType == KeyboardUtil.INPUTTYPE_NUM_NEXT ||
// inputType == KeyboardUtil.INPUTTYPE_NUM_X) {
// keyboardView.setPreviewEnabled(false);
// return;
// }
if (primaryCode == Keyboard.KEYCODE_SHIFT
|| primaryCode == Keyboard.KEYCODE_DELETE
|| primaryCode == 123123
|| primaryCode == 456456
|| primaryCode == 789789
|| primaryCode == 32) {
keyboardView.setPreviewEnabled(false);
return;
}
keyboardView.setPreviewEnabled(true);
return;
}
@Override
public void onKey(int primaryCode, int[] keyCodes) {
//判定是否是中文的正则表达式 [\\u4e00-\\u9fa5]判断一个中文 [\\u4e00-\\u9fa5]+多个中文
String reg = "[\\u4e00-\\u9fa5]";
Editable editable = ed.getText();
int start = ed.getSelectionStart();
if (primaryCode == Keyboard.KEYCODE_CANCEL) {// 收起
hideKeyboardLayout();
if (inputOver != null)
inputOver.inputHasOver(primaryCode, ed);
} else if (primaryCode == Keyboard.KEYCODE_DELETE) {// 回退
if (editable != null && editable.length() > 0) {
if (start > 0) {
editable.delete(start - 1, start);
}
if((editable.toString().length()==editLength-2)){
isupper = false;
showKeyBoardLayout(ed, INPUTTYPE_ABC, -1);
}
if(start == 1){
isupper = false;
showKeyBoardLayout(ed, INPUTTYPE_SYMBOL, -1);
}
}
} else if (primaryCode == Keyboard.KEYCODE_SHIFT) {// 大小写切换
changeKey();
keyboardView.setKeyboard(abcKeyboard);
} else if (primaryCode == Keyboard.KEYCODE_DONE) {// 完成 and 下一个
if (keyboardView.getRightType() == 4) {
hideKeyboardLayout();
if (inputOver != null)
inputOver.inputHasOver(keyboardView.getRightType(), ed);
} else if (keyboardView.getRightType() == 5) {
// 下一个监听
if (inputOver != null)
inputOver.inputHasOver(keyboardView.getRightType(), ed);
}
} else if (primaryCode == 0) {
// 空白键
}else if (primaryCode == 456456) {//转换字母键盘
isupper = false;
if((editable.toString().length()>=editLength-1)){
showKeyBoardLayout(ed, INPUTTYPE_ABC_LAST, -1);
}else{
showKeyBoardLayout(ed, INPUTTYPE_ABC, -1);
}
} else if (primaryCode == 789789) {//转换字符省键盘
isupper = false;
showKeyBoardLayout(ed, INPUTTYPE_SYMBOL, -1);
} else {
// 判断如果有好几个字符,输入省份就修改第一个字符
if(start>0 && inputType==INPUTTYPE_SYMBOL){
// 有输入的文字 并且当前切换到了省份 输入汉字 修改第一个字符
// editable.insert(0, Character.toString((char) primaryCode));
if(primaryCode == -1110){
// 武警的车牌 WJ 的字符 WJ 87 74
if(!ed.getText().toString().startsWith("WJ")){
editable.replace(0,1,Character.toString((char) 87));
editable.insert(1,Character.toString((char) 74));
}
}else{
if(ed.getText().toString().startsWith("WJ")){
editable.replace(0,1,Character.toString((char) primaryCode));
editable.delete(1,2);
}else{
editable.replace(0,1,Character.toString((char) primaryCode));
}
}
return ;
}else{
if(editable.toString().length()>=editLength){
// 最大车牌数量
return ;
}
if(primaryCode == -1110){
// 武警的车牌 WJ 的字符 WJ 87 74
editable.insert(0,Character.toString((char) 87));
editable.insert(1,Character.toString((char) 74));
}else{
editable.insert(start, Character.toString((char) primaryCode));
}
}
if(editable.toString().length()==editLength-1){
showKeyBoardLayout(ed, INPUTTYPE_ABC_LAST, -1);
}
// 判断第一个字符是否是中文,是,则自动切换到数字软键盘
if (ed.getText().toString().matches(reg) || ed.getText().toString().equals("WJ")) {
showKeyBoardLayout(ed, INPUTTYPE_ABC, -1);
}
if(ed.getText().toString().startsWith("WJ")){
// WJ 开头的车是武警的车 位数8位
setEditLength(PLATE_NEW_ENERGY_LENGTH);
}else{
setEditType(editType);
}
}
}
};
/**
* 键盘大小写切换
*/
private void changeKey() {
List<Key> keylist = abcKeyboard.getKeys();
if (isupper) {// 大写切小写
isupper = false;
for (Key key : keylist) {
if (key.label != null && isword(key.label.toString())) {
key.label = key.label.toString().toLowerCase();
key.codes[0] = key.codes[0] + 32;
}
}
} else {// 小写切大写
isupper = true;
for (Key key : keylist) {
if (key.label != null && isword(key.label.toString())) {
key.label = key.label.toString().toUpperCase();
key.codes[0] = key.codes[0] - 32;
}
}
}
}
public void showKeyboard() {
if (keyboardView != null) {
keyboardView.setVisibility(View.GONE);
}
initInputType();
isShow = true;
keyboardView.setVisibility(View.VISIBLE);
}
private void initKeyBoard(int keyBoardViewID) {
mActivity = (Activity) mContext;
if (inflaterView != null) {
keyboardView = (PpKeyBoardView) inflaterView.findViewById(keyBoardViewID);
} else {
keyboardView = (PpKeyBoardView) mActivity
.findViewById(keyBoardViewID);
}
keyboardView.setEnabled(true);
keyboardView.setOnKeyboardActionListener(listener);
keyboardView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_MOVE) {
return true;
}
return false;
}
});
}
private Key getCodes(int i) {
return keyboardView.getKeyboard().getKeys().get(i);
}
private void initInputType() {
// if (inputType == INPUTTYPE_NUM) {
// // isnum = true;
// initKeyBoard(R.id.keyboard_view);
// keyboardView.setPreviewEnabled(false);
// numKeyboard = new Keyboard(mContext, R.xml.symbols);
// setMyKeyBoard(numKeyboard);
// } else if (inputType == INPUTTYPE_NUM_FINISH) {
// initKeyBoard(R.id.keyboard_view);
// keyboardView.setPreviewEnabled(false);
// numKeyboard = new Keyboard(mContext, R.xml.symbols_finish);
// setMyKeyBoard(numKeyboard);
// }
// else if (inputType == INPUTTYPE_NUM_POINT) {
// initKeyBoard(R.id.keyboard_view);
// keyboardView.setPreviewEnabled(false);
// numKeyboard = new Keyboard(mContext, R.xml.symbols_point);
// setMyKeyBoard(numKeyboard);
// }
// else if (inputType == INPUTTYPE_NUM_X) {
// initKeyBoard(R.id.keyboard_view);
// keyboardView.setPreviewEnabled(false);
// numKeyboard = new Keyboard(mContext, R.xml.symbols_x);
// setMyKeyBoard(numKeyboard);
// }
// else if (inputType == INPUTTYPE_NUM_NEXT) {
// initKeyBoard(R.id.keyboard_view);
// keyboardView.setPreviewEnabled(false);
// numKeyboard = new Keyboard(mContext, R.xml.symbols_next);
// setMyKeyBoard(numKeyboard);
// }
if (inputType == INPUTTYPE_ABC) {
// isnum = false;
initKeyBoard(R.id.keyboard_view_abc_sym);
keyboardView.setPreviewEnabled(true);
abcKeyboard = new Keyboard(mContext, R.xml.symbols_abc);
setMyKeyBoard(abcKeyboard);
} else if (inputType == INPUTTYPE_SYMBOL) {
initKeyBoard(R.id.keyboard_view_abc_sym);
keyboardView.setPreviewEnabled(true);
symbolKeyboard = new Keyboard(mContext, R.xml.symbols_symbol);
setMyKeyBoard(symbolKeyboard);
}else if(inputType == INPUTTYPE_ABC_LAST){
initKeyBoard(R.id.keyboard_view_abc_sym);
keyboardView.setPreviewEnabled(true);
abcKeyboardLast = new Keyboard(mContext, R.xml.symbols_abc_last);
setMyKeyBoard(abcKeyboardLast);
}
// else if (inputType == INPUTTYPE_NUM_ABC) {
// initKeyBoard(R.id.keyboard_view);
// keyboardView.setPreviewEnabled(false);
// numKeyboard = new Keyboard(mContext, R.xml.symbols_num_abc);
// setMyKeyBoard(numKeyboard);
// }
}
private void setMyKeyBoard(Keyboard newkeyboard) {
keyboard = newkeyboard;
keyboardView.setKeyboard(newkeyboard);
}
//新的隐藏方法
public void hideKeyboardLayout() {
if (getKeyboardState() == true) {
if (keyBoardLayout != null)
keyBoardLayout.setVisibility(View.GONE);
if (keyBoardStateChangeListener != null)
keyBoardStateChangeListener.KeyBoardStateChange(KEYBOARD_HIDE, ed);
isShow = false;
hideKeyboard();
ed = null;
}
}
/**
* @param editText
* @param keyBoardType 类型
* @param scrollTo 滑动到某个位置,可以是大于等于0的数,其他数不滑动
*/
//新的show方法
public void showKeyBoardLayout(final EditText editText, int keyBoardType, int scrollTo) {
if (editText.equals(ed) && getKeyboardState() == true && this.inputType == keyBoardType)
return;
this.inputType = keyBoardType;
this.scrollTo = scrollTo;
//TODO
if (keyBoardLayout != null && keyBoardLayout.getVisibility() == View.VISIBLE)
Log.d("KeyboardUtil", "visible");
if (setKeyBoardCursorNew(editText)) {
showHandler = new Handler();
showHandler.postDelayed(new Runnable() {
@Override
public void run() {
show(editText);
}
}, 400);
} else {
//直接显示
show(editText);
}
}
private void show(EditText editText) {
this.ed = editText;
if (keyBoardLayout != null)
keyBoardLayout.setVisibility(View.VISIBLE);
showKeyboard();
if (keyBoardStateChangeListener != null)
keyBoardStateChangeListener.KeyBoardStateChange(KEYBOARD_SHOW, editText);
//用于滑动
if (scrollTo >= 0) {
keyBoardScroll(editText, scrollTo);
}
}
private void hideKeyboard() {
isShow = false;
if (keyboardView != null) {
int visibility = keyboardView.getVisibility();
if (visibility == View.VISIBLE) {
keyboardView.setVisibility(View.INVISIBLE);
}
}
if (layoutView != null) {
layoutView.setVisibility(View.GONE);
}
}
private boolean isword(String str) {
String wordstr = "abcdefghijklmnopqrstuvwxyz";
if (wordstr.indexOf(str.toLowerCase()) > -1) {
return true;
}
return false;
}
/**
* @description:TODO 输入监听
*/
public interface InputFinishListener {
public void inputHasOver(int onclickType, EditText editText);
}
/**
* 监听键盘变化
*/
public interface KeyBoardStateChangeListener {
public void KeyBoardStateChange(int state, EditText editText);
}
public void setKeyBoardStateChangeListener(KeyBoardStateChangeListener listener) {
this.keyBoardStateChangeListener = listener;
}
}
然后就是xml的键盘:
省份的xml,普通字符的xml,和最后一位包含汉字的xml
<?xml version="1.0" encoding="UTF-8"?>
<Keyboard
android:keyWidth="9.5%p"
android:keyHeight="6.4%p"
android:horizontalGap="1.2%p"
android:verticalGap="1.55%p"
xmlns:android="http://schemas.android.com/apk/res/android">
<Row android:verticalGap="1%p">
<Key android:codes="20140" android:keyLabel="京"
android:horizontalGap="0.41%p" android:keyWidth="9.5%p"
android:keyEdgeFlags="left" />
<Key android:codes="27941" android:keyLabel="津"
android:horizontalGap="1.71%p" android:keyWidth="9.5%p" />
<Key android:codes="20864" android:keyLabel="冀"
android:horizontalGap="1.71%p" android:keyWidth="9.5%p" />
<Key android:codes="40065" android:keyLabel="鲁"
android:horizontalGap="1.71%p" android:keyWidth="9.5%p" />
<Key android:codes="26187" android:keyLabel="晋"
android:horizontalGap="1.71%p" android:keyWidth="9.5%p" />
<Key android:codes="33945" android:keyLabel="蒙"
android:horizontalGap="1.71%p" android:keyWidth="9.5%p" />
<Key android:codes="36797" android:keyLabel="辽"
android:horizontalGap="1.71%p" android:keyWidth="9.5%p" />
<Key android:codes="21513" android:keyLabel="吉"
android:horizontalGap="1.71%p" android:keyWidth="9.5%p" />
<Key android:codes="40657" android:keyLabel="黑"
android:horizontalGap="1.71%p" android:keyWidth="9.5%p"
android:keyEdgeFlags="right"/>
</Row>
<Row android:verticalGap="1%p">
<Key android:codes="27818" android:keyLabel="沪"
android:horizontalGap="0.41%p" android:keyWidth="9.5%p"
android:keyEdgeFlags="left"/>
<Key android:codes="33487" android:keyLabel="苏"
android:horizontalGap="1.71%p" android:keyWidth="9.5%p" />
<Key android:codes="27993" android:keyLabel="浙"
android:horizontalGap="1.71%p" android:keyWidth="9.5%p" />
<Key android:codes="30358" android:keyLabel="皖"
android:horizontalGap="1.71%p" android:keyWidth="9.5%p" />
<Key android:codes="38397" android:keyLabel="闽"
android:horizontalGap="1.71%p" android:keyWidth="9.5%p" />
<Key android:codes="36195" android:keyLabel="赣"
android:horizontalGap="1.71%p" android:keyWidth="9.5%p" />
<Key android:codes="35947" android:keyLabel="豫"
android:horizontalGap="1.71%p" android:keyWidth="9.5%p" />
<Key android:codes="37122" android:keyLabel="鄂"
android:horizontalGap="1.71%p" android:keyWidth="9.5%p" />
<Key android:codes="28248" android:keyLabel="湘"
android:horizontalGap="1.71%p" android:keyWidth="9.5%p" android:keyEdgeFlags="right" />
</Row>
<Row android:verticalGap="1%p">
<Key android:codes="26690" android:keyLabel="桂" android:keyEdgeFlags="left"
android:horizontalGap="0.41%p" android:keyWidth="9.5%p"
/>
<Key android:codes="28189" android:keyLabel="渝"
android:horizontalGap="1.71%p" android:keyWidth="9.5%p"
/>
<Key android:codes="31908" android:keyLabel="粤"
android:horizontalGap="1.71%p" android:keyWidth="9.5%p" />
<Key android:codes="24029" android:keyLabel="川"
android:horizontalGap="1.71%p" android:keyWidth="9.5%p" />
<Key android:codes="36149" android:keyLabel="贵"
android:horizontalGap="1.71%p" android:keyWidth="9.5%p" />
<Key android:codes="20113" android:keyLabel="云"
android:horizontalGap="1.71%p" android:keyWidth="9.5%p" />
<Key android:codes="34255" android:keyLabel="藏"
android:horizontalGap="1.71%p" android:keyWidth="9.5%p" />
<Key android:codes="38485" android:keyLabel="陕"
android:horizontalGap="1.71%p" android:keyWidth="9.5%p" />
<Key android:codes="29976" android:keyLabel="甘" android:keyEdgeFlags="right"
android:horizontalGap="1.71%p" android:keyWidth="9.5%p" />
</Row>
<Row >
<Key android:codes="456456"
android:keyEdgeFlags="left" android:keyLabel="@string/char_abc"
android:horizontalGap="0.41%p" android:keyWidth="14.7%p"
android:isModifier="true" android:isSticky="true" />
<Key android:codes="38738" android:keyLabel="青"
android:horizontalGap="0.41%p" android:keyWidth="9.5%p"/>
<Key android:codes="29756" android:keyLabel="琼"
android:horizontalGap="0.41%p" android:keyWidth="9.5%p" />
<Key android:codes="26032" android:keyLabel="新"
android:horizontalGap="0.41%p" android:keyWidth="9.5%p"
/>
<Key android:codes="23425" android:keyLabel="宁"
android:horizontalGap="0.41%p" android:keyWidth="9.5%p" />
<Key android:codes="21488" android:keyLabel="台"
android:horizontalGap="0.41%p" android:keyWidth="9.5%p"/>
<Key android:codes="20351" android:keyLabel="使"
android:horizontalGap="0.41%p" android:keyWidth="9.5%p"/>
<Key android:codes="-1110" android:keyLabel="WJ"
android:horizontalGap="0.41%p" android:keyWidth="9.5%p"/>
<Key android:codes="-5" android:horizontalGap="0.41%p" android:keyWidth="14.7%p"
android:keyEdgeFlags="right" android:isRepeatable="false" />
<!-- <Key android:codes="-3" android:isRepeatable="false" android:horizontalGap="2%p" android:keyWidth="13%p" android:keyEdgeFlags="right"
/>-->
</Row>
</Keyboard>
<?xml version="1.0" encoding="UTF-8"?>
<Keyboard android:keyWidth="9.5%p"
android:keyHeight="6.4%p"
android:horizontalGap="1.2%p"
android:verticalGap="1.55%p"
xmlns:android="http://schemas.android.com/apk/res/android">
<Row android:verticalGap="1%p">
<Key android:codes="49" android:keyLabel="1"
android:horizontalGap="0.25%p" android:keyWidth="9.5%p"
android:keyEdgeFlags="left" />
<Key android:codes="50" android:keyLabel="2"
android:horizontalGap="0.5%p" android:keyWidth="9.5%p" />
<Key android:codes="51" android:keyLabel="3"
android:horizontalGap="0.5%p" android:keyWidth="9.5%p" />
<Key android:codes="52" android:keyLabel="4"
android:horizontalGap="0.5%p" android:keyWidth="9.5%p" />
<Key android:codes="53" android:keyLabel="5"
android:horizontalGap="0.5%p" android:keyWidth="9.5%p" />
<Key android:codes="54" android:keyLabel="6"
android:horizontalGap="0.5%p" android:keyWidth="9.5%p" />
<Key android:codes="55" android:keyLabel="7"
android:horizontalGap="0.5%p" android:keyWidth="9.5%p" />
<Key android:codes="56" android:keyLabel="8"
android:horizontalGap="0.5%p" android:keyWidth="9.5%p" />
<Key android:codes="57" android:keyLabel="9"
android:horizontalGap="0.5%p" android:keyWidth="9.5%p" />
<Key android:codes="48" android:keyLabel="0"
android:horizontalGap="0.5%p" android:keyWidth="9.5%p"
android:keyEdgeFlags="right"/>
</Row>
<Row android:verticalGap="1%p">
<Key android:codes="81" android:keyLabel="Q"
android:horizontalGap="0.25%p" android:keyWidth="9.5%p"
android:keyEdgeFlags="left" />
<Key android:codes="87" android:keyLabel="W"
android:horizontalGap="0.5%p" android:keyWidth="9.5%p" />
<Key android:codes="69" android:keyLabel="E"
android:horizontalGap="0.5%p" android:keyWidth="9.5%p" />
<Key android:codes="82" android:keyLabel="R"
android:horizontalGap="0.5%p" android:keyWidth="9.5%p" />
<Key android:codes="84" android:keyLabel="T"
android:horizontalGap="0.5%p" android:keyWidth="9.5%p" />
<Key android:codes="89" android:keyLabel="Y"
android:horizontalGap="0.5%p" android:keyWidth="9.5%p" />
<Key android:codes="85" android:keyLabel="U"
android:horizontalGap="0.5%p" android:keyWidth="9.5%p" />
<!-- <Key android:codes="73" android:keyLabel="I"
android:horizontalGap="2%p" android:keyWidth="8%p" />-->
<Key android:codes="49" android:keyLabel="I"
android:horizontalGap="0.5%p" android:keyWidth="9.5%p" />
<!-- <Key android:codes="79" android:keyLabel="O"
android:horizontalGap="2%p" android:keyWidth="8%p" />-->
<Key android:codes="48" android:keyLabel="O"
android:horizontalGap="0.5%p" android:keyWidth="9.5%p" />
<Key android:codes="80" android:keyLabel="P"
android:horizontalGap="0.5%p" android:keyWidth="9.5%p"
android:keyEdgeFlags="right" />
</Row>
<Row android:verticalGap="1%p">
<Key android:codes="65" android:keyLabel="A"
android:horizontalGap="5.25%p" android:keyWidth="9.5%p"
android:keyEdgeFlags="left"/>
<Key android:codes="83" android:keyLabel="S"
android:horizontalGap="0.5%p" android:keyWidth="9.5%p" />
<Key android:codes="68" android:keyLabel="D"
android:horizontalGap="0.5%p" android:keyWidth="9.5%p" />
<Key android:codes="70" android:keyLabel="F"
android:horizontalGap="0.5%p" android:keyWidth="9.5%p" />
<Key android:codes="71" android:keyLabel="G"
android:horizontalGap="0.5%p" android:keyWidth="9.5%p" />
<Key android:codes="72" android:keyLabel="H"
android:horizontalGap="0.5%p" android:keyWidth="9.5%p" />
<Key android:codes="74" android:keyLabel="J"
android:horizontalGap="0.5%p" android:keyWidth="9.5%p" />
<Key android:codes="75" android:keyLabel="K"
android:horizontalGap="0.5%p" android:keyWidth="9.5%p" />
<Key android:codes="76" android:keyLabel="L"
android:horizontalGap="0.5%p" android:keyWidth="9.5%p"
android:keyEdgeFlags="right" />
</Row>
<Row >
<Key android:codes="789789"
android:keyEdgeFlags="left" android:keyLabel="省份"
android:horizontalGap="0.25%p" android:keyWidth="14.5%p"
android:isModifier="true" android:isSticky="true" />
<Key android:codes="90" android:keyLabel="Z"
android:horizontalGap="0.5%p" android:keyWidth="9.5%p"/>
<Key android:codes="88" android:keyLabel="X"
android:horizontalGap="0.5%p" android:keyWidth="9.5%p" />
<Key android:codes="67" android:keyLabel="C"
android:horizontalGap="0.5%p" android:keyWidth="9.5%p" />
<Key android:codes="86" android:keyLabel="V"
android:horizontalGap="0.5%p" android:keyWidth="9.5%p" />
<Key android:codes="66" android:keyLabel="B"
android:horizontalGap="0.5%p" android:keyWidth="9.5%p" />
<Key android:codes="78" android:keyLabel="N"
android:horizontalGap="0.5%p" android:keyWidth="9.5%p"/>
<Key android:codes="77" android:keyLabel="M"
android:horizontalGap="0.5%p" android:keyWidth="9.5%p"/>
<Key android:keyWidth="14.5%p" android:codes="-5" android:horizontalGap="0.5%p"
android:keyEdgeFlags="right" android:isRepeatable="false"/>
</Row>
</Keyboard>
<?xml version="1.0" encoding="UTF-8"?>
<Keyboard android:keyWidth="9.5%p"
android:keyHeight="6.4%p"
android:horizontalGap="1.2%p"
android:verticalGap="1.55%p"
xmlns:android="http://schemas.android.com/apk/res/android">
<Row android:verticalGap="1%p">
<Key android:codes="49" android:keyLabel="1"
android:horizontalGap="0.25%p" android:keyWidth="9.5%p"
android:keyEdgeFlags="left" />
<Key android:codes="50" android:keyLabel="2"
android:horizontalGap="0.5%p" android:keyWidth="9.5%p" />
<Key android:codes="51" android:keyLabel="3"
android:horizontalGap="0.5%p" android:keyWidth="9.5%p" />
<Key android:codes="52" android:keyLabel="4"
android:horizontalGap="0.5%p" android:keyWidth="9.5%p" />
<Key android:codes="53" android:keyLabel="5"
android:horizontalGap="0.5%p" android:keyWidth="9.5%p" />
<Key android:codes="54" android:keyLabel="6"
android:horizontalGap="0.5%p" android:keyWidth="9.5%p" />
<Key android:codes="55" android:keyLabel="7"
android:horizontalGap="0.5%p" android:keyWidth="9.5%p" />
<Key android:codes="56" android:keyLabel="8"
android:horizontalGap="0.5%p" android:keyWidth="9.5%p" />
<Key android:codes="57" android:keyLabel="9"
android:horizontalGap="0.5%p" android:keyWidth="9.5%p" />
<Key android:codes="48" android:keyLabel="0"
android:horizontalGap="0.5%p" android:keyWidth="9.5%p"
android:keyEdgeFlags="right"/>
</Row>
<Row android:verticalGap="1%p">
<Key android:codes="81" android:keyLabel="Q"
android:horizontalGap="0.25%p" android:keyWidth="9.5%p"
android:keyEdgeFlags="left" />
<Key android:codes="87" android:keyLabel="W"
android:horizontalGap="0.5%p" android:keyWidth="9.5%p" />
<Key android:codes="69" android:keyLabel="E"
android:horizontalGap="0.5%p" android:keyWidth="9.5%p" />
<Key android:codes="82" android:keyLabel="R"
android:horizontalGap="0.5%p" android:keyWidth="9.5%p" />
<Key android:codes="84" android:keyLabel="T"
android:horizontalGap="0.5%p" android:keyWidth="9.5%p" />
<Key android:codes="89" android:keyLabel="Y"
android:horizontalGap="0.5%p" android:keyWidth="9.5%p" />
<Key android:codes="85" android:keyLabel="U"
android:horizontalGap="0.5%p" android:keyWidth="9.5%p" />
<!-- <Key android:codes="73" android:keyLabel="I"
android:horizontalGap="2%p" android:keyWidth="8%p" />-->
<Key android:codes="49" android:keyLabel="I"
android:horizontalGap="0.5%p" android:keyWidth="9.5%p" />
<!-- <Key android:codes="79" android:keyLabel="O"
android:horizontalGap="2%p" android:keyWidth="8%p" />-->
<Key android:codes="48" android:keyLabel="O"
android:horizontalGap="0.5%p" android:keyWidth="9.5%p" />
<Key android:codes="80" android:keyLabel="P"
android:horizontalGap="0.5%p" android:keyWidth="9.5%p"
android:keyEdgeFlags="right" />
</Row>
<Row android:verticalGap="1%p">
<Key android:codes="65" android:keyLabel="A"
android:horizontalGap="5.25%p" android:keyWidth="9.5%p"
android:keyEdgeFlags="left"/>
<Key android:codes="83" android:keyLabel="S"
android:horizontalGap="0.5%p" android:keyWidth="9.5%p" />
<Key android:codes="68" android:keyLabel="D"
android:horizontalGap="0.5%p" android:keyWidth="9.5%p" />
<Key android:codes="70" android:keyLabel="F"
android:horizontalGap="0.5%p" android:keyWidth="9.5%p" />
<Key android:codes="71" android:keyLabel="G"
android:horizontalGap="0.5%p" android:keyWidth="9.5%p" />
<Key android:codes="72" android:keyLabel="H"
android:horizontalGap="0.5%p" android:keyWidth="9.5%p" />
<Key android:codes="74" android:keyLabel="J"
android:horizontalGap="0.5%p" android:keyWidth="9.5%p" />
<Key android:codes="75" android:keyLabel="K"
android:horizontalGap="0.5%p" android:keyWidth="9.5%p" />
<Key android:codes="76" android:keyLabel="L"
android:horizontalGap="0.5%p" android:keyWidth="9.5%p"
android:keyEdgeFlags="right" />
</Row>
<Row android:verticalGap="1%p">
<Key android:codes="90" android:keyLabel="Z" android:keyEdgeFlags="left"
android:horizontalGap="10.25%p" android:keyWidth="9.5%p"/>
<Key android:codes="88" android:keyLabel="X"
android:horizontalGap="0.5%p" android:keyWidth="9.5%p" />
<Key android:codes="67" android:keyLabel="C"
android:horizontalGap="0.5%p" android:keyWidth="9.5%p" />
<Key android:codes="86" android:keyLabel="V"
android:horizontalGap="0.5%p" android:keyWidth="9.5%p" />
<Key android:codes="66" android:keyLabel="B"
android:horizontalGap="0.5%p" android:keyWidth="9.5%p" />
<Key android:codes="78" android:keyLabel="N"
android:horizontalGap="0.5%p" android:keyWidth="9.5%p"/>
<Key android:codes="77" android:keyLabel="M"
android:horizontalGap="0.5%p" android:keyWidth="9.5%p"/>
<Key android:codes="35686" android:keyLabel="警" android:keyEdgeFlags="right"
android:horizontalGap="0.5%p" android:keyWidth="9.5%p"/>
</Row>
<Row >
<Key android:codes="789789"
android:keyEdgeFlags="left" android:keyLabel="省份"
android:horizontalGap="0.25%p" android:keyWidth="14.5%p"
android:isModifier="true" android:isSticky="true" />
<Key android:codes="23398" android:keyLabel="学"
android:horizontalGap="0.5%p" android:keyWidth="9.5%p"/>
<Key android:codes="25346" android:keyLabel="挂"
android:horizontalGap="0.5%p" android:keyWidth="9.5%p" />
<Key android:codes="36229" android:keyLabel="超"
android:horizontalGap="0.5%p" android:keyWidth="9.5%p" />
<Key android:codes="20351" android:keyLabel="使"
android:horizontalGap="0.5%p" android:keyWidth="9.5%p" />
<Key android:codes="39046" android:keyLabel="领"
android:horizontalGap="0.5%p" android:keyWidth="9.5%p" />
<Key android:codes="28207" android:keyLabel="港"
android:horizontalGap="0.5%p" android:keyWidth="9.5%p"/>
<Key android:codes="28595" android:keyLabel="澳"
android:horizontalGap="0.5%p" android:keyWidth="9.5%p"/>
<Key android:keyWidth="14.5%p" android:codes="-5" android:horizontalGap="0.5%p"
android:keyEdgeFlags="right" android:isRepeatable="false"/>
</Row>
</Keyboard>
然后是在MainActivity中的简单使用:
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.KeyEvent;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import com.ziyeyouhu.library.KeyboardTouchListener;
import com.ziyeyouhu.library.KeyboardUtil;
public class MainActivity extends AppCompatActivity {
private RelativeLayout rootView;
private ScrollView scrollView;
private EditText normalEd;
private EditText specialEd;
private KeyboardUtil keyboardUtil;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rootView = (RelativeLayout) findViewById(R.id.root_view);
// scrollView = (ScrollView) findViewById(R.id.sv_main);
normalEd = (EditText) findViewById(R.id.normal_ed);
specialEd = (EditText) findViewById(R.id.special_ed);
initMoveKeyBoard();
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0 ) {
if(keyboardUtil.isShow){
keyboardUtil.hideSystemKeyBoard();
keyboardUtil.hideAllKeyBoard();
keyboardUtil.hideKeyboardLayout();
}else {
return super.onKeyDown(keyCode, event);
}
return false;
} else
return super.onKeyDown(keyCode, event);
}
private void initMoveKeyBoard() {
keyboardUtil = new KeyboardUtil(this, rootView, scrollView);
keyboardUtil.setOtherEdittext(normalEd);
// 车牌位数 普通车7位 新能源车8位
keyboardUtil.setEditType(0);
// monitor the KeyBarod state
keyboardUtil.setKeyBoardStateChangeListener(new KeyBoardStateListener());
// monitor the finish or next Key
keyboardUtil.setInputOverListener(new inputOverListener());
specialEd.setOnTouchListener(new KeyboardTouchListener(keyboardUtil, KeyboardUtil.INPUTTYPE_SYMBOL, -1));
}
class KeyBoardStateListener implements KeyboardUtil.KeyBoardStateChangeListener {
@Override
public void KeyBoardStateChange(int state, EditText editText) {
System.out.println("state" + state);
System.out.println("editText" + editText.getText().toString());
}
}
class inputOverListener implements KeyboardUtil.InputFinishListener {
@Override
public void inputHasOver(int onclickType, EditText editText) {
System.out.println("onclickType" + onclickType);
System.out.println("editText" + editText.getText().toString());
}
}
}
4.相关属性
android:keyBackground=”@drawable/selector_key”
* 按键的背景颜色
android:shadowColor=”#FFFFFF”
android:shadowRadius=”0.0”
* 不加这两个属性 文字会出现模糊
android:keyTextColor=”#000”
* 字体颜色
android:keyTextSize=”18sp”
* 字体大小
android:keyIcon=”@drawable/ic_delete”
* 按键上的图标
android:codes=”20140”
* 输出的内容 对照ASCII表
android:keyLabel=”京”
* 按键上显示的内容
android:horizontalGap=”0px”
* 水平方向的间隙
android:verticalGap=”0px”
* 垂直反向的间隙
android:keyEdgeFlags=”right”
* 按键的对齐方式
下面是代码下载地址:https://download.csdn.net/download/u010833696/10842425
欢迎大家下载,不好意思 我实在没用积分了 ,所以就要了积分,大脚见谅