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开发中gradle下载缓慢的问题级解决方法

《Android开发中gradle下载缓慢的问题级解决方法》本文介绍了解决Android开发中Gradle下载缓慢问题的几种方法,本文给大家介绍的非常详细,感兴趣的朋友跟随小编一起看看吧... 目录一、网络环境优化二、Gradle版本与配置优化三、其他优化措施针对android开发中Gradle下载缓慢的问

MySQL9.0默认路径安装下重置root密码

《MySQL9.0默认路径安装下重置root密码》本文主要介绍了MySQL9.0默认路径安装下重置root密码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们... 目录问题描述环境描述解决方法正常模式下修改密码报错原因问题描述mysqlChina编程采用默认安装路径,

Android 悬浮窗开发示例((动态权限请求 | 前台服务和通知 | 悬浮窗创建 )

《Android悬浮窗开发示例((动态权限请求|前台服务和通知|悬浮窗创建)》本文介绍了Android悬浮窗的实现效果,包括动态权限请求、前台服务和通知的使用,悬浮窗权限需要动态申请并引导... 目录一、悬浮窗 动态权限请求1、动态请求权限2、悬浮窗权限说明3、检查动态权限4、申请动态权限5、权限设置完毕后

Android里面的Service种类以及启动方式

《Android里面的Service种类以及启动方式》Android中的Service分为前台服务和后台服务,前台服务需要亮身份牌并显示通知,后台服务则有启动方式选择,包括startService和b... 目录一句话总结:一、Service 的两种类型:1. 前台服务(必须亮身份牌)2. 后台服务(偷偷干

MySQL修改密码的四种实现方式

《MySQL修改密码的四种实现方式》文章主要介绍了如何使用命令行工具修改MySQL密码,包括使用`setpassword`命令和`mysqladmin`命令,此外,还详细描述了忘记密码时的处理方法,包... 目录mysql修改密码四种方式一、set password命令二、使用mysqladmin三、修改u

Android kotlin语言实现删除文件的解决方案

《Androidkotlin语言实现删除文件的解决方案》:本文主要介绍Androidkotlin语言实现删除文件的解决方案,在项目开发过程中,尤其是需要跨平台协作的项目,那么删除用户指定的文件的... 目录一、前言二、适用环境三、模板内容1.权限申请2.Activity中的模板一、前言在项目开发过程中,尤

电脑密码怎么设置? 一文读懂电脑密码的详细指南

《电脑密码怎么设置?一文读懂电脑密码的详细指南》为了保护个人隐私和数据安全,设置电脑密码显得尤为重要,那么,如何在电脑上设置密码呢?详细请看下文介绍... 设置电脑密码是保护个人隐私、数据安全以及系统安全的重要措施,下面以Windows 11系统为例,跟大家分享一下设置电脑密码的具体办php法。Windo

数据库oracle用户密码过期查询及解决方案

《数据库oracle用户密码过期查询及解决方案》:本文主要介绍如何处理ORACLE数据库用户密码过期和修改密码期限的问题,包括创建用户、赋予权限、修改密码、解锁用户和设置密码期限,文中通过代码介绍... 目录前言一、创建用户、赋予权限、修改密码、解锁用户和设置期限二、查询用户密码期限和过期后的修改1.查询用

Java中的密码加密方式

《Java中的密码加密方式》文章介绍了Java中使用MD5算法对密码进行加密的方法,以及如何通过加盐和多重加密来提高密码的安全性,MD5是一种不可逆的哈希算法,适合用于存储密码,因为其输出的摘要长度固... 目录Java的密码加密方式密码加密一般的应用方式是总结Java的密码加密方式密码加密【这里采用的

Android数据库Room的实际使用过程总结

《Android数据库Room的实际使用过程总结》这篇文章主要给大家介绍了关于Android数据库Room的实际使用过程,详细介绍了如何创建实体类、数据访问对象(DAO)和数据库抽象类,需要的朋友可以... 目录前言一、Room的基本使用1.项目配置2.创建实体类(Entity)3.创建数据访问对象(DAO