最近公司项目有个需求,就是所有需要输入密码的EditText需要弹出自定义软键盘来代替系统键盘,于是就在网上找了些相关demo来学习借鉴一下。下面是我的实现大概思路,涉及主要功能模块三个类:
1、CustomKeyboardView一个自定义keyboradview,通过重新KeyboardView来实现自己的需求业务,主要重写onDraw()方法来给不同按键设置不同的背景;
public class CustomKeyboardView extends KeyboardView {
private int paddingTop;
private Paint paint;
private float shift_startx, shift_endx, shift_starty, shift_endy;
private float del_startx, del_endx, del_starty, del_endy;
private float p_startx, p_endy;
private float q_startx, q_endx, q_starty, q_endy;
private int keyWidth;
private int keyHeight;
private Context context;
private int keyboardView_height;
private PopupWindow mPreviewPopup;
public CustomKeyboardView(Context context, AttributeSet attrs) {
super(context, attrs);
paddingTop = getPaddingTop();
this.context = context;
}
@Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
post(new Runnable() {
@Override
public void run() {
getWidth(); // 获取宽度
keyboardView_height = getHeight();// 获取高度
MyLog.e(toString(), "keyboardViewHeight=" + keyboardView_height);
}
});
List<Keyboard.Key> keys = getKeyboard().getKeys();
paint = new Paint();
paint.setTextSize(UiUtils.dip2px(30));
Rect lRect = new Rect();
Paint.FontMetrics fontMetrics = paint.getFontMetrics();
float fontTotalHeight = fontMetrics.bottom - fontMetrics.top;
float offY = fontTotalHeight / 2 - fontMetrics.bottom;
//对不同按键设置不同背景图片
for (final Keyboard.Key key : keys) {
switch (key.codes[0]) {
case 0:
//顶部标题
Drawable dr_icon = getResources().getDrawable(R.drawable.safe_icon);
//宽高比例3:4
dr_icon.setBounds(UiUtils.dip2px(18), UiUtils.dip2px(7), UiUtils.dip2px(30), UiUtils.dip2px(23));
dr_icon.draw(canvas);
Paint paint_title = new Paint();
paint_title.setTextSize(UiUtils.dip2px(13));
paint_title.setColor(getResources().getColor(R.color.bg_deep_orange));
String str_title = "XXX安全键盘";
paint_title.getTextBounds(str_title, 0, str_title.length(), lRect);
canvas.drawText(str_title, key.x + UiUtils.dip2px(33), key.height - UiUtils.dip2px(5), paint_title);
break;
case -10:
//顶部完成键
Paint paint_complete = new Paint();
paint_complete.setTextSize(UiUtils.dip2px(18));
paint_complete.setColor(getResources().getColor(R.color.txt_blue));
String str_com = "完成";
paint_complete.getTextBounds(str_com, 0, str_com.length(), lRect);
canvas.drawText(str_com, key.x - UiUtils.dip2px(5), key.height - UiUtils.dip2px(3), paint_complete);
break;
case 112:
//p
p_startx = key.x;
p_endy = key.y + key.height + paddingTop;
break;
case -1:
shift_startx = key.x;
shift_endx = key.x + key.width;
shift_starty = key.y;
shift_endy = key.y + key.height + paddingTop;
// MyLog.e("xx_shift","key.width="+key.width+";key.height="+key.height);
//Shift
Drawable dr_press_shift = getResources().getDrawable(R.drawable.bg_shift_press);
Drawable dr_shift = getResources().getDrawable(R.drawable.bg_shift);
dr_press_shift.setBounds(key.x, key.y + paddingTop, key.x + key.width, key.y + key.height + paddingTop);
dr_shift.setBounds(key.x, key.y + paddingTop, key.x + key.width, key.y + key.height + paddingTop);
if (KeyboardUtil.shiftPressed) {
dr_press_shift.draw(canvas);
} else {
dr_shift.draw(canvas);
}
break;
case -5:
del_startx = key.x;
del_endx = key.x + key.width;
del_starty = key.y;
del_endy = key.y + key.height + paddingTop;
//Del
if (isNormal) {
Drawable dr_press_del = getResources().getDrawable(R.drawable.bg_del_press);
Drawable dr_del = getResources().getDrawable(R.drawable.bg_del);
drawKeyBackground(canvas, key, dr_press_del, dr_del);
} else {
Drawable dr_press_del = getResources().getDrawable(R.drawable.bg_num_del_press);
Drawable dr_del = getResources().getDrawable(R.drawable.bg_num_del);
drawKeyBackground(canvas, key, dr_press_del, dr_del);
}
break;
case 32:
//Space
Drawable dr_press_space = getResources().getDrawable(R.drawable.bg_space_press);
Drawable dr_space = getResources().getDrawable(R.drawable.bg_space);
drawKeyBackground(canvas, key, dr_press_space, dr_space);
break;
case -3:
//complete
Drawable dr_complete = getResources().getDrawable(R.drawable.bg_complete);
dr_complete.setBounds(key.x, key.y + paddingTop, key.x + key.width, key.y + key.height + paddingTop);
dr_complete.draw(canvas);
break;
case -2:
//123/ABC
if (isNormal) {
Drawable dr_switch = getResources().getDrawable(R.drawable.bg_num);
dr_switch.setBounds(key.x, key.y + paddingTop, key.x + key.width, key.y + key.height + paddingTop);
dr_switch.draw(canvas);
Paint paint = new Paint();
paint.setTextSize(UiUtils.dip2px(20));
paint.getTextBounds("123", 0, "123".length(), lRect);
float baseLineX = key.width / 2 - lRect.width() / 2 - key.gap / 2;
float baseLineY = key.y + key.height / 2 + offY - paddingTop / 2;
canvas.drawText("123", baseLineX, baseLineY, paint);
} else {
Paint paint = new Paint();
paint.setTextSize(UiUtils.dip2px(25));
paint.getTextBounds("ABC", 0, "ABC".length(), lRect);
float baseLineX_ABC = key.width / 2 - lRect.width() / 2;
float baseLineY_ABC = key.y + key.height / 2 + offY;
Drawable dr_pres = getResources().getDrawable(R.drawable.bg_num_press);
Drawable dr = getResources().getDrawable(R.drawable.bg_num_nor);
dr_pres.setBounds(key.x, key.y + paddingTop, key.x + key.width, key.y + key.height + paddingTop);
dr.setBounds(key.x, key.y + paddingTop, key.x + key.width, key.y + key.height + paddingTop);
if (key.pressed) {
dr_pres.draw(canvas);
} else {
dr.draw(canvas);