返回顶部
首页 > 资讯 > 移动开发 >Android仿支付宝支付密码输入框
  • 719
分享到

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

输入支付宝输入框Android 2022-06-06 06:06:04 719人浏览 独家记忆
摘要

本文实例为大家分享了Android实现一个仿支付宝支付密码的输入框,主要实现如下: PassWordView.java package com.jackie.alipay.p

本文实例为大家分享了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; 
 
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); 
 } 
  
 @TargetApi(Build.VERSION_CODES.LOLLIPOP) 
 private void drawRoundRect(Canvas canvas) { 
 canvas.drawRoundRect(0, 0, mWidth, mHeight, 12, 12, mBorderPaint); 
 } 
  
 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); 
 } 
 } 
  
 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实现动态显示或隐藏密码输入框的内容Android 仿支付宝密码输入框效果android仿微信支付宝的支付密码输入框示例Android高仿微信支付密码输入控件Android仿微信/支付宝密码输入框Android自定义密码输入框和数字键盘Android程序开发之防止密码输入错误 密码明文显示功能Android实现支付宝6位密码输入界面Android自定义密码输入EditTextLayoutAndroid自定义PasswordInputView密码输入


--结束END--

本文标题: Android仿支付宝支付密码输入框

本文链接: https://lsjlt.com/news/23346.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作