1. 程式人生 > 程式設計 >Android自定義控制元件實現簡訊驗證碼自動填充

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,paint);
   }
  });

簡訊驗證碼自動填充

SmsVerifyCodeFilter filter = new SmsVerifyCodeFilter();
filter.setSmsSenderStart("1096");
filter.setSmsSenderContains("5225");
filter.setSmsBodyStart("驗證簡訊:");
filter.setSmsBodyContains("驗證碼");
filter.setVerifyCodeCount(verifyCodeView.getVcTextCount());
verifyCodeView.startListen(filter);

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。