自定义数字键盘

由于最近公司做的一个项目中有一个地方是用到了输入金额数字,但是由于美观原因不能使用系统自带数字键盘,所以就自己写了一个自定义数字键盘的小demo,现记录一下。先看效果图

自定义数字键盘_第1张图片
ScreenGif.gif

xml布局文件,使用表格布局



android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorBackGroundGrayMore"
android:orientation="vertical">



    

        


自定义view类

public class NumKeyPad extends LinearLayout implements View.OnClickListener {
private View mView;
private NumKeyPadClickListener numKeyPadClickListener = null;

private StringBuffer sb_PwdNum;

private Context mContext;

private int maxLength = 5;//最大输入长度

private Button btn_num_one, btn_num_two, btn_num_three, btn_num_fore, btn_num_five, btn_num_six, btn_num_seven, btn_num_eight, btn_num_nine, btn_num_zero, btn_ok, btn_del;


public NumKeyPad(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    getView(context);
}

public NumKeyPad(Context context, AttributeSet attrs) {
    super(context, attrs);
    getView(context);
}

public NumKeyPad(Context context) {
    super(context);
    getView(context);
}


@Override
public void onClick(View v) {

    switch (v.getId()) {

        case R.id.keypad_btn_one:
            InputNumber(1);
            break;
        case R.id.keypad_btn_two:
            InputNumber(2);
            break;
        case R.id.keypad_btn_three:
            InputNumber(3);
            break;
        case R.id.keypad_btn_fore:
            InputNumber(4);
            break;
        case R.id.keypad_btn_five:
            InputNumber(5);
            break;
        case R.id.keypad_btn_six:
            InputNumber(6);
            break;
        case R.id.keypad_btn_seven:
            InputNumber(7);
            break;
        case R.id.keypad_btn_eight:
            InputNumber(8);
            break;
        case R.id.keypad_btn_nine:
            InputNumber(9);
            break;
        case R.id.keypad_btn_zero:
            InputNumber(0);
            break;
        case R.id.keypad_btn_del:

          DelNumber();
            break;
        case R.id.keypad_btn_ok:
          numKeyPadClickListener.OkPwd(mView,sb_PwdNum);
            break;


    }
}

/**
 * 数字键盘输入
 *
 * @param num
 */
private void InputNumber(int num) {

    if (sb_PwdNum.length() >= maxLength) {
        return;
    }

    sb_PwdNum.append(num);
    numKeyPadClickListener.inputPwd(mView, sb_PwdNum);
}



/**
 * 数字键盘回删
 */
private void DelNumber() {

    if (sb_PwdNum.length() <= 0) {
        return;
    }
    sb_PwdNum.deleteCharAt(sb_PwdNum.length() - 1);

    numKeyPadClickListener.deletePwd(mView, sb_PwdNum);

}


/**
 * 得到视图
 */
private void getView(Context mContext) {

    this.mContext = mContext;

    sb_PwdNum = new StringBuffer();
    mView = LayoutInflater.from(mContext).inflate(R.layout.view_kaypad, null);
    ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
    mView.setLayoutParams(params);
    addView(mView);
    btn_num_one = (Button) mView.findViewById(R.id.keypad_btn_one);
    btn_num_two = (Button) mView.findViewById(R.id.keypad_btn_two);
    btn_num_three = (Button) mView.findViewById(R.id.keypad_btn_three);
    btn_num_fore = (Button) mView.findViewById(R.id.keypad_btn_fore);
    btn_num_five = (Button) mView.findViewById(R.id.keypad_btn_five);
    btn_num_six = (Button) mView.findViewById(R.id.keypad_btn_six);
    btn_num_seven = (Button) mView.findViewById(R.id.keypad_btn_seven);
    btn_num_eight = (Button) mView.findViewById(R.id.keypad_btn_eight);
    btn_num_nine = (Button) mView.findViewById(R.id.keypad_btn_nine);
    btn_num_zero = (Button) mView.findViewById(R.id.keypad_btn_zero);
    btn_del = (Button) mView.findViewById(R.id.keypad_btn_del);
    btn_ok = (Button) mView.findViewById(R.id.keypad_btn_ok);

    btn_num_one.setOnClickListener(this);
    btn_num_two.setOnClickListener(this);
    btn_num_three.setOnClickListener(this);
    btn_num_fore.setOnClickListener(this);
    btn_num_five.setOnClickListener(this);
    btn_num_six.setOnClickListener(this);
    btn_num_seven.setOnClickListener(this);
    btn_num_eight.setOnClickListener(this);
    btn_num_nine.setOnClickListener(this);
    btn_num_zero.setOnClickListener(this);
    btn_del.setOnClickListener(this);
    btn_ok.setOnClickListener(this);

}


public void setNumKeyPadClickListener(NumKeyPadClickListener listener) {

    this.numKeyPadClickListener = listener;
}

public interface NumKeyPadClickListener {

    void deletePwd(View v, StringBuffer number);

    void OkPwd(View v, StringBuffer number);

    void inputPwd(View v, StringBuffer number);
}

}

在项目中的使用

首先在布局文件中填写控件,然后在代码中如下使用


mNumKeyPad= (NumKeyPad) findViewById(R.id.keyboard);
mNumKeyPad.setNumKeyPadClickListener(new NumKeyPad.NumKeyPadClickListener() {
@Override
public void deletePwd(View v, StringBuffer number) {
mTextView.setText(number);
}

       @Override
       public void OkPwd(View v, StringBuffer number) {
                mNumKeyPad.setVisibility(View.GONE);
       }

       @Override
       public void inputPwd(View v, StringBuffer number) {
           mTextView.setText(number);

       }
   });


view中接口预留的三个方法分别对应回删,完成和点击数字按键执行的操作,可以根据不同需求进行相应的处理,这个自定义数字键盘属于比较简单的自定义组合view,希望能对各位有所帮助。

你可能感兴趣的:(自定义数字键盘)