Android仿微信/支付宝的方块密码输入框

2024-06-04 12:58

本文主要是介绍Android仿微信/支付宝的方块密码输入框,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

在用到支付类或者验证类app时,都有一个简密的输入框。百度了下有个不错的帖子   点击打开链接 

不过自己也写了个简单的类似的。不废话了。没图说个席八。


懒得运行,直接截layout.xml的效果图先。

布局文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="wrap_content"android:background="#ffffff"android:paddingBottom="20dp"android:paddingTop="30dp" ><TextViewandroid:id="@+id/txtTitle"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerHorizontal="true"android:text="请输入验证码"android:textStyle="bold"android:textSize="22sp" /><LinearLayoutandroid:id="@+id/layout"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_below="@+id/txtTitle"android:layout_centerHorizontal="true"android:layout_marginTop="10dp"android:orientation="horizontal" ><TextViewandroid:id="@+id/t1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:background="@drawable/i1"android:gravity="center"android:inputType="number"android:lines="1"android:maxLines="1" /><TextViewandroid:id="@+id/t2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:background="@drawable/i1"android:gravity="center"android:inputType="number"android:lines="1"android:maxLines="1" /><TextViewandroid:id="@+id/t3"android:layout_width="wrap_content"android:layout_height="wrap_content"android:background="@drawable/i1"android:gravity="center"android:inputType="number"android:lines="1"android:maxLines="1" /><TextViewandroid:id="@+id/t4"android:layout_width="wrap_content"android:layout_height="wrap_content"android:background="@drawable/i2"android:gravity="center"android:inputType="number"android:lines="1"android:maxLines="1" /></LinearLayout><EditTextandroid:id="@+id/editHide"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignBottom="@+id/layout"android:layout_alignLeft="@+id/layout"android:layout_alignRight="@+id/layout"android:layout_alignTop="@+id/layout"android:layout_weight="1"android:background="#00000000"android:cursorVisible="false"android:ems="10"android:inputType="number"android:maxLength="4"android:textColor="#00000000" /><LinearLayoutandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_below="@+id/layout"android:layout_centerHorizontal="true"android:layout_marginTop="20dp" ><Buttonandroid:id="@+id/verifycode_ok"android:layout_width="wrap_content"android:layout_height="wrap_content"android:background="#fa8d70"android:layout_marginRight="30dp"android:textColor="#ffffff"android:text="确定" /><Buttonandroid:id="@+id/verifycode_cancel"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="30dp"android:background="@color/index_time_but"android:textColor="#ffffff"android:text="取消" /></LinearLayout></RelativeLayout>

代码:
import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.TextView;public class MainActivity extends Activity {TextView t1, t2, t3, t4, et;String key = "";@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.verifycode);t1 = (TextView) findViewById(R.id.t1);t2 = (TextView) findViewById(R.id.t2);t3 = (TextView) findViewById(R.id.t3);t4 = (TextView) findViewById(R.id.t4);et = (EditText) findViewById(R.id.editText1);et.addTextChangedListener(tw);}void setKey() {char[] arr = key.toCharArray();t1.setText("");t2.setText("");t3.setText("");t4.setText("");for (int i = 0; i < arr.length; i++) {if (i == 0) {t1.setText(String.valueOf(arr[0]));} else if (i == 1) {t2.setText(String.valueOf(arr[1]));} else if (i == 2) {t3.setText(String.valueOf(arr[2]));} else if (i == 3) {t4.setText(String.valueOf(arr[3]));}}}TextWatcher tw = new TextWatcher() {@Overridepublic void onTextChanged(CharSequence s, int start, int before,int count) {}@Overridepublic void beforeTextChanged(CharSequence s, int start, int count,int after) {}@Overridepublic void afterTextChanged(Editable s) {key = s.toString();setKey();}};
}

好哒。就这样了。运行就有效果了。没有什么自定义,没有什么第三方。


当然。如果你要弹窗形式的话,也一样,加下面的就能弹窗了

	final AlertDialog dialog = new AlertDialog.Builder(mContext).create();dialog.show();dialog.getWindow().setContentView(R.layout.verifycode);t1 = (TextView) dialog.findViewById(R.id.t1);t2 = (TextView) dialog.findViewById(R.id.t2);t3 = (TextView) dialog.findViewById(R.id.t3);t4 = (TextView) dialog.findViewById(R.id.t4);

后期有时间再补上项目DOME。下班。走人


这篇关于Android仿微信/支付宝的方块密码输入框的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1030150

相关文章

Android kotlin中 Channel 和 Flow 的区别和选择使用场景分析

《Androidkotlin中Channel和Flow的区别和选择使用场景分析》Kotlin协程中,Flow是冷数据流,按需触发,适合响应式数据处理;Channel是热数据流,持续发送,支持... 目录一、基本概念界定FlowChannel二、核心特性对比数据生产触发条件生产与消费的关系背压处理机制生命周期

Android ClassLoader加载机制详解

《AndroidClassLoader加载机制详解》Android的ClassLoader负责加载.dex文件,基于双亲委派模型,支持热修复和插件化,需注意类冲突、内存泄漏和兼容性问题,本文给大家介... 目录一、ClassLoader概述1.1 类加载的基本概念1.2 android与Java Class

Spring Security中用户名和密码的验证完整流程

《SpringSecurity中用户名和密码的验证完整流程》本文给大家介绍SpringSecurity中用户名和密码的验证完整流程,本文结合实例代码给大家介绍的非常详细,对大家的学习或工作具有一定... 首先创建了一个UsernamePasswordAuthenticationTChina编程oken对象,这是S

Android DataBinding 与 MVVM使用详解

《AndroidDataBinding与MVVM使用详解》本文介绍AndroidDataBinding库,其通过绑定UI组件与数据源实现自动更新,支持双向绑定和逻辑运算,减少模板代码,结合MV... 目录一、DataBinding 核心概念二、配置与基础使用1. 启用 DataBinding 2. 基础布局

Android ViewBinding使用流程

《AndroidViewBinding使用流程》AndroidViewBinding是Jetpack组件,替代findViewById,提供类型安全、空安全和编译时检查,代码简洁且性能优化,相比Da... 目录一、核心概念二、ViewBinding优点三、使用流程1. 启用 ViewBinding (模块级

PostgreSQL数据库密码被遗忘时的操作步骤

《PostgreSQL数据库密码被遗忘时的操作步骤》密码遗忘是常见的用户问题,因此提供一种安全的遗忘密码找回机制是十分必要的,:本文主要介绍PostgreSQL数据库密码被遗忘时的操作步骤的相关资... 目录前言一、背景知识二、Windows环境下的解决步骤1. 找到PostgreSQL安装目录2. 修改p

Android学习总结之Java和kotlin区别超详细分析

《Android学习总结之Java和kotlin区别超详细分析》Java和Kotlin都是用于Android开发的编程语言,它们各自具有独特的特点和优势,:本文主要介绍Android学习总结之Ja... 目录一、空安全机制真题 1:Kotlin 如何解决 Java 的 NullPointerExceptio

Druid连接池实现自定义数据库密码加解密功能

《Druid连接池实现自定义数据库密码加解密功能》在现代应用开发中,数据安全是至关重要的,本文将介绍如何在​​Druid​​连接池中实现自定义的数据库密码加解密功能,有需要的小伙伴可以参考一下... 目录1. 环境准备2. 密码加密算法的选择3. 自定义 ​​DruidDataSource​​ 的密码解密3

SpringBoot如何对密码等敏感信息进行脱敏处理

《SpringBoot如何对密码等敏感信息进行脱敏处理》这篇文章主要为大家详细介绍了SpringBoot对密码等敏感信息进行脱敏处理的几个常用方法,文中的示例代码讲解详细,感兴趣的小伙伴可以了解下... 目录​1. 配置文件敏感信息脱敏​​2. 日志脱敏​​3. API响应脱敏​​4. 其他注意事项​​总结

JavaScript实战:智能密码生成器开发指南

本文通过JavaScript实战开发智能密码生成器,详解如何运用crypto.getRandomValues实现加密级随机密码生成,包含多字符组合、安全强度可视化、易混淆字符排除等企业级功能。学习密码强度检测算法与信息熵计算原理,获取可直接嵌入项目的完整代码,提升Web应用的安全开发能力 目录