前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Android自定义控件实现短信验证码自动填充

Android自定义控件实现短信验证码自动填充

作者头像
砸漏
发布2020-10-16 15:14:06
1.3K0
发布2020-10-16 15:14:06
举报
文章被收录于专栏:恩蓝脚本

VerifyCodeView

VerifyCodeView是一个用于输入验证码的Android自定义控件,它支持数字类型的验证码,支持自定义外观,并且支持短信验证码自动填充。

项目地址:VerifyCodeView

在项目中引入VerifyCodeView

Gradle

代码语言:javascript
复制
dependencies {
 compile 'com.github.gongw:verifycodeview:1.0.2'
}

Maven

代码语言:javascript
复制
<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

代码语言:javascript
复制
<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

代码语言:javascript
复制
verifycodeView.setOnAllFilledListener(new VerifyCodeView.OnAllFilledListener() {
  @Override
  public void onAllFilled(String text) {
   Toast.makeText(MainActivity.this, "filled by "+text, Toast.LENGTH_SHORT).show();
  }
 });

自定义外观

代码语言:javascript
复制
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);
   }
  });

短信验证码自动填充

代码语言:javascript
复制
SmsVerifyCodeFilter filter = new SmsVerifyCodeFilter();
filter.setSmsSenderStart("1096");
filter.setSmsSenderContains("5225");
filter.setSmsBodyStart("验证短信:");
filter.setSmsBodyContains("验证码");
filter.setVerifyCodeCount(verifyCodeView.getVcTextCount());
verifyCodeView.startListen(filter);

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

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020-09-11 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
验证码
腾讯云新一代行为验证码(Captcha),基于十道安全栅栏, 为网页、App、小程序开发者打造立体、全面的人机验证。最大程度保护注册登录、活动秒杀、点赞发帖、数据保护等各大场景下业务安全的同时,提供更精细化的用户体验。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档