Android自定義驗證碼輸入框的方法實例
??實踐過程
前面我們學(xué)完了EditText和TextView兩個組件,但是,光學(xué)不練沒意思。
所以今天我們趁熱打鐵,利用兩個組件實現(xiàn)個自定義驗證碼輸入框。
思路前瞻:
- 隱形EditText接收輸入,顯性TextView展示內(nèi)容
- 時刻監(jiān)聽EditText回調(diào)更改內(nèi)容
- 自定義RelativeLayout
布局代碼:
<?xml version="1.0" encoding="utf-8"?><!--自定義驗證碼View--> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#F84F64" android:paddingTop="100dp"> <!--線性布局-orientation="horizontal"水平方向--> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal"> <View android:layout_width="0dp" android:layout_height="1dp" android:layout_weight="1" /> <TextView android:id="@+id/txtCode1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/edittext_kuang" android:gravity="center" android:textColor="#ffffff" android:textSize="26sp" /> <View android:layout_width="0dp" android:layout_height="1dp" android:layout_weight="1" /> <TextView android:id="@+id/txtCode2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/edittext_kuang" android:gravity="center" android:textColor="#ffffff" android:textSize="26sp" /> <View android:layout_width="0dp" android:layout_height="1dp" android:layout_weight="1" /> <TextView android:id="@+id/txtCode3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/edittext_kuang" android:gravity="center" android:textColor="#ffffff" android:textSize="26sp" /> <View android:layout_width="0dp" android:layout_height="1dp" android:layout_weight="1" /> <TextView android:id="@+id/txtCode4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/edittext_kuang" android:gravity="center" android:textColor="#ffffff" android:textSize="26sp" /> <View android:layout_width="0dp" android:layout_height="1dp" android:layout_weight="1" /> </LinearLayout> <EditText android:id="@+id/editCode" android:layout_width="match_parent" android:layout_height="50dp" android:background="@android:color/transparent" android:inputType="number" /> </RelativeLayout>
自定義View代碼
/** * Created by akitaka on 2022-01-26. * * @author akitaka * @filename VerificationCodeViewJava * @describe 自定義驗證碼view-Java代碼 * @email 960576866@qq.com */ public class VerificationCodeViewJava extends RelativeLayout { private EditText editText; private List<TextView> textViewList = new ArrayList<>(); private StringBuffer stringBuffer = new StringBuffer(); public VerificationCodeViewJava(Context context, AttributeSet attrs) { this(context, attrs, 0); } public VerificationCodeViewJava(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); //添加布局內(nèi)容 View.inflate(context, R.layout.view_verification_code, this); editText = findViewById(R.id.editCode); textViewList.add(findViewById(R.id.txtCode1)); textViewList.add(findViewById(R.id.txtCode2)); textViewList.add(findViewById(R.id.txtCode3)); textViewList.add(findViewById(R.id.txtCode4)); editText.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { } @Override public void onTextChanged(CharSequence s, int start, int before, int count) { } @Override public void afterTextChanged(Editable s) { //如果有字符輸入時才進行操作 if (!s.toString().equals("")) { //我們限制了4個驗證碼 if (stringBuffer.length() > 3) { editText.setText(""); return; } else { stringBuffer.append(s); //因為editText是輔助的,根本字符串是stringBuffer,所以將EditText置空 editText.setText(""); //現(xiàn)在很多App都是輸入完畢后自動進入下一步邏輯,所以咱們一般都是在這監(jiān)聽,完成后進行回調(diào)業(yè)務(wù)即可 if (stringBuffer.length() == 4) { //驗證碼輸入完畢了,自動進行驗證邏輯 } } for (int i = 0; i < stringBuffer.length(); i++) { textViewList.get(i).setText(stringBuffer.charAt(i) + ""); } } } }); //設(shè)置刪除按鍵的監(jiān)聽 editText.setOnKeyListener(new OnKeyListener() { @Override public boolean onKey(View v, int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_DEL && event.getAction() == KeyEvent.ACTION_DOWN) { if (stringBuffer.length() > 0) { //刪除字符 stringBuffer.delete(stringBuffer.length() - 1, stringBuffer.length()); //將TextView顯示內(nèi)容置空 textViewList.get(stringBuffer.length()).setText(""); } return true; } return false; } }); }
/** * Created by akitaka on 2022-01-26. * @author akitaka * @filename VerificationCodeViewKotlin * @describe 自定義驗證碼view-Kotlin代碼 * @email 960576866@qq.com */ class VerificationCodeViewKotlin : RelativeLayout { private var editText: EditText? = null private val textViewList: MutableList<TextView> = ArrayList() private val stringBuffer = StringBuffer() constructor(context: Context?) : this(context, null) constructor(context: Context?, attrs: AttributeSet?) : this(context, attrs, 0) constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr) init { //添加布局內(nèi)容 View.inflate(context, R.layout.view_verification_code, this) editText = findViewById(R.id.editCode) textViewList.add(findViewById(R.id.txtCode1)) textViewList.add(findViewById(R.id.txtCode2)) textViewList.add(findViewById(R.id.txtCode3)) textViewList.add(findViewById(R.id.txtCode4)) editText!!.addTextChangedListener(object : TextWatcher { override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {} override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {} override fun afterTextChanged(s: Editable) { //如果有字符輸入時才進行操作 if (s.toString() != "") { //我們限制了4個驗證碼 if (stringBuffer.length > 3) { editText!!.setText("") return } else { stringBuffer.append(s) //因為editText是輔助的,根本字符串是stringBuffer,所以將EditText置空 editText!!.setText("") //現(xiàn)在很多App都是輸入完畢后自動進入下一步邏輯,所以咱們一般都是在這監(jiān)聽,完成后進行回調(diào)業(yè)務(wù)即可 if (stringBuffer.length == 4) { //驗證碼輸入完畢了,自動進行驗證邏輯 } } for (i in 0 until stringBuffer.length) { textViewList[i].text = stringBuffer[i].toString() + "" } } } }) //設(shè)置刪除按鍵的監(jiān)聽 editText!!.setOnKeyListener(OnKeyListener { v, keyCode, event -> if (keyCode == KeyEvent.KEYCODE_DEL && event.action == KeyEvent.ACTION_DOWN) { if (stringBuffer.length > 0) { //刪除字符 stringBuffer.delete(stringBuffer.length - 1, stringBuffer.length) //將TextView顯示內(nèi)容置空 textViewList[stringBuffer.length].text = "" } return@OnKeyListener true } false }) } }
直接在目標(biāo)Activity(頁面)布局中使用即可
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <cn.appstudy.customView.VerificationCodeViewJava android:layout_width="match_parent" android:visibility="gone" android:layout_height="match_parent" /> <!-- 或者--> <cn.appstudy.customView.VerificationCodeViewKotlin android:layout_width="match_parent" android:layout_height="match_parent" /> </RelativeLayout>
??總結(jié)
剛學(xué)Android的朋友可能又疑惑了,里面涉及了RelativeLayout和自定義View的知識。沒錯,小空幾種驗證碼的實現(xiàn)方案特意選的這個,這樣我們就引出了下一篇文章布局容器的知識:RelativeLayout(相對布局容器)和LinearLayout(線性布局容器)
當(dāng)然了,設(shè)計千奇百怪。上面只是普通的實現(xiàn),還做過下面?zhèn)z功能需求
自定義驗證碼輸入,自定義輸入鍵盤的-不推薦
直接包含了輸入按鍵寫到整個頁面UI里,禁止軟(?。╂I盤彈出的-較推薦
但不管什么需求,用的是EditText或TextView
都逃脫不了EditText的【addTextChangedListener】、【InputFilter】、【android:inputType】幾個知識點以及TextView的基本屬性應(yīng)用。
更多需求的創(chuàng)意解決方案就靠大家多想想了,有時候基本的技術(shù)解決困難的需求反而更輕松快捷。
到此這篇關(guān)于Android自定義驗證碼輸入框的文章就介紹到這了,更多相關(guān)Android自定義驗證碼輸入框內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Android編程中activity啟動時出現(xiàn)白屏、黑屏問題的解決方法
這篇文章主要介紹了Android編程中activity啟動時出現(xiàn)白屏、黑屏問題的解決方法,涉及Android針對activity啟動設(shè)置的技巧,需要的朋友可以參考下2015-12-12Android?Flutter控件封裝之視頻進度條的實現(xiàn)
這篇文章主要來和大家分享一個很簡單的控制器封裝案例,包含了基本的播放暫停,全屏和退出全屏,文中的示例代碼講解詳細(xì),感興趣的可以了解一下2023-06-06解析ScrollView--仿QQ空間標(biāo)題欄漸變
本篇文章主要介紹了仿QQ空間標(biāo)題欄漸變的實現(xiàn)方法的相關(guān)知識,具有很好的參考價值。下面跟著小編一起來看下吧2017-05-05Android實現(xiàn)實時滑動ViewPager的2種方式
這篇文章主要為大家詳細(xì)介紹了Android實現(xiàn)實時滑動ViewPager的2種方式,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-10-10