自定義純數字密碼輸入鍵盤-仿支付寶數字密碼鍵盤
阿新 • • 發佈:2019-01-22
前陣子遇到專案需求,需要自己整一個純數字密碼輸入鍵盤,所以也就整出來了,如下圖:
其實也就是繼承重寫了popupwindow。
public class MyPopupWindow extends PopupWindow implements View.OnClickListener { public static final int Num_Zero = 0; public static final int Num_One = 1; public static final int Num_Two = 2; public static final int Num_Three= 3; public static final int Num_Four = 4; public static final int Num_Five = 5; public static final int Num_Six = 6; public static final int Num_Seven = 7; public static final int Num_Eight = 8; public static final int Num_Nine = 9; public static final int Btn_Dismiss = 10; public static final int Btn_Del = 11; private View contentView; private Handler handler; public MyPopupWindow(final Activity context, Handler handler) { super(context); this.handler = handler; LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context .LAYOUT_INFLATER_SERVICE); contentView = inflater.inflate(R.layout.activity_activity__launch, null); initEvent(); int height = context.getWindowManager().getDefaultDisplay().getHeight(); int width = context.getWindowManager().getDefaultDisplay().getWidth(); this.setContentView(contentView); this.setWidth(width); this.setHeight(height / 3 + 10); this.setFocusable(true); this.setOutsideTouchable(true); this.update(); this.setBackgroundDrawable(new ColorDrawable()); this.setAnimationStyle(R.style.AnimationPreview); } private void initEvent() { TextView tv00 = (TextView) contentView.findViewById(R.id.tv_00); TextView tv01 = (TextView) contentView.findViewById(R.id.tv_01); TextView tv02 = (TextView) contentView.findViewById(R.id.tv_02); TextView tv03 = (TextView) contentView.findViewById(R.id.tv_03); TextView tv04 = (TextView) contentView.findViewById(R.id.tv_04); TextView tv05 = (TextView) contentView.findViewById(R.id.tv_05); TextView tv06 = (TextView) contentView.findViewById(R.id.tv_06); TextView tv07 = (TextView) contentView.findViewById(R.id.tv_07); TextView tv08 = (TextView) contentView.findViewById(R.id.tv_08); TextView tv09 = (TextView) contentView.findViewById(R.id.tv_09); TextView btnPoint = (TextView) contentView.findViewById(R.id.btn_point); ImageView btnDel = (ImageView) contentView.findViewById(R.id.btn_del); tv00.setOnClickListener(this); tv01.setOnClickListener(this); tv02.setOnClickListener(this); tv03.setOnClickListener(this); tv04.setOnClickListener(this); tv05.setOnClickListener(this); tv06.setOnClickListener(this); tv07.setOnClickListener(this); tv08.setOnClickListener(this); tv09.setOnClickListener(this); btnPoint.setOnClickListener(this); btnDel.setOnClickListener(this); } public void showMyPopUpWindow(View parent) { this.showAtLocation(parent, Gravity.BOTTOM, 0, 0); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.tv_00: handler.sendEmptyMessage(this.Num_Zero); break; case R.id.tv_01: handler.sendEmptyMessage(this.Num_One); break; case R.id.tv_02: handler.sendEmptyMessage(this.Num_Two); break; case R.id.tv_03: handler.sendEmptyMessage(this.Num_Three); break; case R.id.tv_04: handler.sendEmptyMessage(this.Num_Four); break; case R.id.tv_05: handler.sendEmptyMessage(this.Num_Five); break; case R.id.tv_06: handler.sendEmptyMessage(this.Num_Six); break; case R.id.tv_07: handler.sendEmptyMessage(this.Num_Seven); break; case R.id.tv_08: handler.sendEmptyMessage(this.Num_Eight); break; case R.id.tv_09: handler.sendEmptyMessage(this.Num_Nine); break; case R.id.btn_point: this.dismiss(); break; case R.id.btn_del: handler.sendEmptyMessage(this.Btn_Del); break; } } }
然後,在呼叫該類的Activity中用Handler訊息機制進行相對應的處理即可。
Demo地址:http://pan.baidu.com/s/1hrmVYyo