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

相关文章

Windows 上如果忘记了 MySQL 密码 重置密码的两种方法

《Windows上如果忘记了MySQL密码重置密码的两种方法》:本文主要介绍Windows上如果忘记了MySQL密码重置密码的两种方法,本文通过两种方法结合实例代码给大家介绍的非常详细,感... 目录方法 1:以跳过权限验证模式启动 mysql 并重置密码方法 2:使用 my.ini 文件的临时配置在 Wi

Android实现在线预览office文档的示例详解

《Android实现在线预览office文档的示例详解》在移动端展示在线Office文档(如Word、Excel、PPT)是一项常见需求,这篇文章为大家重点介绍了两种方案的实现方法,希望对大家有一定的... 目录一、项目概述二、相关技术知识三、实现思路3.1 方案一:WebView + Office Onl

Android实现两台手机屏幕共享和远程控制功能

《Android实现两台手机屏幕共享和远程控制功能》在远程协助、在线教学、技术支持等多种场景下,实时获得另一部移动设备的屏幕画面,并对其进行操作,具有极高的应用价值,本项目旨在实现两台Android手... 目录一、项目概述二、相关知识2.1 MediaProjection API2.2 Socket 网络

Android实现悬浮按钮功能

《Android实现悬浮按钮功能》在很多场景中,我们希望在应用或系统任意界面上都能看到一个小的“悬浮按钮”(FloatingButton),用来快速启动工具、展示未读信息或快捷操作,所以本文给大家介绍... 目录一、项目概述二、相关技术知识三、实现思路四、整合代码4.1 Java 代码(MainActivi

Android Mainline基础简介

《AndroidMainline基础简介》AndroidMainline是通过模块化更新Android核心组件的框架,可能提高安全性,本文给大家介绍AndroidMainline基础简介,感兴趣的朋... 目录关键要点什么是 android Mainline?Android Mainline 的工作原理关键

如何解决idea的Module:‘:app‘platform‘android-32‘not found.问题

《如何解决idea的Module:‘:app‘platform‘android-32‘notfound.问题》:本文主要介绍如何解决idea的Module:‘:app‘platform‘andr... 目录idea的Module:‘:app‘pwww.chinasem.cnlatform‘android-32

Android实现打开本地pdf文件的两种方式

《Android实现打开本地pdf文件的两种方式》在现代应用中,PDF格式因其跨平台、稳定性好、展示内容一致等特点,在Android平台上,如何高效地打开本地PDF文件,不仅关系到用户体验,也直接影响... 目录一、项目概述二、相关知识2.1 PDF文件基本概述2.2 android 文件访问与存储权限2.

Android Studio 配置国内镜像源的实现步骤

《AndroidStudio配置国内镜像源的实现步骤》本文主要介绍了AndroidStudio配置国内镜像源的实现步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,... 目录一、修改 hosts,解决 SDK 下载失败的问题二、修改 gradle 地址,解决 gradle

在Android平台上实现消息推送功能

《在Android平台上实现消息推送功能》随着移动互联网应用的飞速发展,消息推送已成为移动应用中不可或缺的功能,在Android平台上,实现消息推送涉及到服务端的消息发送、客户端的消息接收、通知渠道(... 目录一、项目概述二、相关知识介绍2.1 消息推送的基本原理2.2 Firebase Cloud Me

Python从零打造高安全密码管理器

《Python从零打造高安全密码管理器》在数字化时代,每人平均需要管理近百个账号密码,本文将带大家深入剖析一个基于Python的高安全性密码管理器实现方案,感兴趣的小伙伴可以参考一下... 目录一、前言:为什么我们需要专属密码管理器二、系统架构设计2.1 安全加密体系2.2 密码强度策略三、核心功能实现详解