本篇博客将实现一个自定义软键盘实现数字录入的小Demo,也是为之后可能遇到的输入手机号,支付键盘等做一些知识上的储备,也是希望能够帮到处于学习的同学,在写的不对的地方希望各位能够指出来。含有demo
第一步设置 keyboard对应的xml文件,实现键盘值的排列。在res 资源文件下新建xml文件夹,新增keyboard_xml 如下内容。
具体的宽高也是可以自行去进行调整的。这里需要注意code值的赋予,因为在后面点击事件包括view绘制都用到该数值。
第二步自定义布局文件View,如下内容所示,核心工作就是绘制背景与内容。
public class CustomNumKeyboardView
extends KeyboardView
{
private Context mContext;
private Keyboard mKeyboard;
public CustomNumKeyboardView(Context paramContext, AttributeSet paramAttributeSet)
{
super(paramContext, paramAttributeSet);
this.mContext = paramContext;
}
public CustomNumKeyboardView(Context paramContext, AttributeSet paramAttributeSet, int paramInt)
{
super(paramContext, paramAttributeSet, paramInt);
this.mContext = paramContext;
}
private void drawIcon(Canvas paramCanvas, Keyboard.Key paramKey)
{
if (paramKey.icon != null)
{
paramKey.icon.setBounds(
paramKey.x + (paramKey.width - paramKey.icon.getIntrinsicWidth()) / 2,
paramKey.y + (paramKey.height - paramKey.icon.getIntrinsicHeight()) / 2,
paramKey.x + (paramKey.width - paramKey.icon.getIntrinsicWidth()) / 2 + paramKey.icon.getIntrinsicWidth(),
paramKey.y + (paramKey.height - paramKey.icon.getIntrinsicHeight()) / 2 + paramKey.icon.getIntrinsicHeight());
paramKey.icon.draw(paramCanvas);
}
}
// 绘制背景
private void drawKeyBackground(int paramInt, Canvas paramCanvas, Keyboard.Key paramKey)
{
Drawable localDrawable = this.mContext.getResources().getDrawable(paramInt);
int[] arrayOfInt = paramKey.getCurrentDrawableState();
if (paramKey.codes[0] != 0) {
localDrawable.setState(arrayOfInt);
}
localDrawable.setBounds(paramKey.x, paramKey.y,
paramKey.x + paramKey.width,
paramKey.y + paramKey.height);
localDrawable.draw(paramCanvas);
}
// 绘制文字
private void drawText(Canvas paramCanvas, Keyboard.Key paramKey, float paramFloat, int paramInt, Typeface paramTypeface)
{
Rect localRect = new Rect();
Paint localPaint = new Paint();
localPaint.setTextAlign(Paint.Align.CENTER);
localPaint.setAntiAlias(true);
localPaint.setColor(paramInt);
if (paramKey.label != null)
{
// 字体大小
localPaint.setTextSize(paramFloat);
localPaint.setTypeface(paramTypeface);
localPaint.getTextBounds(paramKey.label.toString(), 0, paramKey.label.toString().length(), localRect);
paramFloat = paramKey.x + paramKey.width / 2;
paramInt = paramKey.y;
int i = paramKey.height / 2;
paramCanvas.drawText(paramKey.label.toString(), paramFloat, localRect.height() / 2 + (paramInt + i), localPaint);
}
}
/*
key值对应关系
public static final int KEYCODE_SHIFT = -1;
public static final int KEYCODE_MODE_CHANGE = -2;
public static final int KEYCODE_CANCEL = -3;
public static final int KEYCODE_DONE = -4;
public static final int KEYCODE_DELETE = -5;
public static final int KEYCODE_ALT = -6;*/
@Override
public void onDraw(Canvas paramCanvas)
{
super.onDraw(paramCanvas);
this.mKeyboard = getKeyboard();
Iterator localIterator = this.mKeyboard.getKeys().iterator();
while (localIterator.hasNext())
{
Keyboard.Key localKey = (Keyboard.Key)localIterator.next();
if (localKey.codes[0] == -5)
{
// 绘制删除键
drawKeyBackground(R.drawable.keyboard_delete_btn_bg, paramCanvas, localKey);
drawIcon(paramCanvas, localKey);
} else if (localKey.codes[0] == -4)
{
// 绘制确认键
drawKeyBackground(R.drawable.keyboard_cofirm_btn_bg, paramCanvas, localKey);
drawText(paramCanvas, localKey, getResources().getDimension(R.dimen.fontsize36), -1, Typeface.DEFAULT);
}else if (localKey.codes[0] == -3){
// 绘制键盘图标
drawKeyBackground(R.drawable.keyboard_btn_normal_bg, paramCanvas, localKey);
drawIcon(paramCanvas, localKey);
} else {
// 绘制其他的数字 这里和 xml是对应的关系
drawKeyBackground(R.drawable.keyboard_btn_normal_bg, paramCanvas, localKey);
drawText(paramCanvas, localKey, getResources().getDimension(R.dimen.fontsize36), R.color.color_red, Typeface.DEFAULT);
}
}
}
}
第三步完成 封装与绑定。 随时随地的针对editText弹出特定键盘,注意要关闭系统软件盘,然后设置监听回调事件
public class KeyboardUtil {
private int keyboardLayout;
private Context mContext;
private EditText mEditText;
private KeyboardView mKeyboardView;
private InputMethodManager mInputMethodManager;
private NumKeyboardListener mNumKeyboardListener;
private KeyboardView.OnKeyboardActionListener mOnKeyboardActionListener = new KeyboardView.OnKeyboardActionListener() {
@Override
public void onPress(int primaryCode) {
}
@Override
public void onRelease(int primaryCode) {
}
@Override
public void onKey(int primaryCode, int[] keyCodes) {
if (primaryCode == Keyboard.KEYCODE_CANCEL) {
// 取消 -------- 进行隐藏的效果 ---------
// --------------------------
if (mNumKeyboardListener != null){
mNumKeyboardListener.onClose();
}
} else if (primaryCode == Keyboard.KEYCODE_DELETE) {
// 回退 --------- 删除该数据 ---------
if (mEditText != null){
int start = mEditText.getSelectionStart();
if (start >= 1){
mEditText.getText().delete(start-1,start);
}
}
} else if (primaryCode == Keyboard.KEYCODE_DONE){
// 确定提交--------------
if (mNumKeyboardListener != null){
mNumKeyboardListener.onConfirm();
}
}else {
// 具体的数值
String num = Character.toString((char) primaryCode);
if (mEditText != null){
int length = mEditText.getText().toString().length();
mEditText.getText().insert(length,num);
}
if (mNumKeyboardListener != null){
mNumKeyboardListener.onNumberChanged(num);
}
}
}
@Override
public void onText(CharSequence text) {
}
@Override
public void swipeLeft() {
}
@Override
public void swipeRight() {
}
@Override
public void swipeDown() {
}
@Override
public void swipeUp() {
}
};
public KeyboardUtil(Context mContext,KeyboardView keyboardView){
this.mContext = mContext;
this.mKeyboardView = keyboardView;
this.mInputMethodManager = ((InputMethodManager)this.mContext.getSystemService(Context.INPUT_METHOD_SERVICE));
}
public void attachEditText(EditText editText){
this.mEditText = editText;
}
/**
* 展示keyboard
*/
public void showNumKeyboard(){
mKeyboardView.setKeyboard(new Keyboard(this.mContext, R.xml.keyboard_num_woith_dot));
mKeyboardView.setEnabled(true);
mKeyboardView.setPreviewEnabled(false);
mKeyboardView.setOnKeyboardActionListener(this.mOnKeyboardActionListener);
}
/**
* 隐藏系统的软键盘
*/
private void hideSystemKeyboard(){
Method localMethod = null;
try {
localMethod = EditText.class.getMethod("setShowSoftInputOnFocus", Boolean.TYPE);
localMethod.setAccessible(true);
try {
localMethod.invoke(this.mEditText, Boolean.FALSE);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
this.mInputMethodManager.hideSoftInputFromWindow(this.mEditText.getWindowToken(), 0);
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
}
public void setNumKeyboardListener(NumKeyboardListener numKeyboardListener){
this.mNumKeyboardListener = numKeyboardListener;
}
public interface NumKeyboardListener{
void onClose();
void onConfirm();
void onNumberChanged(String paramString);
}
}
嗯嗯,大概就是这些东西了,希望对你们有帮助。 同类其他不错的文章
demo项目地址