Android 自定義驗(yàn)證碼輸入框的實(shí)例代碼(支持粘貼連續(xù)性)
需求
1、能自定義輸入框個(gè)數(shù)和樣式
2、支持長(zhǎng)按粘貼或剪切板內(nèi)容自動(dòng)填充(粘貼連續(xù)性)
其中第2點(diǎn)是最為重要的,正是其他人沒(méi)有這點(diǎn),逼得自己弄一個(gè)
示例
別人的示例:
粘貼居然不支持連續(xù)性,只能粘貼第一個(gè)字符,所以用的有點(diǎn)難受
自己的示例:
原理
大致是Edittext + n* TextView,然后設(shè)置edittext字體跟背景顏色都為透明,隱藏光標(biāo)
Edittext:監(jiān)聽(tīng)edittext每次輸入一個(gè)字符就賦值到對(duì)應(yīng)的TextView上,然后在清空自己
下劃線:在TextView下面添加View
光標(biāo):這里的每個(gè)TextView的焦點(diǎn)光標(biāo)其實(shí)對(duì)View設(shè)置了ValueAnimator
粘貼:粘貼彈窗是自定義的PopupWindow
源碼有詳細(xì)注釋,這里就不一一說(shuō)明
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() { } }); //清除驗(yàn)證碼 view.clearCode();
Attributes
VCInputType
輸入框背景色支持類型
1、@drawable/xxx
2、@color/xxx
3、#xxxxxx
總結(jié)
以上所述是小編給大家介紹的Android 自定義驗(yàn)證碼輸入框的實(shí)例代碼(支持粘貼連續(xù)性),希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
如果你覺(jué)得本文對(duì)你有幫助,歡迎轉(zhuǎn)載,煩請(qǐng)注明出處,謝謝!
相關(guān)文章
android開(kāi)發(fā)教程之自定義屬性用法詳解
這篇文章主要介紹了android開(kāi)發(fā)中的自定義屬性用法詳解,需要的朋友可以參考下2014-04-04Android ToolBar整合實(shí)例使用方法詳解
這篇文章主要為大家詳細(xì)介紹了Android ToolBar整合實(shí)例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-02-02Flutter有狀態(tài)組件StatefulWidget生命周期詳解
這篇文章主要為大家介紹了Flutter有狀態(tài)組件StatefulWidget生命周期詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01

詳解Android XML中引用自定義內(nèi)部類view的四個(gè)why

Android AnalogClock簡(jiǎn)單使用方法實(shí)例

Android 屬性動(dòng)畫ValueAnimator與插值器詳解

android實(shí)現(xiàn)通過(guò)NFC讀取卡號(hào)

Android內(nèi)存優(yōu)化操作方法梳理總結(jié)

Android利用滑動(dòng)菜單框架實(shí)現(xiàn)滑動(dòng)菜單效果