Android如何实现数字九宫格软键盘

这篇文章主要介绍了Android如何实现数字九宫格软键盘,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。

前言

一开始大概是这种

Android如何实现数字九宫格软键盘

需求

组长说 要不搞一个自定义软键盘吧 数字搞大点 方便外卖员输入数字

我设置了输入EditText的输入格式为Number 还是不行

那就开搞吧

先来看下实现的效果图吧

实现效果GIF

Android如何实现数字九宫格软键盘

实现代码

自定义View 一个NineNumericKeyboardView

/**
*AuthorbyLyu
*Dateon2021/5/26-19:55
*Description:九宫格数字软键盘
*/
publicclassNineNumericKeyboardViewextendsView{
/**
*列
*/
privatestaticfinalintTOTAL_COL=3;
/**
*行
*/
privatestaticfinalintTOTAL_ROW=4;

privatePaintHuiseBgPaint,linePaint;
privatePaintmTextPaint;
privateintmViewWidth;//键盘宽度
privateintmViewHight;//键盘高度
privatefloatmCellWidth,mCellHight;//单元格宽度、高度
privateRowrows[]=newRow[TOTAL_ROW];
privateBitmapbitmap;//删除按钮图片
privatePaintmCutTextPaint;


//回调方法
publicinterfaceCallBack{
voidclickNum(Stringnum);//回调点击的数字

voiddeleteNum();//回调删除
}

privateCallBackmCallBack;//回调

publicvoidsetOnCallBack(CallBackcallBack){
mCallBack=callBack;
}

publicNineNumericKeyboardView(Contextcontext,AttributeSetattrs,intdefStyle){
super(context,attrs,defStyle);
init(context);

}

publicNineNumericKeyboardView(Contextcontext,AttributeSetattrs){
super(context,attrs);
init(context);

}

publicNineNumericKeyboardView(Contextcontext){
super(context);
init(context);
}

@Override
protectedvoidonDraw(Canvascanvas){
super.onDraw(canvas);
drawLine(canvas);
for(inti=0;i<TOTAL_ROW;i++){
if(rows[i]!=null)
rows[i].drawCells(canvas);
}
}

/**
*画6条直线
*
*@paramcanvas
*/
privatevoiddrawLine(Canvascanvas){
canvas.drawLine(0,0,mViewWidth,0,linePaint);
canvas.drawLine(0,mCellHight,mViewWidth,mCellHight,linePaint);
canvas.drawLine(0,mCellHight*2,mViewWidth,mCellHight*2,linePaint);
canvas.drawLine(0,mCellHight*3,mViewWidth,mCellHight*3,linePaint);
canvas.drawLine(mCellWidth,0,mCellWidth,mViewHight,linePaint);
canvas.drawLine(mCellWidth*2,0,mCellWidth*2,mViewHight,linePaint);


}

/**
*初始化画笔
*
*@paramcontext
*/
privatevoidinit(Contextcontext){
mTextPaint=newPaint(Paint.ANTI_ALIAS_FLAG);
mCutTextPaint=newPaint(Paint.ANTI_ALIAS_FLAG);
linePaint=newPaint(Paint.ANTI_ALIAS_FLAG);
linePaint.setTextSize(1.0f);
linePaint.setColor(0x90000000);

HuiseBgPaint=newPaint(Paint.ANTI_ALIAS_FLAG);
HuiseBgPaint.setStyle(Paint.Style.FILL);
HuiseBgPaint.setColor(Color.parseColor("#e9e9e9"));

initDate();
}

privatevoidinitDate(){
fillDate();
}

@Override
protectedvoidonSizeChanged(intw,inth,intoldw,intoldh){
super.onSizeChanged(w,h,oldw,oldh);
mViewWidth=w;
mViewHight=h;
mCellWidth=mViewWidth/TOTAL_COL;
mCellHight=mViewHight/TOTAL_ROW;
mTextPaint.setTextSize(mCellHight/3);

}

privateCellmClickCell=null;
privatefloatmDownX;
privatefloatmDownY;

/*
*
*触摸事件为了确定点击位置的数字
*/
@Override
publicbooleanonTouchEvent(MotionEventevent){
switch(event.getAction()){
caseMotionEvent.ACTION_DOWN:
mDownX=event.getX();
mDownY=event.getY();
intcol=(int)(mDownX/mCellWidth);
introw=(int)(mDownY/mCellHight);
measureClickCell(col,row);
break;
caseMotionEvent.ACTION_UP:
if(mClickCell!=null){
//在抬起后把状态置为默认
rows[mClickCell.i].cells[mClickCell.j].state=State.DEFAULT_NUM;
mClickCell=null;
invalidate();
}
break;
}
returntrue;
}

/**
*测量点击单元格
*
*@paramcol列
*@paramrow行
*/
privatevoidmeasureClickCell(intcol,introw){
if(col>=TOTAL_COL||row>=TOTAL_ROW)
return;
if(rows[row]!=null){
mClickCell=newCell(rows[row].cells[col].num,rows[row].cells[col].state,rows[row].cells[col].i,
rows[row].cells[col].j);
rows[row].cells[col].state=State.CLICK_NUM;
if("-5".equals(rows[row].cells[col].num)){
mCallBack.deleteNum();
}else{
mCallBack.clickNum(rows[row].cells[col].num);
}
invalidate();
}
}

/**
*组以一行为一组
*/
privateclassRow{
publicintj;

Row(intj){
this.j=j;
}

//一行3个单元格
publicCell[]cells=newCell[TOTAL_COL];

publicvoiddrawCells(Canvascanvas){
for(inti=0;i<cells.length;i++){
if(cells[i]!=null)
cells[i].drawSelf(canvas);
}

}
}

//单元格
privateclassCell{
publicStringnum;
publicStatestate;
/**
*i=行j=列
*/
publicinti;
publicintj;

publicCell(Stringnum,Statestate,inti,intj){
super();
this.num=num;
this.state=state;
this.i=i;
this.j=j;
}

//绘制一个单元格如果颜色需要自定义可以修改
publicvoiddrawSelf(Canvascanvas){
switch(state){
caseCLICK_NUM:
//绘制点击效果灰色背景
canvas.drawRect((mCellWidth*j),(mCellHight*i),
(mCellWidth*(j+1)),(mCellHight*(i+1)),HuiseBgPaint);
break;
}
if("-5".equals(num)){
//绘制删除图片
canvas.drawBitmap(bitmap,(float)(mCellWidth*2.5-bitmap.getWidth()/2),(float)(mCellHight*3.5-bitmap.getHeight()/2),HuiseBgPaint);
}else{
//绘制数字
canvas.drawText(num,(float)((j+0.5)*mCellWidth-mTextPaint.measureText(num)/2),
(float)((i+0.5)*mCellHight+mTextPaint.measureText(num,0,1)/2),
mTextPaint);
}


}
}

/**
*cell的state
*/
privateenumState{
DEFAULT_NUM,CLICK_NUM;
}

privateList<String>numKeys=Arrays.asList("1","2","3","4","5","6","7","8","9","0");

/**
*填充数字
*/
privatevoidfillDate(){
intpostion=0;
for(inti=0;i<TOTAL_ROW;i++){
rows[i]=newRow(i);
for(intj=0;j<TOTAL_COL;j++){
if(i==3&&j==0){
rows[i].cells[j]=newCell(".",State.DEFAULT_NUM,i,j);
continue;
}elseif(i==3&&j==2){
rows[i].cells[j]=newCell("-5",State.DEFAULT_NUM,i,j);
continue;
}else{
rows[i].cells[j]=newCell(numKeys.get(postion),State.DEFAULT_NUM,i,j);
postion++;
}
}
}
//这里是插入一张删除数字的图一般是X
bitmap=BitmapFactory.decodeResource(getResources(),R.mipmap.keyboard_delete);
}

}

使用方法

利用android自带的组件PopupWindow在指定页面的下方弹出即可 完成效果

在指定的View页面

//初始化键盘
privatevoidinitKeyboardView(){
//设置不弹出系统键盘
etInputPickupCode.setInputType(InputType.TYPE_NULL);
//自己监听EditText的点击事件弹出我们自定义的键盘
etInputPickupCode.setOnClickListener(view->mPop.showAtLocation(llKey,Gravity.BOTTOM,0,0));
mPop=newPopupWindow();
mPopView=LayoutInflater.from(getApplicationContext()).inflate(R.layout.custom_keyboardview,null);
mPop.setContentView(mPopView);
mPop.setTouchable(true);
mPop.setFocusable(true);
mPop.setBackgroundDrawable(newColorDrawable());
mPop.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
mPop.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
mCustomKeyView=mPopView.findViewById(R.id.key_view);
//设置回调,并进行文本的插入与删除
mCustomKeyView.setOnCallBack(this);
}


@Override
publicvoidclickNum(Stringnum){
if(etInputPickupCode.getText().length()<8){
etInputPickupCode.append(num);
}
}

@Override
publicvoiddeleteNum(){
intlast=etInputPickupCode.getText().length();
if(last>0){
//删除最后一位
etInputPickupCode.getText().delete(last-1,last);
}
}

感谢你能够认真阅读完这篇文章,希望小编分享的“Android如何实现数字九宫格软键盘”这篇文章对大家有帮助,同时也希望大家多多支持恰卡编程网,关注恰卡编程网行业资讯频道,更多相关知识等着你来学习!

发布于 2021-05-30 14:10:16
收藏
分享
海报
0 条评论
195
上一篇:Unity如何实现语音识别功能 下一篇:怎么用RabbitMQ和Swoole实现一个异步任务系统
目录

    0 条评论

    本站已关闭游客评论,请登录或者注册后再评论吧~

    忘记密码?

    图形验证码