Android 之微信支付宝支付密码框点睛提要

2023-11-22 19:30

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

本文

http://afra55.github.io/2016/12/22/verification_code_view/

图示

前情提要

思路是用一个横向的 LinearLayout 包 TextView 展示的。

使用手机自带的键盘,监听按键。

Code

Attrs - vcode_attrs.xml

<?xml version="1.0" encoding="utf-8"?>
<resources><declare-styleable name="VerificationCodeView"><attr name="vcodeTextSize" format="dimension" /><attr name="vcodeTextColor" format="color" /><attr name="vcodeBottomIcon" format="reference" /><attr name="vcodeViewCount" format="integer" /><attr name="vcodeItemSpaceSize" format="dimension" /></declare-styleable>
</resources>

resources - vcode_default.xml

<resources><dimen name="default_text_size" >18sp</dimen><dimen name="vcode_text_size" >18sp</dimen><dimen name="vcode_bottom_icon_height">6dp</dimen><dimen name="vcode_bottom_icon_width">48dp</dimen><dimen name="vcode_item_space_size">19dp</dimen><color name="vcode_bottom_icon_bg">#f6a500</color><color name="vcode_bottom_error_icon_bg">#e84849</color><color name="vcode_text_color">#000000</color>
</resources>

drawable - icon_vcode_bottom.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"><corners android:radius="90dp" /><solid android:color="@color/vcode_bottom_icon_bg" /><sizeandroid:height="@dimen/vcode_bottom_icon_height"android:width="@dimen/vcode_bottom_icon_width" />
</shape>

drawable - icon_vcode_err_bottom.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"><corners android:radius="90dp" /><solid android:color="@color/vcode_bottom_error_icon_bg" /><sizeandroid:height="@dimen/vcode_bottom_icon_height"android:width="@dimen/vcode_bottom_icon_width" />
</shape>

Utils - TextViewUtils.java

import android.graphics.drawable.Drawable;
import android.widget.EditText;
import android.widget.TextView;/*** Created by Victor Yang on 2016/12/15 0015.* TextViewUtils.*/public class TextViewUtils {public static void addTextViewLeftIcon(EditText editText, Drawable drawable, int imgSize) {if (editText == null || drawable == null || imgSize <= 0) {return;}drawable.setBounds(0, 0, imgSize, imgSize);editText.setCompoundDrawables(drawable, null, null, null);}public static void addTextViewBottomIcon(TextView textView, Drawable drawable, int imgWidth, int imgHeight) {if (textView == null || drawable == null || imgWidth <= 0) {return;}drawable.setBounds(0, 0, imgWidth, imgHeight);textView.setCompoundDrawables(null, null, null, drawable);}
}

Code - VerificationCodeView.java

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.text.InputType;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.inputmethod.BaseInputConnection;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputConnection;
import android.view.inputmethod.InputMethodManager;
import android.widget.LinearLayout;
import android.widget.TextView;import java.util.ArrayList;
import java.util.List;/*** Created by Victor Yang on 2016/12/19 0019.* 验证码 view*/public class VerificationCodeView extends LinearLayout {public interface CodeCompleteListener{void complete(boolean complete);}// 验证码 text sizeprivate float mVCodeTextSize;// 验证码 text colorprivate int mVCodeTextColor;// 验证码 底部 iconprivate Drawable mVCodeBottomIcon;private Drawable mVCodeBottomErrorIcon;// 验证码 item 之间的空隙private int mVCodeItemCenterSpaceSize;// 存储键盘的输入数字private StringBuilder mCodeStringBuilder;// 存储 验证码 itemprivate List<TextView> mCodeViewList;// 验证码 输入完成 的监听private CodeCompleteListener mCodeCompleteListener;public VerificationCodeView(Context context) {this(context, null);}public VerificationCodeView(Context context, AttributeSet attrs) {this(context, attrs, 0);}public VerificationCodeView(Context context, AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);init(attrs, defStyleAttr);}private void init(AttributeSet attrs, int defStyle) {TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.VerificationCodeView, defStyle, 0);mVCodeTextSize = typedArray.getDimensionPixelSize(R.styleable.VerificationCodeView_vcodeTextSize, getResources().getDimensionPixelSize(R.dimen.vcode_text_size));mVCodeTextColor = typedArray.getColor(R.styleable.VerificationCodeView_vcodeTextColor,getResources().getColor(R.color.vcode_text_color));mVCodeBottomIcon = typedArray.getDrawable(R.styleable.VerificationCodeView_vcodeBottomIcon);if (mVCodeBottomIcon == null) {mVCodeBottomIcon = getResources().getDrawable(R.drawable.icon_vcode_bottom);}mVCodeBottomErrorIcon = typedArray.getDrawable(R.styleable.VerificationCodeView_vcodeBottomIcon);if (mVCodeBottomErrorIcon == null) {mVCodeBottomErrorIcon = getResources().getDrawable(R.drawable.icon_vcode_error_bottom);}int mVCodeViewCount = typedArray.getInt(R.styleable.VerificationCodeView_vcodeViewCount, 4);mVCodeItemCenterSpaceSize = typedArray.getDimensionPixelSize(R.styleable.VerificationCodeView_vcodeItemSpaceSize,getResources().getDimensionPixelSize(R.dimen.vcode_item_space_size));typedArray.recycle();setOrientation(HORIZONTAL);setGravity(Gravity.CENTER);// 获取触摸时焦点setFocusableInTouchMode(true);if (mCodeStringBuilder == null) mCodeStringBuilder = new StringBuilder();mCodeViewList = new ArrayList<>();// 初始化验证码 itemfor (int i = 0; i < mVCodeViewCount; i++) {TextView underLineCodeView = getUnderLineCodeView();mCodeViewList.add(underLineCodeView);addView(underLineCodeView);}}public void setCodeCompleteListener(CodeCompleteListener listener) {mCodeCompleteListener = listener;}private TextView getUnderLineCodeView() {TextView textView = new TextView(getContext());textView.setTextSize(mVCodeTextSize);textView.setTextColor(mVCodeTextColor);textView.setGravity(Gravity.CENTER);int padding = mVCodeItemCenterSpaceSize / 2;textView.setPadding(padding, 0, padding, 0);int bottomIconWidth = (int) getResources().getDimension(R.dimen.vcode_bottom_icon_width);int bottomIconHeight = (int) getResources().getDimension(R.dimen.vcode_bottom_icon_height);TextViewUtils.addTextViewBottomIcon(textView, mVCodeBottomIcon, bottomIconWidth, bottomIconHeight);return textView;}@Overridepublic boolean onTouchEvent(MotionEvent event) {requestFocusFromTouch();requestFocus();// 触摸控件时显示 键盘if (event.getAction() == MotionEvent.ACTION_DOWN) {InputMethodManager imm = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);imm.showSoftInput(this, InputMethodManager.SHOW_FORCED);}return true;}@Overridepublic InputConnection onCreateInputConnection(EditorInfo outAttrs) {// 声明一个数字数字键盘BaseInputConnection fic = new BaseInputConnection(this, false);outAttrs.actionLabel = null;outAttrs.inputType = InputType.TYPE_CLASS_NUMBER;outAttrs.imeOptions = EditorInfo.IME_ACTION_NONE;return fic;}@Overridepublic boolean onKeyDown(int keyCode, KeyEvent event) {if (mCodeStringBuilder == null) mCodeStringBuilder = new StringBuilder();// keyCode = 67 是回退,keyCode = 【7,16】 是数字 【0,9】if (keyCode == 67 && mCodeStringBuilder.length() > 0) {mCodeStringBuilder.deleteCharAt(mCodeStringBuilder.length() - 1);resetCodeShowView();} else if (keyCode >= 7&& keyCode <= 16&& mCodeStringBuilder.length() < mCodeViewList.size()) {mCodeStringBuilder.append(keyCode - 7);resetCodeShowView();}if (mCodeStringBuilder.length() >= mCodeViewList.size() || keyCode == 66) {InputMethodManager imm = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);imm.hideSoftInputFromWindow(getWindowToken(), 0);}return super.onKeyDown(keyCode, event);}private void resetCodeShowView() {if (mCodeStringBuilder == null || mCodeViewList == null || mCodeViewList.size() <= 0) {return;}for (int i = 0 ; i< mCodeViewList.size(); i++) {mCodeViewList.get(i).setText("");if (i < mCodeStringBuilder.length()) {mCodeViewList.get(i).setText(String.valueOf(mCodeStringBuilder.charAt(i)));}}if (mCodeCompleteListener != null) {if (mCodeStringBuilder.length() == mCodeViewList.size()) {mCodeCompleteListener.complete(true);} else {mCodeCompleteListener.complete(false);}}}public void setCodeItemLineDrawable(Drawable drawable) {for (TextView textView : mCodeViewList) {int bottomIconWidth = (int) getResources().getDimension(R.dimen.vcode_bottom_icon_width);int bottomIconHeight = (int) getResources().getDimension(R.dimen.vcode_bottom_icon_height);TextViewUtils.addTextViewBottomIcon(textView, drawable, bottomIconWidth, bottomIconHeight);}}public void resetCodeItemLineDrawable() {setCodeItemLineDrawable(mVCodeBottomIcon);}public void setCodeItemErrorLineDrawable() {for (TextView textView : mCodeViewList) {int bottomIconWidth = (int) getResources().getDimension(R.dimen.vcode_bottom_icon_width);int bottomIconHeight = (int) getResources().getDimension(R.dimen.vcode_bottom_icon_height);TextViewUtils.addTextViewBottomIcon(textView, mVCodeBottomErrorIcon, bottomIconWidth, bottomIconHeight);}}public String getTextString() {return mCodeStringBuilder == null ? "" : mCodeStringBuilder.toString();}
}

这篇关于Android 之微信支付宝支付密码框点睛提要的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



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

相关文章

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应用的安全开发能力 目录