Android 自定義驗證碼輸入框的實例代碼(支持粘貼連續(xù)性)
需求
1、能自定義輸入框個數(shù)和樣式
2、支持長按粘貼或剪切板內(nèi)容自動填充(粘貼連續(xù)性)
其中第2點是最為重要的,正是其他人沒有這點,逼得自己弄一個
示例
別人的示例:
粘貼居然不支持連續(xù)性,只能粘貼第一個字符,所以用的有點難受
自己的示例:
原理
大致是Edittext + n* TextView,然后設(shè)置edittext字體跟背景顏色都為透明,隱藏光標
Edittext:監(jiān)聽edittext每次輸入一個字符就賦值到對應(yīng)的TextView上,然后在清空自己
下劃線:在TextView下面添加View
光標:這里的每個TextView的焦點光標其實對View設(shè)置了ValueAnimator
粘貼:粘貼彈窗是自定義的PopupWindow
源碼有詳細注釋,這里就不一一說明
Github
https://github.com/WShaobin/VerificationCodeInputView
Gradle
Step 1. Add it in your root build.gradle at the end of repositories:
allprojects { repositories { ... maven { url 'https://jitpack.io' } } }
Step 2. Add the dependency:
dependencies { implementation 'com.github.WShaobin:VerificationCodeInputView:1.0.2' }
How to use
In layout
<com.wynsbin.vciv.VerificationCodeInputView android:id="@+id/vciv_code" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="48dp" android:gravity="center" android:orientation="horizontal" app:vciv_et_background="@android:color/white" app:vciv_et_foucs_background="@android:color/holo_orange_dark" app:vciv_et_cursor_color="@color/colorPrimary" app:vciv_et_height="58dp" app:vciv_et_inputType="number" app:vciv_et_number="6" app:vciv_et_text_color="@android:color/black" app:vciv_et_text_size="18sp" app:vciv_et_underline_default_color="@android:color/holo_green_dark" app:vciv_et_underline_focus_color="@android:color/holo_blue_bright" app:vciv_et_underline_height="2dp" app:vciv_et_underline_show="true" app:vciv_et_width="58dp" />
In Java Code
VerificationCodeInputView view = findViewById(R.id.vciv_code); view.setOnInputListener(new VerificationCodeInputView.OnInputListener() { @Override public void onComplete(String code) { Toast.makeText(MainActivity.this, code, Toast.LENGTH_SHORT).show(); } @Override public void onInput() { } }); //清除驗證碼 view.clearCode();
Attributes
VCInputType
輸入框背景色支持類型
1、@drawable/xxx
2、@color/xxx
3、#xxxxxx
總結(jié)
以上所述是小編給大家介紹的Android 自定義驗證碼輸入框的實例代碼(支持粘貼連續(xù)性),希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
如果你覺得本文對你有幫助,歡迎轉(zhuǎn)載,煩請注明出處,謝謝!
相關(guān)文章
Flutter有狀態(tài)組件StatefulWidget生命周期詳解
這篇文章主要為大家介紹了Flutter有狀態(tài)組件StatefulWidget生命周期詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-01-01React-Native之Android(6.0及以上)權(quán)限申請詳解
這篇文章主要介紹了React-Native之Android(6.0及以上)權(quán)限申請詳解,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-11-11詳解Android XML中引用自定義內(nèi)部類view的四個why
本篇文章主要介紹了詳解Android XML中引用自定義內(nèi)部類view,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。2016-12-12Android 屬性動畫ValueAnimator與插值器詳解
這篇文章主要介紹了Android 屬性動畫ValueAnimator與插值器詳解的相關(guān)資料,需要的朋友可以參考下2017-05-05Android內(nèi)存優(yōu)化操作方法梳理總結(jié)
這篇文章主要介紹了Android 內(nèi)存優(yōu)化知識點梳理總結(jié),Android 操作系統(tǒng)給每個進程都會分配指定額度的內(nèi)存空間,App 使用內(nèi)存來進行快速的文件訪問交互,長時間如此便需要優(yōu)化策略,文章分享優(yōu)化知識點總結(jié),需要的朋友可以參考一下2022-11-11