Android 自定義驗(yàn)證碼輸入框的實(shí)例代碼(支持粘貼連續(xù)性)
需求
1、能自定義輸入框個數(shù)和樣式
2、支持長按粘貼或剪切板內(nèi)容自動填充(粘貼連續(xù)性)
其中第2點(diǎn)是最為重要的,正是其他人沒有這點(diǎn),逼得自己弄一個
示例
別人的示例:

粘貼居然不支持連續(xù)性,只能粘貼第一個字符,所以用的有點(diǎn)難受
自己的示例:

原理
大致是Edittext + n* TextView,然后設(shè)置edittext字體跟背景顏色都為透明,隱藏光標(biāo)
Edittext:監(jiān)聽edittext每次輸入一個字符就賦值到對應(yīng)的TextView上,然后在清空自己
下劃線:在TextView下面添加View
光標(biāo):這里的每個TextView的焦點(diǎn)光標(biāo)其實(shí)對View設(shè)置了ValueAnimator
粘貼:粘貼彈窗是自定義的PopupWindow
源碼有詳細(xì)注釋,這里就不一一說明
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ù)性),希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
如果你覺得本文對你有幫助,歡迎轉(zhuǎn)載,煩請注明出處,謝謝!
相關(guān)文章
Android LayoutInflater深入分析及應(yīng)用
這篇文章主要介紹了Android LayoutInflater分析的相關(guān)資料,需要的朋友可以參考下2017-02-02
探討:android項(xiàng)目開發(fā) 統(tǒng)籌兼顧 需要考慮的因素
本篇文章是對基于android項(xiàng)目開發(fā) 統(tǒng)籌兼顧 需要考慮的因素進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06
Android Studio error: Unable to start the daemon process的解決方
這篇文章主要介紹了在 Android Studio 上新建項(xiàng)目,出現(xiàn) Unable to start the daemon process問題的幾種的解決方法,需要的朋友可以參考下2020-10-10
Android ApplicationInfo 應(yīng)用程序信息的詳解
這篇文章主要介紹了Android ApplicationInfo 應(yīng)用程序信息的詳解的相關(guān)資料,希望通過本文能幫助到大家,需要的朋友可以參考下2017-10-10
Android sqlite cursor的遍歷實(shí)例詳解
Android BottomNavigationView結(jié)合ViewPager實(shí)現(xiàn)底部導(dǎo)航欄步驟詳解

