iis服务器助手广告广告
返回顶部
首页 > 资讯 > 移动开发 >Android自定义验证码输入框的方法实例
  • 226
分享到

Android自定义验证码输入框的方法实例

2024-04-02 19:04:59 226人浏览 独家记忆
摘要

目录?实践过程?总结?实践过程 前面我们学完了EditText和TextView两个组件,但是,光学不练没意思。 所以今天我们趁热打铁,利用两个组件实现个自定义验证码输入框。 思路

?实践过程

前面我们学完了EditText和TextView两个组件,但是,光学不练没意思。

所以今天我们趁热打铁,利用两个组件实现个自定义验证码输入框。

思路前瞻:

  • 隐形EditText接收输入,显性TextView展示内容
  • 时刻监听EditText回调更改内容
  • 自定义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代码


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);
        //添加布局内容
        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("");
                        //现在很多App都是输入完毕后自动进入下一步逻辑,所以咱们一般都是在这监听,完成后进行回调业务即可
                        if (stringBuffer.length() == 4) {
                            //验证码输入完毕了,自动进行验证逻辑
                        }
                    }

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

        //设置删除按键的监听
        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显示内容置空
                        textViewList.get(stringBuffer.length()).setText("");
                    }
                    return true;
                }
                return false;
            }
        });
    }

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 {
        //添加布局内容
        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("")
                        //现在很多App都是输入完毕后自动进入下一步逻辑,所以咱们一般都是在这监听,完成后进行回调业务即可
                        if (stringBuffer.length == 4) {
                            //验证码输入完毕了,自动进行验证逻辑
                        }
                    }
                    for (i in 0 until stringBuffer.length) {
                        textViewList[i].text = stringBuffer[i].toString() + ""
                    }
                }
            }
        })

        //设置删除按键的监听
        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显示内容置空
                    textViewList[stringBuffer.length].text = ""
                }
                return@OnKeyListener true
            }
            false
        })
    }
}

直接在目标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>

 

?总结

刚学Android的朋友可能又疑惑了,里面涉及了RelativeLayout和自定义View的知识。没错,小空几种验证码的实现方案特意选的这个,这样我们就引出了下一篇文章布局容器的知识:RelativeLayout(相对布局容器)和LinearLayout(线性布局容器)

当然了,设计千奇百怪。上面只是普通的实现,还做过下面俩功能需求

自定义验证码输入,自定义输入键盘的-不推荐

直接包含了输入按键写到整个页面UI里,禁止软(小)键盘弹出的-较推荐

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

都逃脱不了EditText的【addTextChangedListener】、【InputFilter】、【android:inputType】几个知识点以及TextView的基本属性应用。

更多需求的创意解决方案就靠大家多想想了,有时候基本的技术解决困难的需求反而更轻松快捷。

到此这篇关于Android自定义验证码输入框的文章就介绍到这了,更多相关Android自定义验证码输入框内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: Android自定义验证码输入框的方法实例

本文链接: https://www.lsjlt.com/news/138632.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • Android自定义验证码输入框的方法实例
    目录实践过程总结实践过程 前面我们学完了EditText和TextView两个组件,但是,光学不练没意思。 所以今天我们趁热打铁,利用两个组件实现个自定义验证码输入框。 思路前瞻:...
    99+
    2024-04-02
  • Qt实现自定义验证码输入框控件的方法
    目录前言UI布局:页面样式初始化:功能实现:前言 本文实现了自定义的验证码输入框控件。控件包括图标、输入框、获取验证码按钮、验证码获取倒计时标签。支持鼠标点击获取验证码按钮后开始显示...
    99+
    2024-04-02
  • uniapp自定义输入框,实现验证码输入框、密码输入框、兼容微信小程序
    前言 在移动端或者小程序项目中,验证码输入框、密码输入框也是很常见的,今天我们就来实现一个这样的效果。 图片展示 代码实现 我这里是用uniapp实现的可兼容微信小程序。 大家如果需要微信小程序也可以参考此案例,实现思路都是一样的。 {{...
    99+
    2023-08-18
    微信小程序 uni-app 小程序
  • uniapp自定义验证码输入框并隐藏光标
    目录一. 前言二. 实现思路三. 代码实现四. 过程中遇到的问题一. 前言 先看下使用场景效果图: 点击输入框唤起键盘,蓝框就相当于input的光标,验证码输入错误或者不符合格式要求...
    99+
    2023-02-22
    uniapp验证码输入框 uniapp验证码
  • Android实现短信验证码输入框
    本文实例为大家分享了Android实现短信验证码输入框的具体代码,供大家参考,具体内容如下 其实用官方自定的那个inputEditText默认带下划线的,然后自己再实行焦点和输入框...
    99+
    2024-04-02
  • Android怎么实现自定义密码输入框
    本篇内容主要讲解“Android怎么实现自定义密码输入框”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Android怎么实现自定义密码输入框”吧!一、实现效果及方案预期效果图:如上图所示,要实现...
    99+
    2023-06-25
  • Android自定义view实现TextView方形输入框
    本文实例为大家分享了Android自定义view实现TextView方形输入框的具体代码,供大家参考,具体内容如下 先奉上最终效果图 实现思路分析: 1、 使用一个LinearLa...
    99+
    2024-04-02
  • uniapp怎么自定义验证码输入框并隐藏光标
    这篇文章主要讲解了“uniapp怎么自定义验证码输入框并隐藏光标”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“uniapp怎么自定义验证码输入框并隐藏光标”吧!一. 前言点击输入框唤起键盘,...
    99+
    2023-07-05
  • Android Compose自定义TextField如何实现自定义的输入框
    这篇文章主要介绍Android Compose自定义TextField如何实现自定义的输入框,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!简单自定义BasicTextField示例代码 var&n...
    99+
    2023-06-29
  • Flutter验证码输入框的2种方法实现
    目录重点是什么?从头开始制作 OTP 字段使用第三个包结论本文向您展示了在 Flutter 中实现完美的验证码输入框几种不同方法。 重点是什么? 真实世界的 完美的验证码输入框或 P...
    99+
    2024-04-02
  • Android自定义滑动验证条的示例代码
    本文介绍了Android自定义滑动验证条的示例代码,分享给大家,具体如下:*注:不知道为什么,h6的标签在这里没用了,所以我也只能用Markdown的语法来写了项目地址:https://github.com/994866755/handso...
    99+
    2023-05-30
    android 滑动验证条 roi
  • Angular如何使用输入框实现自定义验证功能
    这篇文章将为大家详细讲解有关Angular如何使用输入框实现自定义验证功能,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。此插件使用angular.js、JQuery实现。...
    99+
    2024-04-02
  • Android自定义密码输入框的简单实现过程
    目录一、实现效果及方案二、实现总结一、实现效果及方案 预期效果图: 如上图所示,要实现一个这种密码输入框的样式,原生并未提供类似的效果,所以需要自定义控件的方式实现。 预期的基...
    99+
    2024-04-02
  • Android自定义view实现输入框效果
    本文实例为大家分享了Android自定义view实现输入框的具体代码,供大家参考,具体内容如下 自定义输入框的View package com.fenghongzhang.day...
    99+
    2024-04-02
  • Android自定义九宫格输入框
    本文实例为大家分享了Android自定义九宫格输入框的具体代码,供大家参考,具体内容如下 效果 实现 绘制宫格分割线 这里我们用一个RectF类型的数组来装载数据。在onSizeC...
    99+
    2024-04-02
  • Flutter验证码输入框的实现方法有哪些
    Flutter验证码输入框的实现方法有哪些,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。重点是什么?真实世界的 完美的验证码输入框或 PIN 输入 UI 通常满...
    99+
    2023-06-22
  • AndroidCompose自定义TextField实现自定义的输入框
    目录概览简单自定义BasicTextField示例实现自定义样式的BasicTextField使用BasicTextField自定义百度输入框概览 众所周知Compose中默认的Te...
    99+
    2024-04-02
  • js实现验证码输入框示例详解
    目录前言思路遇到的问题HTMLCSSJS前言 验证码输入框应该是很常见的需求吧,不知道各位小伙伴在在遇到的时候是选择找一找插件还是自己动手撸一个呢?我花了点时间撸了一个出来,实际体验...
    99+
    2024-04-02
  • Android自定义View实现随机数验证码
    目录前言效果自定义 View 分类步骤1.自定义属性2.添加构造方法3.在构造里获取自定义样式4.重写 onDraw 计算坐标绘制5.重写 onMeasure 测量宽高6.设置点击事...
    99+
    2024-04-02
  • android自定义对话框实例代码
    1.实现效果    2.定义dialog.xml (res/layout/dialog.xml) <?xml version="1.0" encoding="utf...
    99+
    2024-04-02
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作