Android自定义控件实现短信验证码自动填充
- 作者: 嗫?暁雲?
- 来源: 51数据库
- 2020-08-15
这篇文章主要为大家详细介绍了Android自定义控件实现短信验证码自动填充,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
VerifyCodeView
VerifyCodeView是一个用于输入验证码的Android自定义控件,它支持数字类型的验证码,支持自定义外观,并且支持短信验证码自动填充。
项目地址:VerifyCodeView
在项目中引入VerifyCodeView
Gradle
dependencies { compile 'com.github.gongw:verifycodeview:1.0.2' }
Maven
<dependency> <groupId>com.github.gongw</groupId> <artifactId>verifycodeview</artifactId> <version>1.0.2</version> <type>pom</type> </dependency>
支持的自定义属性
- vcTextCount - 验证码长度
- vcTextColor - 验证码字体颜色
- vcTextSize - 验证码字体大小,单位sp
- vcTextFont - 验证码字体样式文件路径(assets中的路径)
- vcDividerWidth - 每个验证码之间的间隔距离,单位dp
- vcWrapper - 包裹验证码的外观样式
- vcWrapperStrokeWidth - 包裹验证码的外观的线条宽度,单位dp
- vcWrapperColor - 包裹验证码的外观颜色
- vcNextWrapperColor - 包裹将要被填充的验证码的外观颜色
使用示例
xml
<com.github.gongw.VerifyCodeView android:layout_width="240dp" android:layout_height="50dp" android:layout_marginTop="42dp" app:vcTextColor="#b63b21" app:vcTextCount="4" app:vcTextSize="36sp" app:vcDividerWidth="8dp" app:vcWrapper="centerLine" app:vcWrapperColor="#313335" app:vcNextWrapperColor="#b63b21" app:vcWrapperStrokeWidth="2dp" />
java
verifycodeView.setOnAllFilledListener(new VerifyCodeView.OnAllFilledListener() { @Override public void onAllFilled(String text) { Toast.makeText(MainActivity.this, "filled by "+text, Toast.LENGTH_SHORT).show(); } });
自定义外观
verifycodeView.setVcWrapper(new VerifyCodeWrapper() { @Override public boolean isCovered() { //whether the wrapper and verify code display together return false; } @Override public void drawWrapper(Canvas canvas, Paint paint, RectF rectF, RectF textRectF) { //draw your own wrapper canvas.drawLine(textRectF.left - textRectF.width()/2, rectF.height()/2, textRectF.right + textRectF.width() / 2, rectF.height()/2, paint); } });
短信验证码自动填充
SmsVerifyCodeFilter filter = new SmsVerifyCodeFilter(); filter.setSmsSenderStart("1096"); filter.setSmsSenderContains("5225"); filter.setSmsBodyStart("验证短信:"); filter.setSmsBodyContains("验证码"); filter.setVerifyCodeCount(verifyCodeView.getVcTextCount()); verifyCodeView.startListen(filter);
以上就是本文的全部内容,希望对大家的学习有所帮助,
推荐阅读
热点文章
android中Bitmap用法(显示,保存,缩放,旋转)实例分析
12
android 仿微信聊天气泡效果实现思路
1
Android的尺度,drawable-xxxxxxx
2
Codeforces Round #656 (Div. 3) (C、D题)
1
Android之handler异步消息处理机制解析
6
GridView中图片显示出现上下间距过大,左右图片显示类似瀑布流的问题
0
AsyncTask的简单使用
5
两个简单Fragment之间的通信(三种方式)
18
uboot修改设置boot参数命令
41
android中实现从相册中一次性获取多张图片与拍照,并将选中的图片显示出来
2