android仿支付宝密码输入框效果
作者:zhoushenxian 发布时间:2021-08-06 12:14:01
标签: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();
}
}
效果:
github:https://github.com/zhouguizhi/ZhiFuBaoPwdView
来源:http://blog.csdn.net/coderinchina/article/details/52145793
0
投稿
猜你喜欢
- 前言最常用的对字符串操作的类有三个,分别是String,StringBuilder,StringBuffer,下面将会详细的说说这三个类..
- 相信大家一定遇到过某些App在手机桌面打开时会出现短暂或者几秒钟的白屏情况吧,没错那是应用程序启动后系统默认的背景色,此时应用的第一个Act
- 前言之前写过一篇关于配置中心对配置内容加密解密的介绍:《Spring Cloud构建微服务架构:分布式配置中心(加密解密) 》。在这篇文章中
- 一元运算符,也叫单项算符,一目运算符,一元算符 ,英文名字:UnaryOperator。描述:接受一个参数为类型T,返回值类型也为T。源码:
- 大致思路:注解实现方式:就是用 反射机制. 获取指定的包下使用了注解的类,存储在一个map容器, 然后获取map容器下类的属性, 利用反射给
- 使用 replace 函数动态填充字符串String str="Hello {0},我是 {1},今年{2}岁"
- 代码如下import java.util.concurrent.Callable;import java.util.concurrent.E
- 前言:已经有一个月没写点什么了,感觉心里空落落的。今天再来篇干货,想要学习Webapi的园友们速速动起来,跟着博主一起来学习吧。之前分享过一
- C#实体类转换方式将一个实体类的数据赋值到另一个实体类中(亦或者实现深拷贝)。以下提供两种方式一种是序列化一种是泛型+反射实现功能两个实体类
- 实例:用户输入一个日期,要求输出这个日期是星期几和在这一年中的第几天://声明一个DateTime类型的变量用于存放用户输入的日期DateT
- 前言本文主要给大家介绍的是关于Java对xls文件进行读写操作的相关内容,分享出来供大家参考学习,下面话不多说了,来一起看看详细的介绍:wi
- 错误表现如下图示,这两处可能报错发生原因一个pom文件里面,只能有有一对denpendencies标签,所有的配置都要放入这个depende
- 一个Java程序的执行要经过编译和执行(解释)这两个步骤,同时Java又是面向对象的编程语言。当子类和父类存在同一个方法,子类重写了父类的方
- 背景众所周知,所有被打开的系统资源,比如流、文件或者Socket连接等,都需要被开发者手动关闭,否则随着程序的不断运行,资源泄露将会累积成重
- 本文将通过阅读spring源码,分析@ComponentScan注解扫描组件的原理。和@Bean注解一样,@ComponentScan注解也
- java * 类可以分为两种。 静态代理:由程序员创建或特定工具自动生成源代码,再对其编译。在程序运行前,代理类的.class文件就已经存
- 本文实例为大家分享了C++实现俄罗斯方块的具体代码,供大家参考,具体内容如下先是效果图:主菜单:游戏:设置:错误处理:代码:#include
- zuul动态路由网关服务是流量的唯一入口。不能随便停服务。所以动态路由就显得尤为必要。数据库动态路由基于事件刷新机制热修改zuul的路由属性
- 本文采用C#实例讲解了处理图片为浮雕效果的实现方法,这在PS中是一个常见的功能,也是C#中的一个简单的图像处理例子。程序先读取原图,然后依次
- 引言在开发中有时候经常需要一些Http请求,请求数据,下载内容,也有一些简单的分布式应用直接使用Http请求作为跨应用的交互协议。在Java