前言
驗證碼輸入框是很多APP必不可少的組件,之前在重構注冊登錄頁面的時候,重新設計了UI,所以不能再簡單的用EditText來做了,所以這篇文章將分享一下如何實現一個常見的驗證碼輸入框。下面話不多說了,來一起看看詳細的介紹吧。
正文
先摟一眼效果吧

不要把注意力都放在頭頂的那一抹綠上,重點在輸入框,可能大多數APP里都是采用6個方框的UI效果,我這里是按照我們設計的要求,用6根橫線來劃出6個數字的位置。一開始我想的是直接用6個TextView,然后傳遞焦點的做法,但是發現實現起來有一定的難度。又在網上查了一下,發現比較靠譜的辦法是用6個TextView加一個EditText來實現,也按照這個方法去實現了,但是后來在測試的時候就發現了問題:網上給出的實現方式需要監聽軟鍵盤的刪除按鈕
editText.setOnKeyListener(new OnKeyListener() { @Override public boolean onKey(View v, int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_DEL && event.getAction() == KeyEvent.ACTION_DOWN) { //TODO: return true; } return false; } });這是一個大家熟知的寫法,但是這個監聽的方法其實并不靠譜(在安卓原生鍵盤上就監聽不到),因為這個監聽是否觸發,并沒有強制的要求,全看輸入法開發者的心情,這是官方文檔中的描述:
Key presses in software keyboards will generally NOT trigger this method, although some may elect to do so in some situations.
只能輸入,不能刪除,這可不行啊,用戶肯定會罵娘的,我可不想被拿去去祭天什么的...
于是乎只能想辦法在原有的基礎上做一些修改,來規避這個問題,最后采用的方案是:采用一個TextView的數組來維護6個TextView,然后藏一個透明的EditTextView在后面用于接收用戶輸入的內容,再把輸入的內容展示到6個TextView上就行了,UI什么的可以自己隨意設計。在實現的過程中,遇到的一個關鍵問題就是:當輸入的內容超過6位以后我該如何處理?一開始的方案是通過判斷當前輸入的位數然后再做相應的處理,網上的方案也是這么實現的,我后來一想,根本用不著這么麻煩,只需要一行屬性就能解決這個問題:
android:maxLength="6"
只需要在EditText的屬性里限制它的最大長度,就不用再去代碼里做處理了,直接把EditTextView里的內容完全照搬到TextView上就可以了。
最終的完整代碼如下:
public class VerifyCodeView extends RelativeLayout { private EditText editText; private TextView[] textViews; private static int MAX = 6; private String inputContent; public VerifyCodeView(Context context) { this(context, null); } public VerifyCodeView(Context context, AttributeSet attrs) { this(context, attrs, 0); } public VerifyCodeView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); View.inflate(context, R.layout.view_verify_code, this); textViews = new TextView[MAX]; textViews[0] = (TextView) findViewById(R.id.item_code_iv0); textViews[1] = (TextView) findViewById(R.id.item_code_iv1); textViews[2] = (TextView) findViewById(R.id.item_code_iv2); textViews[3] = (TextView) findViewById(R.id.item_code_iv3); textViews[4] = (TextView) findViewById(R.id.item_code_iv4); textViews[5] = (TextView) findViewById(R.id.item_code_iv5); editText = (EditText) findViewById(R.id.item_edittext); editText.setCursorVisible(false);//隱藏光標 setEditTextListener(); } private void setEditTextListener() { editText.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) { } @Override public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) { } @Override public void afterTextChanged(Editable editable) { inputContent = editText.getText().toString(); if (inputCompleteListener != null) { if (inputContent.length() >= MAX) { inputCompleteListener.inputComplete(); } else { inputCompleteListener.invalidContent(); } } for (int i = 0; i < MAX; i++) { if (i < inputContent.length()) { textViews[i].setText(String.valueOf(inputContent.charAt(i))); } else { textViews[i].setText(""); } } } }); } private InputCompleteListener inputCompleteListener; public void setInputCompleteListener(InputCompleteListener inputCompleteListener) { this.inputCompleteListener = inputCompleteListener; } public interface InputCompleteListener { void inputComplete(); void invalidContent(); } public String getEditContent() { return inputContent; }}如果需要完整的demo,可以訪問我的github:https://github.com/jb274585381/VerifyCodeViewDemo,當然大家也可以直接本地下載。
總結
有時候我們實現一個需求,不光要考慮最終的效果,還要考慮時間成本,能用最簡單的方法實現當然是最好的,以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對武林網的支持。
新聞熱點
疑難解答