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

相关文章

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

SpringBoot实现微信小程序支付功能

《SpringBoot实现微信小程序支付功能》小程序支付功能已成为众多应用的核心需求之一,本文主要介绍了SpringBoot实现微信小程序支付功能,文中通过示例代码介绍的非常详细,对大家的学习或者工作... 目录一、引言二、准备工作(一)微信支付商户平台配置(二)Spring Boot项目搭建(三)配置文件

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

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