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实现图片浏览功能的示例详解(附带源码)

《Android实现图片浏览功能的示例详解(附带源码)》在许多应用中,都需要展示图片并支持用户进行浏览,本文主要为大家介绍了如何通过Android实现图片浏览功能,感兴趣的小伙伴可以跟随小编一起学习一... 目录一、项目背景详细介绍二、项目需求详细介绍三、相关技术详细介绍四、实现思路详细介绍五、完整实现代码

在Android中使用WebView在线查看PDF文件的方法示例

《在Android中使用WebView在线查看PDF文件的方法示例》在Android应用开发中,有时我们需要在客户端展示PDF文件,以便用户可以阅读或交互,:本文主要介绍在Android中使用We... 目录简介:1. WebView组件介绍2. 在androidManifest.XML中添加Interne

MySQL8 密码强度评估与配置详解

《MySQL8密码强度评估与配置详解》MySQL8默认启用密码强度插件,实施MEDIUM策略(长度8、含数字/字母/特殊字符),支持动态调整与配置文件设置,推荐使用STRONG策略并定期更新密码以提... 目录一、mysql 8 密码强度评估机制1.核心插件:validate_password2.密码策略级

Android协程高级用法大全

《Android协程高级用法大全》这篇文章给大家介绍Android协程高级用法大全,本文结合实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友跟随小编一起学习吧... 目录1️⃣ 协程作用域(CoroutineScope)与生命周期绑定Activity/Fragment 中手

MySQL设置密码复杂度策略的完整步骤(附代码示例)

《MySQL设置密码复杂度策略的完整步骤(附代码示例)》MySQL密码策略还可能包括密码复杂度的检查,如是否要求密码包含大写字母、小写字母、数字和特殊字符等,:本文主要介绍MySQL设置密码复杂度... 目录前言1. 使用 validate_password 插件1.1 启用 validate_passwo

Android 缓存日志Logcat导出与分析最佳实践

《Android缓存日志Logcat导出与分析最佳实践》本文全面介绍AndroidLogcat缓存日志的导出与分析方法,涵盖按进程、缓冲区类型及日志级别过滤,自动化工具使用,常见问题解决方案和最佳实... 目录android 缓存日志(Logcat)导出与分析全攻略为什么要导出缓存日志?按需过滤导出1. 按

Android Paging 分页加载库使用实践

《AndroidPaging分页加载库使用实践》AndroidPaging库是Jetpack组件的一部分,它提供了一套完整的解决方案来处理大型数据集的分页加载,本文将深入探讨Paging库... 目录前言一、Paging 库概述二、Paging 3 核心组件1. PagingSource2. Pager3.

Python对接支付宝支付之使用AliPay实现的详细操作指南

《Python对接支付宝支付之使用AliPay实现的详细操作指南》支付宝没有提供PythonSDK,但是强大的github就有提供python-alipay-sdk,封装里很多复杂操作,使用这个我们就... 目录一、引言二、准备工作2.1 支付宝开放平台入驻与应用创建2.2 密钥生成与配置2.3 安装ali

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