最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android自定義驗(yàn)證碼輸入框的方法實(shí)例

 更新時(shí)間:2022年02月10日 09:20:44   作者:芝麻粒兒  
這篇文章主要給大家介紹了關(guān)于Android自定義驗(yàn)證碼輸入框的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),對(duì)各位Android開(kāi)發(fā)者們具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

??實(shí)踐過(guò)程

前面我們學(xué)完了EditText和TextView兩個(gè)組件,但是,光學(xué)不練沒(méi)意思。

所以今天我們趁熱打鐵,利用兩個(gè)組件實(shí)現(xiàn)個(gè)自定義驗(yàn)證碼輸入框。

思路前瞻:

  • 隱形EditText接收輸入,顯性TextView展示內(nèi)容
  • 時(shí)刻監(jiān)聽(tīng)EditText回調(diào)更改內(nèi)容
  • 自定義RelativeLayout

布局代碼:

<?xml version="1.0" encoding="utf-8"?><!--自定義驗(yàn)證碼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 自定義驗(yàn)證碼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) {
                //如果有字符輸入時(shí)才進(jìn)行操作
                if (!s.toString().equals("")) {
                    //我們限制了4個(gè)驗(yàn)證碼
                    if (stringBuffer.length() > 3) {
                        editText.setText("");
                        return;
                    } else {
                        stringBuffer.append(s);
                        //因?yàn)閑ditText是輔助的,根本字符串是stringBuffer,所以將EditText置空
                        editText.setText("");
                        //現(xiàn)在很多App都是輸入完畢后自動(dòng)進(jìn)入下一步邏輯,所以咱們一般都是在這監(jiān)聽(tīng),完成后進(jìn)行回調(diào)業(yè)務(wù)即可
                        if (stringBuffer.length() == 4) {
                            //驗(yàn)證碼輸入完畢了,自動(dòng)進(jìn)行驗(yàn)證邏輯
                        }
                    }

                    for (int i = 0; i < stringBuffer.length(); i++) {
                        textViewList.get(i).setText(stringBuffer.charAt(i) + "");
                    }
                }
            }
        });

        //設(shè)置刪除按鍵的監(jiān)聽(tīng)
        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 自定義驗(yàn)證碼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) {
                //如果有字符輸入時(shí)才進(jìn)行操作
                if (s.toString() != "") {
                    //我們限制了4個(gè)驗(yàn)證碼
                    if (stringBuffer.length > 3) {
                        editText!!.setText("")
                        return
                    } else {
                        stringBuffer.append(s)
                        //因?yàn)閑ditText是輔助的,根本字符串是stringBuffer,所以將EditText置空
                        editText!!.setText("")
                        //現(xiàn)在很多App都是輸入完畢后自動(dòng)進(jìn)入下一步邏輯,所以咱們一般都是在這監(jiān)聽(tīng),完成后進(jìn)行回調(diào)業(yè)務(wù)即可
                        if (stringBuffer.length == 4) {
                            //驗(yàn)證碼輸入完畢了,自動(dòng)進(jìn)行驗(yàn)證邏輯
                        }
                    }
                    for (i in 0 until stringBuffer.length) {
                        textViewList[i].text = stringBuffer[i].toString() + ""
                    }
                }
            }
        })

        //設(shè)置刪除按鍵的監(jiān)聽(tīng)
        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(頁(yè)面)布局中使用即可

<?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的知識(shí)。沒(méi)錯(cuò),小空幾種驗(yàn)證碼的實(shí)現(xiàn)方案特意選的這個(gè),這樣我們就引出了下一篇文章布局容器的知識(shí):RelativeLayout(相對(duì)布局容器)和LinearLayout(線性布局容器)

當(dāng)然了,設(shè)計(jì)千奇百怪。上面只是普通的實(shí)現(xiàn),還做過(guò)下面?zhèn)z功能需求

自定義驗(yàn)證碼輸入,自定義輸入鍵盤的-不推薦

直接包含了輸入按鍵寫到整個(gè)頁(yè)面UI里,禁止軟(?。╂I盤彈出的-較推薦

但不管什么需求,用的是EditText或TextView

都逃脫不了EditText的【addTextChangedListener】、【InputFilter】、【android:inputType】幾個(gè)知識(shí)點(diǎn)以及TextView的基本屬性應(yīng)用。

更多需求的創(chuàng)意解決方案就靠大家多想想了,有時(shí)候基本的技術(shù)解決困難的需求反而更輕松快捷。

到此這篇關(guān)于Android自定義驗(yàn)證碼輸入框的文章就介紹到這了,更多相關(guān)Android自定義驗(yàn)證碼輸入框內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • android 9PNG圖片制作過(guò)程(圖文介紹)

    android 9PNG圖片制作過(guò)程(圖文介紹)

    我們想要是有些圖片可以拉伸而不失真多好啊,這時(shí)候我們就要想起android為我們提供的9.png格式的圖片了,9.png格式的圖片是安卓平臺(tái)上新創(chuàng)的一種被拉伸卻不失真的玩意
    2013-01-01
  • Android webview使用方法總結(jié)

    Android webview使用方法總結(jié)

    這篇文章主要介紹了Android webview使用方法總結(jié)的相關(guān)資料,需要的朋友可以參考下
    2017-07-07
  • Android clipChildren屬性實(shí)例詳解

    Android clipChildren屬性實(shí)例詳解

    本文主要介紹Android clipChildren的屬性,這里對(duì)clipChildren屬性做了一個(gè)小例子,展示了效果圖和實(shí)例代碼,方便大家觀看理解
    2016-07-07
  • Android開(kāi)發(fā)之保存圖片到相冊(cè)的三種方法詳解

    Android開(kāi)發(fā)之保存圖片到相冊(cè)的三種方法詳解

    這篇文章主要介紹了Android開(kāi)發(fā)實(shí)現(xiàn)的保存圖片到相冊(cè)功能的三種方法,文中的示例代碼講解詳細(xì),有一定的參考價(jià)值,感興趣的可以了解一下
    2022-04-04
  • Android  兩個(gè)Fragment之間的跳轉(zhuǎn)和數(shù)據(jù)的傳遞實(shí)例詳解

    Android 兩個(gè)Fragment之間的跳轉(zhuǎn)和數(shù)據(jù)的傳遞實(shí)例詳解

    這篇文章主要介紹了Android 兩個(gè)Fragment之間的跳轉(zhuǎn)和數(shù)據(jù)的傳遞實(shí)例詳解的相關(guān)資料,這里說(shuō)明實(shí)現(xiàn)的思路及實(shí)現(xiàn)方法,需要的朋友可以參考下
    2017-07-07
  • Android照片墻應(yīng)用實(shí)現(xiàn) 再多的圖片也不怕崩潰

    Android照片墻應(yīng)用實(shí)現(xiàn) 再多的圖片也不怕崩潰

    這篇文章主要為大家詳細(xì)介紹了Android照片墻應(yīng)用實(shí)現(xiàn),再多的圖片也不怕崩潰,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-10-10
  • RecyclerView底部分割線去除的方法

    RecyclerView底部分割線去除的方法

    如何完美的去除RecyclerView底部分割線?這篇文章主要為大家詳細(xì)介紹了RecyclerView底部分割線去除的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-04-04
  • Android實(shí)現(xiàn)EditText輸入金額

    Android實(shí)現(xiàn)EditText輸入金額

    EditText是Android中一個(gè)非常實(shí)用的控件,有很多InputType,可以來(lái)達(dá)到不同的輸入效果,下面通過(guò)實(shí)例代碼給大家解析android實(shí)現(xiàn)edittext輸入金額,需要的朋友參考下吧
    2016-12-12
  • Android實(shí)現(xiàn)自定義的彈幕效果

    Android實(shí)現(xiàn)自定義的彈幕效果

    現(xiàn)在的視頻網(wǎng)站基本都帶有彈幕效果,滿屏幕的文字從右到左飄來(lái)飄去。看起來(lái)還蠻炫的,這篇文章就是來(lái)實(shí)現(xiàn)這個(gè)效果,大部分的都是從右向左移動(dòng)漂移,本文的效果中也支持從左向右的漂移移動(dòng)效果,同時(shí)也支持屏幕彈幕最多顯示個(gè)數(shù)的設(shè)置。有需要的可以參考借鑒。
    2016-08-08
  • android實(shí)現(xiàn)指紋識(shí)別功能

    android實(shí)現(xiàn)指紋識(shí)別功能

    這篇文章主要介紹了android指紋識(shí)別功能,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2019-09-09

最新評(píng)論

东源县| 康马县| 北票市| 班戈县| 嘉禾县| 莆田市| 仪陇县| 洮南市| 增城市| 黄山市| 务川| 宜章县| 咸丰县| 潍坊市| 呈贡县| 台安县| 泰宁县| 庄河市| 达拉特旗| 新疆| 班玛县| 农安县| 濮阳县| 通许县| 来宾市| 高雄县| 澄城县| 云林县| 弥勒县| 雅江县| 天等县| 临漳县| 合水县| 两当县| 资中县| 周口市| 宜良县| 晋城| 外汇| 蒲江县| 建瓯市|