自定義數字鍵盤組件,實現了數字校驗,按鈕事件等,效果圖以下: 佈局代碼:android
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:layout_width="match_parent" android:layout_height="60dp"> <Button android:id="@+id/keyboard_1" android:layout_weight="1" style="@style/keyboardBtn" android:background="@drawable/keyboard_btn" android:layout_width="match_parent" android:layout_height="match_parent" android:text="@string/kb_1" /> <Button android:id="@+id/keyboard_2" android:layout_weight="1" style="@style/keyboardBtn" android:background="@drawable/keyboard_btn" android:layout_width="match_parent" android:layout_height="match_parent" android:text="@string/kb_2" /> <Button android:id="@+id/keyboard_3" android:layout_weight="1" style="@style/keyboardBtn" android:background="@drawable/keyboard_btn" android:layout_width="match_parent" android:layout_height="match_parent" android:text="@string/kb_3" /> <Button android:id="@+id/keyboard_clear" android:layout_weight="1" android:textColor="@color/btn_bg_red_color" android:textSize="@dimen/abc_text_size_title_material" android:background="@drawable/keyboard_btn" android:layout_width="match_parent" android:layout_height="match_parent" android:text="@string/kb_clear" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="60dp"> <Button android:id="@+id/keyboard_4" android:layout_weight="1" style="@style/keyboardBtn" android:background="@drawable/keyboard_btn" android:layout_width="match_parent" android:layout_height="match_parent" android:text="@string/kb_4" /> <Button android:id="@+id/keyboard_5" android:layout_weight="1" style="@style/keyboardBtn" android:background="@drawable/keyboard_btn" android:layout_width="match_parent" android:layout_height="match_parent" android:text="@string/kb_5" /> <Button android:id="@+id/keyboard_6" android:layout_weight="1" style="@style/keyboardBtn" android:background="@drawable/keyboard_btn" android:layout_width="match_parent" android:layout_height="match_parent" android:text="@string/kb_6" /> <LinearLayout android:id="@+id/keyboard_back" android:layout_weight="1" android:background="@drawable/keyboard_btn" android:gravity="center" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:background="@drawable/keyboard_clear_bitmap" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> </LinearLayout> <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="120dp"> <LinearLayout android:layout_weight="1" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:layout_width="match_parent" android:layout_height="60dp"> <Button android:id="@+id/keyboard_7" android:layout_weight="1" style="@style/keyboardBtn" android:background="@drawable/keyboard_btn" android:layout_width="match_parent" android:layout_height="match_parent" android:text="@string/kb_7" /> <Button android:id="@+id/keyboard_8" android:layout_weight="1" style="@style/keyboardBtn" android:background="@drawable/keyboard_btn" android:layout_width="match_parent" android:layout_height="match_parent" android:text="@string/kb_8" /> <Button android:id="@+id/keyboard_9" android:layout_weight="1" style="@style/keyboardBtn" android:background="@drawable/keyboard_btn" android:layout_width="match_parent" android:layout_height="match_parent" android:text="@string/kb_9" /> </LinearLayout> <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="60dp"> <Button android:id="@+id/keyboard_0" android:layout_weight="2" style="@style/keyboardBtn" android:background="@drawable/keyboard_btn" android:layout_width="match_parent" android:layout_height="match_parent" android:text="@string/kb_0" /> <Button android:id="@+id/keyboard_dian" android:layout_weight="1" style="@style/keyboardBtn" android:background="@drawable/keyboard_btn" android:layout_width="match_parent" android:layout_height="match_parent" android:text="@string/kb_dian" /> </LinearLayout> </LinearLayout> <Button android:id="@+id/keyboard_alipay" android:layout_weight="3" android:textColor="@color/white" android:textSize="@dimen/abc_text_size_title_material" android:background="@color/pay_btn_grey" android:layout_width="match_parent" android:layout_height="match_parent" android:text="收款" /> </LinearLayout> </LinearLayout>
對應的封裝的Java代碼:微信
package com.zqsy.qrcodebox.component; import android.content.Context; import android.util.AttributeSet; import android.view.LayoutInflater; import android.view.View; import android.widget.Button; import android.widget.LinearLayout; import com.zqsy.qrcodebox.R; /** * Created by zhangda on 2017/12/3. * 自定義數字鍵盤組件 */ public class NumberKeyBoard extends LinearLayout{ private Context mContext; private OnValueChangedListener onValueChangedListener; private OnAlipayCheckedListener onAlipayCheckedListener; private OnWeixinCheckedListener onWeixinCheckedListener; private String resultStr = ""; private Button btn1; private Button btn2; private Button btn3; private Button btn4; private Button btn5; private Button btn6; private Button btn7; private Button btn8; private Button btn9; private Button btn0; private Button btnDian; private LinearLayout btnBack; private Button btnClear; private Button btnAlipay; // private Button btnWeixin; public NumberKeyBoard(Context context) { super(context); } public NumberKeyBoard(Context context, AttributeSet attrs) { super(context, attrs); mContext = context; LayoutInflater.from(context).inflate(R.layout.component_keyborad, this); initBtnsListeners(); } private void initBtnsListeners() { btn1 = findViewById(R.id.keyboard_1); btn2 = findViewById(R.id.keyboard_2); btn3 = findViewById(R.id.keyboard_3); btn4 = findViewById(R.id.keyboard_4); btn5 = findViewById(R.id.keyboard_5); btn6 = findViewById(R.id.keyboard_6); btn7 = findViewById(R.id.keyboard_7); btn8 = findViewById(R.id.keyboard_8); btn9 = findViewById(R.id.keyboard_9); btn0 = findViewById(R.id.keyboard_0); btnDian = findViewById(R.id.keyboard_dian); btnBack = findViewById(R.id.keyboard_back); btnClear = findViewById(R.id.keyboard_clear); btnAlipay = findViewById(R.id.keyboard_alipay); // btnWeixin = findViewById(R.id.keyboard_weixin); OnClickListener buttonsOnClickListener = new OnClickListener() { @Override public void onClick(View view) { switch (view.getId()){ case R.id.keyboard_1: addNumberToResult("1"); break; case R.id.keyboard_2: addNumberToResult("2"); break; case R.id.keyboard_3: addNumberToResult("3"); break; case R.id.keyboard_4: addNumberToResult("4"); break; case R.id.keyboard_5: addNumberToResult("5"); break; case R.id.keyboard_6: addNumberToResult("6"); break; case R.id.keyboard_7: addNumberToResult("7"); break; case R.id.keyboard_8: addNumberToResult("8"); break; case R.id.keyboard_9: addNumberToResult("9"); break; case R.id.keyboard_0: addNumberToResult("0"); break; case R.id.keyboard_dian: addNumberToResult("."); break; case R.id.keyboard_back: reduceNumberToResult(); break; case R.id.keyboard_clear: clearNumber(); break; } } }; btn1.setOnClickListener(buttonsOnClickListener); btn2.setOnClickListener(buttonsOnClickListener); btn3.setOnClickListener(buttonsOnClickListener); btn4.setOnClickListener(buttonsOnClickListener); btn5.setOnClickListener(buttonsOnClickListener); btn6.setOnClickListener(buttonsOnClickListener); btn7.setOnClickListener(buttonsOnClickListener); btn8.setOnClickListener(buttonsOnClickListener); btn9.setOnClickListener(buttonsOnClickListener); btn0.setOnClickListener(buttonsOnClickListener); btnDian.setOnClickListener(buttonsOnClickListener); btnBack.setOnClickListener(buttonsOnClickListener); btnClear.setOnClickListener(buttonsOnClickListener); btnAlipay.setOnClickListener(new OnClickListener() { @Override public void onClick(View view) { //支付按鈕能夠點擊時才觸發事件 if(btnAlipay.getTag() != null && btnAlipay.getTag().equals("canPress")){ if(onAlipayCheckedListener != null){ onAlipayCheckedListener.onClick(formatResult(resultStr)); } } } }); // btnWeixin.setOnClickListener(new OnClickListener() { // @Override // public void onClick(View view) { // if(onWeixinCheckedListener != null){ // onWeixinCheckedListener.onClick(formatResult(resultStr)); // } // // } // }); } //清除數字 public void clearNumber() { resultStr = ""; if(onValueChangedListener != null){ onValueChangedListener.onChange(formatResult(resultStr)); } } //擦除數字 private void reduceNumberToResult() { if(resultStr.length() > 0){ resultStr = resultStr.substring(0, resultStr.length()-1); } if(onValueChangedListener != null){ onValueChangedListener.onChange(formatResult(resultStr)); } } //增長數字 private void addNumberToResult(String numberStr) { if(resultStr.indexOf(".") >= 0){ if(numberStr.equals(".")){ return; }else{ //限制小數點後面最多有兩位小數 int xiaoshuLen = resultStr.substring(resultStr.indexOf("."), resultStr.length()).length(); if(xiaoshuLen == 3){ return; } } } //第一次輸入的是點,顯示0. if(resultStr.equals("") && numberStr.equals(".")){ numberStr = "0."; } //數字前屢次輸入0,只顯示一個0 if(resultStr != null && !resultStr.equals("") && resultStr.equals("0") && numberStr.equals("0")){ return; } resultStr += numberStr; if(onValueChangedListener != null){ onValueChangedListener.onChange(formatResult(resultStr)); } } private String formatResult(String str){ if(str == null || str.equals("")){ //收款按鈕設置爲灰色 btnAlipay.setTag("cannotPress"); btnAlipay.setBackgroundColor(getResources().getColor(R.color.pay_btn_grey)); return "0"; }else{ double resultDou = Double.parseDouble(str); if(resultDou > 0){ btnAlipay.setTag("canPress"); btnAlipay.setBackgroundColor(getResources().getColor(R.color.btn_bg_red_color)); }else{ btnAlipay.setTag("cannotPress"); btnAlipay.setBackgroundColor(getResources().getColor(R.color.pay_btn_grey)); } } return str; } /*** * 鍵盤控件的值改變事件方法 * @param listener */ public void setOnValueChangedListener(OnValueChangedListener listener){ onValueChangedListener = listener; } /*** * 支付寶結帳事件 * @param listener */ public void setOnAlipayCheckedListener(OnAlipayCheckedListener listener){ onAlipayCheckedListener = listener; } /*** * 微信結帳事件 * @param listener */ public void setOnWeixinCheckedListener(OnWeixinCheckedListener listener){ onWeixinCheckedListener = listener; } public interface OnValueChangedListener{ void onChange(String result); } public interface OnAlipayCheckedListener{ void onClick(String result); } public interface OnWeixinCheckedListener{ void onClick(String result); } }
在須要使用該組件的佈局文件中添加以下:ide
<yourpakage.component.NumberKeyBoard android:id="@+id/cashier_numberKeyBoard" android:layout_alignParentBottom="true" android:layout_width="match_parent" android:layout_height="wrap_content"> </yourpakage.component.NumberKeyBoard>
在須要使用該組件的activity添加:佈局
NumberKeyBoard numberKeyBoard = findViewById(R.id.cashier_numberKeyBoard); numberKeyBoard.setOnValueChangedListener(new NumberKeyBoard.OnValueChangedListener() { @Override public void onChange(String result) { cashier_account.setText(result+""); } }); numberKeyBoard.setOnAlipayCheckedListener(new NumberKeyBoard.OnAlipayCheckedListener() { @Override public void onClick(String result) { goToPay(result, "alipay"); } });
ok,大功告成!this