Android自定義控件實現(xiàn)通用驗證碼輸入框
本文為大家分享了Android實現(xiàn)通用驗證碼輸入框的具體代碼,供大家參考,具體內(nèi)容如下
效果圖
話不多說先上效果圖,可以先先看看是不是自己想要的
閑聊
閑來無事優(yōu)化項目時,發(fā)現(xiàn)原來的驗證碼輸入框,可擴展性不高,就拿來優(yōu)化了一下,說說我開始的的思路吧,最開始是想用自定義View實現(xiàn)的,但是發(fā)現(xiàn)各種畫矩,太煩人了,最后采用的組合控件的形式,Android有現(xiàn)成的控件,用來組合組合就能用,為什么不用呢。
源碼
xml ITEM 布局文件(view_auth_code_input_item.xml)
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:id="@+id/number_tv" style="@style/TextStyleMain" android:layout_width="match_parent" android:layout_height="0mm" android:layout_weight="1" android:maxLength="1" android:text="0" android:textSize="72mm" /> <View android:id="@+id/split_v" android:layout_width="match_parent" android:layout_height="1mm" android:background="@color/colorMain" /> </LinearLayout>
attrs 自定義屬性(attrs.xml)
<?xml version="1.0" encoding="utf-8"?> <resources> <!-- 自定義驗證碼輸入框 屬性 --> <declare-styleable name="AuthCodeInputView"> <!-- 當(dāng)前輸入位分割線顏色 --> <attr name="currentSplitLineColor" format="reference|color" /> <!-- 其他輸入位分割線顏色 --> <attr name="otherSplitLineColor" format="reference|color" /> <!-- 分割線高度 --> <attr name="splitLineHeight" format="dimension" /> <!-- 驗證碼位數(shù) --> <attr name="digit" format="integer" /> <!-- 單個驗證碼寬度 --> <attr name="singleCaptchaWidth" format="dimension" /> <!-- 驗證碼當(dāng)前輸入位字體顏色 --> <attr name="currentTextColor" format="reference|color" /> <!-- 驗證碼當(dāng)前輸入位字體大小 --> <attr name="currentTextSize" format="dimension" /> <!-- 驗證碼其他輸入位字體顏色 --> <attr name="otherTextColor" format="reference|color" /> <!-- 驗證碼其它輸入位字體大小 --> <attr name="otherTextSize" format="dimension" /> <!-- 默認顏色 --> <attr name="defaultColor" format="reference|color" /> <!-- 默認字體大小 --> <attr name="defaultTextSize" format="dimension" /> <!-- 默認間距 --> <attr name="defaultSpacing" format="dimension" /> </declare-styleable> </resources>
組合控件(AuthCodeInputView.java)
import android.content.Context; import android.content.res.TypedArray; import android.graphics.Color; import android.os.Build; import android.text.TextUtils; import android.util.AttributeSet; import android.util.TypedValue; import android.view.Gravity; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.LinearLayout; import android.widget.TextView; import androidx.annotation.ColorInt; import androidx.annotation.RequiresApi; /** * <pre> * <b>author</b> :BraveTou * <b>blog</b> :https://blog.csdn.net/bravetou * <b>time</b> :2020/9/4 16:43 * <b>desc</b> :<pre> * 自定義驗證碼輸入框 * </pre> * </pre> */ public class AuthCodeInputView extends LinearLayout { // <!-- 默認間距 --> private int mDefaultSpacing = 16; // <!-- 默認顏色 --> @ColorInt private int mDefaultColor = Color.BLACK; // <!-- 默認字體大小 --> private int mDefaultTextSize = 36; // <!-- 當(dāng)前輸入位分割線顏色 --> @ColorInt private int mCurrentSplitLineColor = mDefaultColor; // <!-- 其他輸入位分割線顏色 --> @ColorInt private int mOtherSplitLineColor = mDefaultColor; // <!-- 分割線高度 --> private int mSplitLineHeight = 1; // <!-- 驗證碼位數(shù) --> private int mDigit = 4; // <!-- 單個驗證碼寬度 --> private int mSingleCaptchaWidth = 100; // <!-- 驗證碼當(dāng)前輸入位字體顏色 --> @ColorInt private int mCurrentTextColor = mDefaultColor; // <!-- 驗證碼當(dāng)前輸入位字體大小 --> private int mCurrentTextSize = mDefaultTextSize; // <!-- 驗證碼其他輸入位字體顏色 --> @ColorInt private int mOtherTextColor = mDefaultColor; // <!-- 驗證碼其它輸入位字體大小 --> private int mOtherTextSize = mDefaultTextSize; // 記錄當(dāng)前輸入文本 private String mText = ""; public AuthCodeInputView(Context context) { super(context); init(context, null); } public AuthCodeInputView(Context context, AttributeSet attrs) { super(context, attrs); init(context, attrs); } public AuthCodeInputView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(context, attrs); } @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) public AuthCodeInputView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { super(context, attrs, defStyleAttr, defStyleRes); init(context, attrs); } // 初始化 private void init(Context context, AttributeSet attrs) { setOrientation(LinearLayout.HORIZONTAL); setGravity(Gravity.CENTER); if (getChildCount() > 0) { removeAllViews(); } initAttrs(context, attrs); if (mDigit <= 0) { return; } for (int i = 0; i < mDigit; i++) { // 實例化 ITEM 組件 View child = LayoutInflater.from(context).inflate( R.layout.view_auth_code_input_item, this, false); LayoutParams lp = new LayoutParams(mSingleCaptchaWidth, ViewGroup.LayoutParams.MATCH_PARENT); if (i != 0) { lp.leftMargin = mDefaultSpacing; } child.setLayoutParams(lp); setViewAttrs(child, null, false); // 分割線高度只在初始化時設(shè)置一次 View mSplitV = child.findViewById(R.id.split_v); LinearLayout.LayoutParams params = new LinearLayout.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, mSplitLineHeight); mSplitV.setLayoutParams(params); addView(child); } } // 設(shè)置(未)選中屬性 private void setViewAttrs(View child, String text, boolean isSelected) { TextView mNumberTv = child.findViewById(R.id.number_tv); View mSplitV = child.findViewById(R.id.split_v); if (isSelected) { mNumberTv.setTextColor(mCurrentTextColor); mNumberTv.setTextSize(TypedValue.COMPLEX_UNIT_PX, mCurrentTextSize); mSplitV.setBackgroundColor(mCurrentSplitLineColor); } else { mNumberTv.setTextColor(mOtherTextColor); mNumberTv.setTextSize(TypedValue.COMPLEX_UNIT_PX, mOtherTextSize); mSplitV.setBackgroundColor(mOtherSplitLineColor); } mNumberTv.setText(TextUtils.isEmpty(text) ? "" : text); } // 初始化屬性 private void initAttrs(Context context, AttributeSet attrs) { if (null != attrs) { // AttributeSet 屬性值的索引 TypedArray o = context.obtainStyledAttributes(attrs, R.styleable.AuthCodeInputView); // 默認間距 mDefaultSpacing = (int) o.getDimension(R.styleable.AuthCodeInputView_defaultSpacing, 16f); // 獲取默認顏色 mDefaultColor = o.getColor(R.styleable.AuthCodeInputView_defaultColor, Color.BLACK); // 獲取默認字體大小 mDefaultTextSize = (int) o.getDimension(R.styleable.AuthCodeInputView_defaultTextSize , 36f); // 輸入位分割線顏色 mCurrentSplitLineColor = o.getColor(R.styleable.AuthCodeInputView_currentSplitLineColor, mDefaultColor); // 其他輸入位分割線顏色 mOtherSplitLineColor = o.getColor(R.styleable.AuthCodeInputView_otherSplitLineColor, mDefaultColor); // 分割線高度 mSplitLineHeight = (int) o.getDimension(R.styleable.AuthCodeInputView_splitLineHeight , 1f); mSplitLineHeight = mSplitLineHeight <= 1 ? 1 : mSplitLineHeight; // 驗證碼位數(shù) mDigit = o.getInteger(R.styleable.AuthCodeInputView_digit, 4); // 單個驗證碼寬度 mSingleCaptchaWidth = (int) o.getDimension(R.styleable.AuthCodeInputView_singleCaptchaWidth, 100f); // 驗證碼當(dāng)前輸入位字體顏色 mCurrentTextColor = o.getColor(R.styleable.AuthCodeInputView_currentTextColor, mDefaultColor); // 驗證碼當(dāng)前輸入位字體大小 mCurrentTextSize = (int) o.getDimension(R.styleable.AuthCodeInputView_currentTextSize , mDefaultTextSize); // 驗證碼其他輸入位字體顏色 mOtherTextColor = o.getColor(R.styleable.AuthCodeInputView_otherTextColor, mDefaultColor); // 驗證碼其它輸入位字體大小 mOtherTextSize = (int) o.getDimension(R.styleable.AuthCodeInputView_otherTextSize, mDefaultTextSize); // 回收資源 o.recycle(); } } // 追加文本 public void addText(String text) { text = TextUtils.isEmpty(text) ? "" : text; setText(mText + text); } // 刪除文本 public void delText() { int count = TextUtils.isEmpty(mText) ? 0 : mText.length(); if (count > 0) { setText(mText.substring(0, count - 1)); } else { setText(""); } } // 設(shè)置文本 public void setText(String text) { text = text.trim(); int length = TextUtils.isEmpty(text) ? 0 : text.length(); if (length > mDigit) { this.mText = text.substring(0, mDigit); length = mDigit; } else { this.mText = length > 0 ? text : ""; } int count = getChildCount(); for (int i = 0; i < count; i++) { View child = getChildAt(i); if (i + 1 < length) { setViewAttrs(child, String.valueOf(text.charAt(i)), false); } else if (i + 1 == length) { setViewAttrs(child, String.valueOf(text.charAt(i)), true); } else { setViewAttrs(child, null, false); } } } // 獲取文本 public String getText() { return mText; } }
至于效果圖下面那個安全鍵盤源碼就太多了,我就不多了的,我這邊是組合控件實現(xiàn)的,超簡單。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android Studio 3.0被調(diào)方法參數(shù)名提示的取消方法
這篇文章主要介紹了去掉android studio 3.0被調(diào)方法參數(shù)名提示的解決方法,在文章末尾給大家補充介紹了Android Studio 3.0 gradle提示太老的解決方法,非常不錯,需要的朋友可以參考下2017-11-11Android指紋識別功能深入淺出分析到實戰(zhàn)(6.0以下系統(tǒng)解決方案)
指紋識別在現(xiàn)實應(yīng)用中已經(jīng)很多了,本篇文章主要介紹了Android指紋識別功能,具有一定的參考價值,有需要的可以了解一下。2016-11-11Jetpack Compose實現(xiàn)動畫效果的方法詳解
compose為支持動畫提供了大量的 api,通過這些 api 我們可以輕松實現(xiàn)動畫效果。本文將為大家介紹利用compose實現(xiàn)的多種動畫效果的示例代碼,需要的可以參考一下2022-02-02Android利用Sensor(傳感器)實現(xiàn)指南針小功能
這篇文章主要為大家詳細介紹了Android利用Sensor(傳感器)實現(xiàn)指南針小功能的相關(guān)資料,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-02-02