欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android仿支付宝支付密码输入框

程序员文章站 2024-03-02 23:27:10
本文实例为大家分享了android实现一个仿支付宝支付密码的输入框,主要实现如下: passwordview.java package com.jackie.a...

本文实例为大家分享了android实现一个仿支付宝支付密码的输入框,主要实现如下:

passwordview.java

package com.jackie.alipay.password; 
 
import android.annotation.targetapi; 
import android.content.context; 
import android.graphics.canvas; 
import android.graphics.color; 
import android.graphics.paint; 
import android.os.build; 
import android.util.attributeset; 
import android.widget.edittext; 
import android.widget.toast; 
 
/** 
 * created by administrator on 2016/10/31. 
 */ 
 
public class passwordview extends edittext { 
 private paint mborderpaint; //外框画笔 
 private paint mlinepaint; //线的画笔 
 private paint mpasswordpaint; //密码画笔 
 private int mpasswordtextlength; //输入密码的长度 
 private int mwidth; 
 private int mheight; 
 
 private static final int password_length = 6;//密码的长度 
 private static final int password_radius = 15; 
 
 public passwordview(context context) { 
 this(context, null); 
 } 
 
 public passwordview(context context, attributeset attrs) { 
 this(context, attrs, 0); 
 } 
 
 public passwordview(context context, attributeset attrs, int defstyleattr) { 
 super(context, attrs, defstyleattr); 
 
 initview(); 
 } 
 
 private void initview() { 
 setfocusable(true); 
 
 mborderpaint = new paint(); 
 mborderpaint.setstrokewidth(8); 
 mborderpaint.setcolor(color.white); 
 mborderpaint.setstyle(paint.style.fill); 
 
 mlinepaint = new paint(); 
 mlinepaint.setcolor(color.parsecolor("#838b8b")); 
 mlinepaint.setstrokewidth(4); 
 
 mpasswordpaint = new paint(); 
 mpasswordpaint.setcolor(color.black); 
 mpasswordpaint.setstrokewidth(12); 
 } 
 
 @override 
 protected void ondraw(canvas canvas) { 
 super.ondraw(canvas); 
 
 mwidth = getmeasuredwidth(); 
 mheight = getmeasuredheight(); 
 
 drawroundrect(canvas); 
 drawline(canvas); 
 drawpassword(canvas); 
 } 
 
 /** 
 * 绘制圆角矩形背景 
 * @param canvas 
 */ 
 @targetapi(build.version_codes.lollipop) 
 private void drawroundrect(canvas canvas) { 
 canvas.drawroundrect(0, 0, mwidth, mheight, 12, 12, mborderpaint); 
 
 } 
 
 /** 
 * 绘制分割线 
 * @param canvas 
 */ 
 private void drawline(canvas canvas) { 
 for (int i = 1; i < password_length; i++) { 
  float x = mwidth * i / password_length; 
  canvas.drawline(x, 12, x, mheight - 12, mlinepaint); 
 } 
 } 
 
 /** 
 * 绘制密码 
 * @param canvas 
 */ 
 private void drawpassword(canvas canvas) { 
 float cx, cy = mheight / 2; 
 float half = mwidth / password_length / 2; 
 for (int i = 0; i < mpasswordtextlength; i++) { 
  cx = mwidth * i / password_length + half; 
  canvas.drawcircle(cx, cy, password_radius, mpasswordpaint); 
 } 
 } 
 
 @override 
 protected void ontextchanged(charsequence text, int start, int lengthbefore, int lengthafter) { 
 super.ontextchanged(text, start, lengthbefore, lengthafter); 
 
 mpasswordtextlength = text.tostring().length(); 
 
 if (mpasswordtextlength == password_length) { 
  toast.maketext(getcontext(), "您设置的密码为: " + text, toast.length_short).show();; 
 } 
 
 invalidate(); 
 } 
 
 
 public void reset(){ 
 settext(""); 
 invalidate(); 
 } 
}

  效果图如下:

Android仿支付宝支付密码输入框

更多内容请参考专题:android密码使用教程

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。