溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

怎么在Android中自定義控件實現(xiàn)自動填充短信驗證碼

發(fā)布時間:2021-05-27 17:51:01 來源:億速云 閱讀:122 作者:Leah 欄目:移動開發(fā)

怎么在Android中自定義控件實現(xiàn)自動填充短信驗證碼?針對這個問題,這篇文章詳細(xì)介紹了相對應(yīng)的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。

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);

關(guān)于怎么在Android中自定義控件實現(xiàn)自動填充短信驗證碼問題的解答就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注億速云行業(yè)資訊頻道了解更多相關(guān)知識。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI