由于最近公司做的一个项目中有一个地方是用到了输入金额数字,但是由于美观原因不能使用系统自带数字键盘,所以就自己写了一个自定义数字键盘的小demo,现记录一下。先看效果图
ScreenGif.gifxml布局文件,使用表格布局
<pre>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorBackGroundGrayMore"
android:orientation="vertical">
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:layout_marginTop="20dp">
<TableRow
android:layout_marginLeft="10dp"
android:layout_height="60dp"
android:layout_marginRight="10dp">
<Button
android:id="@+id/keypad_btn_one"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/selector_button_pay_num"
android:text="1"
android:textSize="20sp"
style="?android:attr/borderlessButtonStyle"
android:textColor="@color/colorTextGray" />
<Button
android:id="@+id/keypad_btn_two"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginLeft="2dp"
style="?android:attr/borderlessButtonStyle"
android:layout_weight="1"
android:background="@drawable/selector_button_pay_num"
android:text="2"
android:textSize="20sp"
android:textColor="@color/colorTextGray" />
<Button
android:id="@+id/keypad_btn_three"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginLeft="2dp"
style="?android:attr/borderlessButtonStyle"
android:layout_weight="1"
android:background="@drawable/selector_button_pay_num"
android:text="3"
android:textSize="20sp"
android:textColor="@color/colorTextGray" />
</TableRow>
<TableRow
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="2dp">
<Button
android:id="@+id/keypad_btn_fore"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/selector_button_pay_num"
android:text="4"
style="?android:attr/borderlessButtonStyle"
android:textSize="20sp"
android:textColor="@color/colorTextGray" />
<Button
android:id="@+id/keypad_btn_five"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginLeft="2dp"
android:layout_weight="1"
style="?android:attr/borderlessButtonStyle"
android:background="@drawable/selector_button_pay_num"
android:text="5"
android:textSize="20sp"
android:textColor="@color/colorTextGray" />
<Button
android:id="@+id/keypad_btn_six"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginLeft="2dp"
style="?android:attr/borderlessButtonStyle"
android:layout_weight="1"
android:textSize="20sp"
android:background="@drawable/selector_button_pay_num"
android:text="6"
android:textColor="@color/colorTextGray" />
</TableRow>
<TableRow
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="2dp">
<Button
android:id="@+id/keypad_btn_seven"
android:layout_width="0dp"
android:layout_height="match_parent"
style="?android:attr/borderlessButtonStyle"
android:textSize="20sp"
android:layout_weight="1"
android:background="@drawable/selector_button_pay_num"
android:text="7"
android:textColor="@color/colorTextGray" />
<Button
android:id="@+id/keypad_btn_eight"
android:layout_width="0dp"
android:textSize="20sp"
style="?android:attr/borderlessButtonStyle"
android:layout_height="match_parent"
android:layout_marginLeft="2dp"
android:layout_weight="1"
android:background="@drawable/selector_button_pay_num"
android:text="8"
android:textColor="@color/colorTextGray" />
<Button
android:id="@+id/keypad_btn_nine"
android:layout_width="0dp"
style="?android:attr/borderlessButtonStyle"
android:layout_height="match_parent"
android:layout_marginLeft="2dp"
android:layout_weight="1"
android:textSize="20sp"
android:background="@drawable/selector_button_pay_num"
android:text="9"
android:textColor="@color/colorTextGray" />
</TableRow>
<TableRow
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="2dp">
<Button
android:id="@+id/keypad_btn_del"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/selector_button_pay_num"
android:text="回删"
style="?android:attr/borderlessButtonStyle"
android:textSize="18sp"
android:textColor="@color/colorTextGray" />
<Button
android:id="@+id/keypad_btn_zero"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginLeft="2dp"
android:layout_weight="1"
android:background="@drawable/selector_button_pay_num"
android:text="0"
style="?android:attr/borderlessButtonStyle"
android:textSize="20sp"
android:textColor="@color/colorTextGray" />
<Button
android:id="@+id/keypad_btn_ok"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginLeft="2dp"
android:layout_weight="1"
android:background="@drawable/selector_button_pay_num"
android:text="完成"
style="?android:attr/borderlessButtonStyle"
android:textSize="18sp"
android:textColor="@color/colorTextGray" />
</TableRow>
</TableLayout>
</LinearLayout>
</pre>
自定义view类
<pre>
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);
}
}
</pre>
在项目中的使用
首先在布局文件中填写控件,然后在代码中如下使用
<pre>
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);
}
});
</pre>
view中接口预留的三个方法分别对应回删,完成和点击数字按键执行的操作,可以根据不同需求进行相应的处理,这个自定义数字键盘属于比较简单的自定义组合view,希望能对各位有所帮助。
网友评论