av一区二区在线观看_亚洲男人的天堂网站_日韩亚洲视频_在线成人免费_欧美日韩精品免费观看视频_久草视

您的位置:首頁技術(shù)文章
文章詳情頁

Android自定義控件實現(xiàn)通用驗證碼輸入框

瀏覽:6日期:2022-09-21 08:00:54

本文為大家分享了Android實現(xiàn)通用驗證碼輸入框的具體代碼,供大家參考,具體內(nèi)容如下

效果圖

話不多說先上效果圖,可以先先看看是不是自己想要的

Android自定義控件實現(xiàn)通用驗證碼輸入框

Android自定義控件實現(xiàn)通用驗證碼輸入框

閑聊

閑來無事優(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: android:layout_width='match_parent' android:layout_height='0mm' android:layout_weight='1' android:maxLength='1' android:text='0' android:textSize='72mm' /> <View android: 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' /> <!-- 默認(rèn)顏色 --> <attr name='defaultColor' format='reference|color' /> <!-- 默認(rèn)字體大小 --> <attr name='defaultTextSize' format='dimension' /> <!-- 默認(rèn)間距 --> <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 { // <!-- 默認(rèn)間距 --> private int mDefaultSpacing = 16; // <!-- 默認(rèn)顏色 --> @ColorInt private int mDefaultColor = Color.BLACK; // <!-- 默認(rèn)字體大小 --> 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); // 默認(rèn)間距 mDefaultSpacing = (int) o.getDimension(R.styleable.AuthCodeInputView_defaultSpacing, 16f); // 獲取默認(rèn)顏色 mDefaultColor = o.getColor(R.styleable.AuthCodeInputView_defaultColor, Color.BLACK); // 獲取默認(rèn)字體大小 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í)有所幫助,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: Android
相關(guān)文章:
主站蜘蛛池模板: 亚洲成人一级 | 欧美性网站 | 成人污污视频 | www操操| 亚洲一区中文字幕在线观看 | 午夜一级做a爰片久久毛片 精品综合 | 亚洲精品日本 | 精产国产伦理一二三区 | 精品国产久 | 一级毛片网 | 久久久影院 | 国产成人高清成人av片在线看 | 成人h视频在线观看 | 日韩av成人在线 | 欧美看片| 亚洲一区二区三区在线播放 | 久久久久久亚洲欧洲 | 亚洲成色777777在线观看影院 | 中文字幕在线观看精品 | 521av网站| 精品国产第一区二区三区 | 国产一区二区三区四区五区加勒比 | 午夜视频在线播放 | 51ⅴ精品国产91久久久久久 | 中日字幕大片在线播放 | 男人电影天堂 | 久久蜜桃资源一区二区老牛 | 久在线精品视频 | 欧美激情国产日韩精品一区18 | 久久国产精品-国产精品 | 国内自拍视频在线观看 | 日韩手机视频 | 免费1区2区3区 | 国产91丝袜在线熟 | 99久久精品国产一区二区三区 | 久久中文字幕一区 | 久久久久久国产精品 | 久草免费视 | 欧美午夜精品理论片a级按摩 | 一区二区三区四区视频 | 91久久|