简单实现Android验证码
作者:lizebin_bin 时间:2021-12-02 07:09:17
安卓验证码的简单实现
我们经常在登录或者注册的时候要求输入验证码,这里简单介绍一下一种方法
效果如下
首先是要获取 随机的四个字母组合,我这里是将26个字母存储到一个数组中,然后随机生成4个下标值,取这四个下标值对应的字母作为验证码。
public class RandomChars {
char[] chars;
public RandomChars() {
chars = new char[26];
for (int i = 0; i < 26; i++) {
chars[i] = (char) (i + 65);
}
}
public char[] get4Chars() {
char[] rlt = new char[4];
for (int i = 0; i < rlt.length; i++) {
int randomIndex = (int) (Math.random() * 26);
rlt[i] = chars[randomIndex];
}
return rlt;
}
}
自定义一个CodeView进行验证码的绘制,主要在onDraw方法中操作,学艺不精,还不能好好在onMeasure中控制大小位置等。
float unitWidth = (float) getWidth() / (float) chars.length;
for (int i = 0; i < chars.length; i++) {
String str = chars[i] + "";
textPaint.getTextBounds(str, 0, str.length(), mRect);
resetColor();
int angel = (int) (Math.random()*(8-(-8)+1)+(-8));
canvas.rotate(angel);//旋转字母,随机角度
canvas.drawText(str, i * unitWidth + 5, getHeight() / 2 - mRect.centerY(), textPaint);
/**
* 很关键,旋转
*/
canvas.save();//保存状态
canvas.restore();//恢复
}
/**
* 重新设置随机颜色
*/
private void resetColor() {
int r = (int) (Math.random() * 230 - 30);
int g = (int) (Math.random() * 230 - 30);
int b = (int) (Math.random() * 230 - 30);
textPaint.setColor(Color.rgb(r, g, b));
}
设置该控件并传入四个字符就ok了,验证是否输入正确的时候,考虑到大小写问题,所以将输入的字母全部转换成大写,一般都是不区分大小写。
submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String inputStr = input.getText().toString();
inputStr = inputStr.toUpperCase();
str = str.toUpperCase();
if (str.equals(inputStr)) {
Toast.makeText(MainActivity.this, "输入正确", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(MainActivity.this, "验证码输入错误", Toast.LENGTH_SHORT).show();
char[] getchar = randomChars.get4Chars();
str = new String(getchar);
codeView.setChars(getchar);
}
}
});
感觉还有挺多不足的地方,以后继续改进吧!
标签:Android,验证码
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
详解Java类库的概念以及import的使用方法
2022-04-18 06:56:54
![](https://img.aspxhome.com/file/2023/4/62774_0s.png)
java的内部类和外部类用法讲解
2022-10-18 21:14:41
![](https://img.aspxhome.com/file/2023/2/110822_0s.jpg)
Hutool开发MapUtil工具类使用示例
2022-10-21 22:49:27
![](https://img.aspxhome.com/file/2023/3/63693_0s.jpg)
Unity实现跑马灯抽奖效果
2022-10-09 04:09:54
![](https://img.aspxhome.com/file/2023/4/67054_0s.jpg)
springboot调用支付宝第三方接口(沙箱环境)
2023-11-25 06:12:08
![](https://img.aspxhome.com/file/2023/8/58748_0s.png)
Android仿微信清理内存图表动画(解决surfaceView屏幕闪烁问题)demo实例详解
2023-05-31 15:35:27
![](https://img.aspxhome.com/file/2023/7/111737_0s.gif)
Java单例模式简单示例
2022-04-24 03:46:20
![](https://img.aspxhome.com/file/2023/3/61843_0s.png)
Java concurrency之非公平锁_动力节点Java学院整理
2022-07-31 22:04:37
详细解读Java的Lambda表达式
2021-12-30 15:32:36
![](https://img.aspxhome.com/file/2023/0/71650_0s.jpg)
C# 数独求解算法的实现
2022-04-04 18:48:57
![](https://img.aspxhome.com/file/2023/2/68102_0s.png)
java中replaceAll替换圆括号实例代码
2023-09-30 16:29:54
Java 中如何使用 stream 流
2021-10-09 01:13:37
SpringBoot拦截 器如何获取http请求参数
2023-11-28 19:40:48
![](https://img.aspxhome.com/file/2023/6/59926_0s.png)
解决MyEclipse出现the user operation is waiting的问题
2022-05-02 21:44:07
![](https://img.aspxhome.com/file/2023/0/64750_0s.gif)
Flutter利用Hero组件实现自定义路径效果的动画
2023-06-25 13:46:29
![](https://img.aspxhome.com/file/2023/3/85453_0s.png)
Java中Stream流中map和forEach的区别详解
2022-07-09 12:24:43
![](https://img.aspxhome.com/file/2023/8/108428_0s.jpg)
C#实现的ZPL条码打印类完整实例
2022-12-06 14:35:05
详解SpringMVC常用注解功能及属性
2021-12-29 02:49:23
Android 二维码 生成和识别二维码 附源码下载
2023-05-01 12:45:01
![](https://img.aspxhome.com/file/2023/8/87298_0s.jpg)
Android实现无标题栏全屏的方法
2023-06-25 11:14:27