Android 自定义数字键盘实现方法

自定义数字键盘

1.键盘布局
horizontalGap:按键间的水平间隔
keyHeight:按键高度以%或者%p结尾
keyWidth:按键宽度,”33.33333%p”
verticalGap:按键间的垂直间隔
codes:可以是系统给的固定值也可以是自定义的值

  
 

    
    
    


    
    
    


    
    
    


    
    
    


2.继承keyboardview
这里主要就是3个点:1、获取按键布局(setkeyboard)2、绘制空白键跟删除键(onDraw)3、设置回调(OnKeyPressListener)。
删除按钮我做了一点处理,因为直接使用获取的dp(intrinsicWidth)的话图片会比较大,不是很好看,为了协调一点我宽高各自除了个6,然后计算了上下左右的边距(widthInterval,heightInterval),再进行的绘制。

这里用了自定义的属性展示在这里:

  
    
    

NumKeyView的代码

 public class NumKeyView extends KeyboardView implements OnKeyboardActionListener {
  //用于区分左下角空白按键,(要与xml里设置的数值相同)
  private int KEYCODE_EMPTY=-10;
   //删除按键背景图片
  private Drawable mDeleteDrawable;
  //最下面两个灰色的按键(空白按键跟删除按键)
  private int mBgColor;


 public NumKeyView(Context context, AttributeSet attrs) {
    super(context, attrs);
    init(context,attrs,0);
 }

 public NumKeyView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    init(context, attrs, defStyleAttr);
 }

 private void init(Context context, AttributeSet attrs, int defStyleAttr) {
    TypedArray ta=context.obtainStyledAttributes(attrs, R.styleable.NumKeyView);
    mBgColor=ta.getColor(R.styleable.NumKeyView_gbColor, Color.RED);
    mDeleteDrawable=ta.getDrawable(R.styleable.NumKeyView_deleteDrawable);
    ta.recycle();
    //获取xml中的按键布局
    Keyboard keyboard=new Keyboard(context,R.xml.numkeyview);
    setKeyboard(keyboard);
    setEnabled(true);
    setPreviewEnabled(false);
    setOnKeyboardActionListener(this);
    }

@Override
public void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    List keys=getKeyboard().getKeys();
    for (Keyboard.Key key:keys){
        //绘制空白键
        if (key.codes[0]==KEYCODE_EMPTY){
            drawKeyBackGround(key,canvas);
        }else if (key.codes[0]==Keyboard.KEYCODE_DELETE){
            //绘制删除键背景
            drawKeyBackGround(key,canvas);
            //绘制按键图片
            drawkeyDelete(key,canvas);
        }
    }
}
private void drawKeyBackGround(Keyboard.Key key, Canvas canvas) {
    ColorDrawable colordrawable=new ColorDrawable(mBgColor);
    colordrawable.setBounds(key.x,key.y,key.x+key.width,key.y+key.height);
    colordrawable.draw(canvas);
}
private void drawkeyDelete(Keyboard.Key key, Canvas canvas) {
    int intrinsicWidth=mDeleteDrawable.getIntrinsicWidth();
    int intrinsicHeight=mDeleteDrawable.getIntrinsicHeight();
    int drawWidth=key.width;
    int drawHeight=key.height;
    if(drawWidth

注意!调用的时候,因为使用了自定义属性(AS)所以注意添加下面一行代码
xmlns:app="http://schemas.android.com/apk/res-auto"
3.其他部分
上面按键部分就做好了,下面看下自定义按键的中上面的接口回调

public class KeyDemo extends AppCompatActivity {
private EditText mEditText;
private NumKeyView mKeyView;
private Button mButton;
private LinearLayout mLinearlayout;
private PopupWindow mPop;
private LinearLayout mProgressBarLn;
private View mPopView;
private Handler mhandler=new Handler(){
    @Override
    public void handleMessage(Message msg) {
        super.handleMessage(msg);
        mPop.dismiss();
        mKeyView.setVisibility(View.VISIBLE);
        mProgressBarLn.setVisibility(View.INVISIBLE);
        mButton.setText("完成");
    }
};
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_keyboard);
    init();
    mButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            //点击按钮显示键盘
            mPop.showAtLocation(mLinearlayout, Gravity.BOTTOM,0,0);
        }
    });
    //设置回调,并进行文本的插入与删除
    mKeyView.setOnKeyPressListener(new NumKeyView.OnKeyPressListener() {
        @Override
        public void onInertKey(String text) {
            if(mEditText.getText().length()<6){
                mEditText.append(text);
                //文本长度为6时隐藏键盘,显示进度条,一段时间后pop消失
                if (mEditText.getText().length()==6){
                    mKeyView.setVisibility(View.INVISIBLE);
                    mProgressBarLn.setVisibility(View.VISIBLE);
                    mhandler.sendEmptyMessageDelayed(0x11,4000);
                }
            }
        }

        @Override
        public void onDeleteKey() {
            int last=mEditText.getText().length();
            if (last>0){
                //删除最后一位
            mEditText.getText().delete(last-1,last);
            }
        }
    });

}

private void init() {
    mEditText= (EditText) findViewById(R.id.et);
    mButton= (Button) findViewById(R.id.bt);
    mLinearlayout= (LinearLayout) findViewById(R.id.ln);
    mPop=new PopupWindow();
    mPopView=LayoutInflater.from(getApplicationContext()).inflate(R.layout.keyboard_pop,null);
    mPop.setContentView(mPopView);
    mPop.setTouchable(true);
    mPop.setFocusable(true);
    mPop.setBackgroundDrawable(new ColorDrawable());
    mPop.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
    mPop.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
    mPop.setAnimationStyle(R.style.PopWindowstyle);
    mProgressBarLn= (LinearLayout) mPopView.findViewById(R.id.progressbar_ln);
    mKeyView= (NumKeyView) mPopView.findViewById(R.id.keyboardview);
}
}

主布局

 
 


Pop布局

 




    
    



Pop动画

弹出





 

消失





定义动画

  

你可能感兴趣的:(Android 自定义数字键盘实现方法)