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学习总结之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应用的安全开发能力 目录

使用Java实现Navicat密码的加密与解密的代码解析

《使用Java实现Navicat密码的加密与解密的代码解析》:本文主要介绍使用Java实现Navicat密码的加密与解密,通过本文,我们了解了如何利用Java语言实现对Navicat保存的数据库密... 目录一、背景介绍二、环境准备三、代码解析四、核心代码展示五、总结在日常开发过程中,我们有时需要处理各种软

Android NDK版本迭代与FFmpeg交叉编译完全指南

《AndroidNDK版本迭代与FFmpeg交叉编译完全指南》在Android开发中,使用NDK进行原生代码开发是一项常见需求,特别是当我们需要集成FFmpeg这样的多媒体处理库时,本文将深入分析A... 目录一、android NDK版本迭代分界线二、FFmpeg交叉编译关键注意事项三、完整编译脚本示例四

Android与iOS设备MAC地址生成原理及Java实现详解

《Android与iOS设备MAC地址生成原理及Java实现详解》在无线网络通信中,MAC(MediaAccessControl)地址是设备的唯一网络标识符,本文主要介绍了Android与iOS设备M... 目录引言1. MAC地址基础1.1 MAC地址的组成1.2 MAC地址的分类2. android与I

Android 实现一个隐私弹窗功能

《Android实现一个隐私弹窗功能》:本文主要介绍Android实现一个隐私弹窗功能,本文通过实例代码给大家介绍的非常详细,感兴趣的朋友一起看看吧... 效果图如下:1. 设置同意、退出、点击用户协议、点击隐私协议的函数参数2. 《用户协议》、《隐私政策》设置成可点击的,且颜色要区分出来res/l

Android实现一键录屏功能(附源码)

《Android实现一键录屏功能(附源码)》在Android5.0及以上版本,系统提供了MediaProjectionAPI,允许应用在用户授权下录制屏幕内容并输出到视频文件,所以本文将基于此实现一个... 目录一、项目介绍二、相关技术与原理三、系统权限与用户授权四、项目架构与流程五、环境配置与依赖六、完整

Android 12解决push framework.jar无法开机的方法小结

《Android12解决pushframework.jar无法开机的方法小结》:本文主要介绍在Android12中解决pushframework.jar无法开机的方法,包括编译指令、框架层和s... 目录1. android 编译指令1.1 framework层的编译指令1.2 替换framework.ja