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

android仿支付宝密码输入框效果

程序员文章站 2022-03-02 13:23:36
本文实例为大家分享了android仿支付宝密码输入框展示的具体代码,供大家参考,具体内容如下 这个没什么好分析的,就是一些基本的绘制什么线,矩形什么的,看代码更具体...

本文实例为大家分享了android仿支付宝密码输入框展示的具体代码,供大家参考,具体内容如下

这个没什么好分析的,就是一些基本的绘制什么线,矩形什么的,看代码更具体

布局文件:

<?xml version="1.0" encoding="utf-8"?>
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 >
 <com.example.custompasswordview.passwordview
  android:id="@+id/passwordview"
  android:layout_width="match_parent"
  android:layout_height="105px"
  android:layout_margintop="100px"
  android:layout_marginleft="20px"
  android:layout_marginright="20px"
  android:inputtype="number"
  android:cursorvisible="false"
  android:focusable="true"
  android:focusableintouchmode="true"
  android:enabled="true"
  android:clickable="true"
   />
 <button
  android:id="@+id/btn_pass_reset"
  android:layout_width="250px"
  android:layout_height="90px"
  android:text="重置"
  android:layout_below="@id/passwordview"
  android:layout_margintop="20px"
  android:layout_marginleft="40px"
  android:gravity="center"
  />
</relativelayout>

mainactivity.java

package com.example.custompasswordview;
import android.app.activity;
import android.os.bundle;
import android.view.view;
import android.widget.button;
public class mainactivity extends activity {
 private button btn_pass_reset;
 private passwordview passwordview;
 @override
 protected void oncreate(bundle savedinstancestate) {
  super.oncreate(savedinstancestate);
  setcontentview(r.layout.activity_main);
  btn_pass_reset = (button) findviewbyid(r.id.btn_pass_reset);
  passwordview = (passwordview) findviewbyid(r.id.passwordview);
  btn_pass_reset.setonclicklistener(new view.onclicklistener() {
   @override
   public void onclick(view v) {
    passwordview.setempeytext();
   }
  });
 }
}

自定义edittext输入框

package com.example.custompasswordview;
import android.content.context;
import android.graphics.canvas;
import android.graphics.color;
import android.graphics.paint;
import android.util.attributeset;
import android.util.log;
import android.widget.edittext;
import android.widget.toast;

/**
 * created by adminis on 2016/8/7.
 */
public class passwordview extends edittext {
 private static final string tag ="passwordview" ;
 private paint bordpaint;//外框画笔
 private paint linepaint;//线 的画笔
 private paint passtextpaint;//密码画笔
 private int width;
 private int height;
 private int passwordlength = 6;//代码的长度
 private int textlength;
 private int 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);
  initpaint();
 }

 /**
  * 初始化画笔
  */
 private void initpaint() {
  setfocusable(true);
  bordpaint = new paint();
  bordpaint.setstrokewidth(8);
  bordpaint.setcolor(color.white);
  bordpaint.setstyle(paint.style.fill);

  linepaint = new paint();
  linepaint.setcolor(color.parsecolor("#838b8b"));
  linepaint.setstrokewidth(4);

  passtextpaint = new paint();
  passtextpaint.setcolor(color.parsecolor("#000000"));
  passtextpaint.setstrokewidth(12);

 }

 @override
 protected void ondraw(canvas canvas) {
  super.ondraw(canvas);
  height = getmeasuredheight();
  width = getmeasuredwidth();
  drawroundrect(canvas);
  drawline(canvas);
  drawtextpass(canvas);
 }

 /**
  * 绘制密码
  * @param canvas
  */
 private void drawtextpass(canvas canvas) {
  float cx, cy = height/ 2;
  float half = width / passwordlength / 2;
  for(int i = 0; i < textlength; i++) {
   cx = width * i / passwordlength + half;
   canvas.drawcircle(cx, cy, radius, passtextpaint);
  }
 }

 /**
  * 绘制线
  * @param canvas
  */
 private void drawline(canvas canvas) {
  for (int i = 1; i < passwordlength; i++) {
   float x = width * i / passwordlength;
   canvas.drawline(x, 12, x, height-12, linepaint);
  }
 }
 /**
  * 绘制背景
  * @param canvas
  */
 private void drawroundrect(canvas canvas) {
   canvas.drawroundrect(0,0,width,height,12,12,bordpaint);
 }
 @override
 protected void ontextchanged(charsequence text, int start, int lengthbefore, int lengthafter) {
  super.ontextchanged(text, start, lengthbefore, lengthafter);
  this.textlength = text.tostring().length();
  if(textlength==6){
   toast.maketext(getcontext(),"您设置的密码为"+text,toast.length_short).show();
  }
  invalidate();
 }
 public void setempeytext(){
  settext("");
  invalidate();
 }
}


效果:

android仿支付宝密码输入框效果

github:https://github.com/zhouguizhi/zhifubaopwdview

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