今天在掘金上看了一篇文章,实现自定义软键盘,发现其实实现方式比较简单,不需要改动系统api,只是单纯的加载自己的键盘布局,隐藏系统弹出的键盘,实现数字错位,安全输入软键盘,记录一下实现过程用于总结
github地址: https://github.com/fushuangdage/CustomView
<Keyboard
xmlns:android="http://schemas.android.com/apk/res/android"
android:horizontalGap="0px"
android:keyHeight="9%p"
android:keyWidth="25%p"
android:verticalGap="0px"
>
<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="-5"
android:isRepeatable="true"
android:keyEdgeFlags="right"
android:keyHeight="18%p"
android:keyIcon="@drawable/icon_delete_32dp"/>
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"/>
<Key
android:codes="-4"
android:keyEdgeFlags="right"
android:keyHeight="18%p"
android:keyLabel="确定"
android:keyIcon="@drawable/icon_enter_32dp"/>
Row>
<Row>
<Key
android:codes="46"
android:keyLabel="."/>
<Key
android:codes="48"
android:keyLabel="0"/>
<Key
android:codes="-3"
android:keyIcon="@drawable/icon_hide_keyboard"/>
Row>
Keyboard>
首先编写关于自定义布局文件.键盘布局在xml定义
继承KeyboardView 画自己的键盘输入面板,可在ondraw方法中对默认的绘制放置覆盖绘制
package com.example.admin.customview;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.inputmethodservice.Keyboard;
import android.inputmethodservice.KeyboardView;
import android.util.AttributeSet;
import java.lang.reflect.Field;
import java.util.List;
/**
* Created by fushuang on 2017/8/22.
*/
public class MykeyBoardView extends KeyboardView {
private Context context;
public MykeyBoardView(Context context, AttributeSet attrs) {
super(context, attrs);
this.context = context;
}
public MykeyBoardView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
this.context=context;
}
@Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
Keyboard keyboard = getKeyboard();
List.Key> keys=null;
if (keyboard != null) {
keys = keyboard.getKeys();
for (Keyboard.Key key : keys) {
if (key.codes[0]==-4) {
drawKeyBackground(R.drawable.bg_keyboardview_yes,canvas,key);
drawText(canvas,key);
}
}
}
}
private void drawText(Canvas canvas, Keyboard.Key key) {
Rect bounds = new Rect();
Paint paint = new Paint();
paint.setTextAlign(Paint.Align.CENTER);
paint.setAntiAlias(true);
paint.setColor(Color.WHITE);
if (key.label != null) {
String label = key.label.toString();
Field field;
if (label.length() > 1 && key.codes.length < 2) {
int labelTextSize = 0;
try {
field = KeyboardView.class.getDeclaredField("mLabelTextSize");
field.setAccessible(true);
labelTextSize = (int) field.get(this);
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
paint.setTextSize(labelTextSize);
paint.setTypeface(Typeface.DEFAULT_BOLD);
} else {
int keyTextSize = 0;
try {
field = KeyboardView.class.getDeclaredField("mLabelTextSize");
field.setAccessible(true);
keyTextSize = (int) field.get(this);
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
paint.setTextSize(keyTextSize);
paint.setTypeface(Typeface.DEFAULT);
}
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 (key.icon != null) {
key.icon.setBounds(key.x + (key.width - key.icon.getIntrinsicWidth()) / 2, key.y + (key.height - key.icon.getIntrinsicHeight()) / 2,
key.x + (key.width - key.icon.getIntrinsicWidth()) / 2 + key.icon.getIntrinsicWidth(), key.y + (key.height - key.icon.getIntrinsicHeight()) / 2 + key.icon.getIntrinsicHeight());
key.icon.draw(canvas);
}
}
private void drawKeyBackground(int id, Canvas canvas, Keyboard.Key key) {
Drawable drawable = context.getResources().getDrawable(id);
int[] drawableState = key.getCurrentDrawableState();
if (key.codes[0]!=0) {
drawable.setState(drawableState);
}
drawable.setBounds(key.x,key.y, key.x+key.width,key.y+key.height);
drawable.draw(canvas);
}
}
KeyboardUtil 中主要实现当点输入文本框输入的隐藏软键盘,显示自定义键盘的逻辑
package com.example.admin.customview;
import android.app.Activity;
import android.content.Context;
import android.inputmethodservice.Keyboard;
import android.inputmethodservice.KeyboardView;
import android.os.Build;
import android.text.Editable;
import android.text.InputType;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.Toast;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;
/**
* Created by fushuang on 2017/8/22.
*/
public class KeyboardUtil implements KeyboardView.OnKeyboardActionListener {
private final MykeyBoardView mKeyBoardView;
private Keyboard mKeyboder;
private EditText mEditText;
private Activity mActivity;
public KeyboardUtil(Activity mActivity) {
this.mActivity = mActivity;
mKeyboder = new Keyboard(mActivity,R.xml.keyboardnumber);
mKeyBoardView = ((MykeyBoardView) mActivity.findViewById(R.id.keyboard_view));
}
public void attachTo(EditText editText){
mEditText = editText;
hideSystemSofeKeyboard(mActivity,editText);
showSoftKeyBoard();
}
private boolean isNumber(String str) {
String wordstr = "0123456789";
return wordstr.contains(str);
}
private void showSoftKeyBoard() {
// mKeyBoardView.setKeyboard(mKeyboder);
List keys = mKeyboder.getKeys();
List newkeyList = new ArrayList();
for (Keyboard.Key key : keys) {
if (key.label!=null && isNumber(key.label.toString())) {
newkeyList.add(key);
}
}
int count = newkeyList.size();
LinkedList temp=new LinkedList();
for (int i = 0; i < count; i++) {
temp.add(new KeyModel(48 + i, i + ""));
}
Random random = new Random();
for (int i = 0; i < count; i++) {
int index = random.nextInt(count - i);
KeyModel keyModel = temp.get(index);
newkeyList.get(i).label=keyModel.getLable();
newkeyList.get(i).codes[0]=keyModel.getCode();
temp.remove(index);
}
mKeyBoardView.setKeyboard(mKeyboder);
mKeyBoardView.setEnabled(true);
mKeyBoardView.setPreviewEnabled(false);
mKeyBoardView.setVisibility(View.VISIBLE);
mKeyBoardView.setOnKeyboardActionListener(this);
}
/**
* 隐藏系统键盘
*
* @param editText
*/
public static void hideSystemSofeKeyboard(Context context, EditText editText) {
int sdkInt = Build.VERSION.SDK_INT;
if (sdkInt >= 11) {
try {
Class cls = EditText.class;
Method setShowSoftInputOnFocus;
setShowSoftInputOnFocus = cls.getMethod("setShowSoftInputOnFocus", boolean.class);
setShowSoftInputOnFocus.setAccessible(true);
setShowSoftInputOnFocus.invoke(editText, false);
} catch (SecurityException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
} else {
editText.setInputType(InputType.TYPE_NULL);
}
// 如果软键盘已经显示,则隐藏
InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
}
@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 start = mEditText.getSelectionStart();
if (primaryCode == Keyboard.KEYCODE_DELETE) {// 回退
if (editable != null && editable.length() > 0) {
if (start > 0) {
editable.delete(start - 1, start);
}
}
} else if (primaryCode == Keyboard.KEYCODE_CANCEL) {// 隐藏键盘
hideKeyboard();
// if (mOnCancelClick != null) {
// mOnCancelClick.onCancellClick();
// }
} else if (primaryCode == Keyboard.KEYCODE_DONE) {// 隐藏键盘
hideKeyboard();
// if (mOnOkClick != null) {
// mOnOkClick.onOkClick();
// }
} else {
editable.insert(start, Character.toString((char) primaryCode));
}
}
private void hideKeyboard() {
mKeyBoardView.setVisibility(View.GONE);
}
@Override
public void onText(CharSequence text) {
}
@Override
public void swipeLeft() {
Toast.makeText(mActivity, "swipeLeft", Toast.LENGTH_SHORT).show();
}
@Override
public void swipeRight() {
Toast.makeText(mActivity, "swipeRight", Toast.LENGTH_SHORT).show();
}
@Override
public void swipeDown() {
Toast.makeText(mActivity, "swipeDown", Toast.LENGTH_SHORT).show();
}
@Override
public void swipeUp() {
Toast.makeText(mActivity, "swipeUp", Toast.LENGTH_SHORT).show();
}
}
只是照着网上代码自己实现了一遍,感觉只是有些方法不太常用.其余的跟自定义控件没啥区别