Android自定义View仿支付宝输入六位密码功能_Android

2024-02-24 05:48

本文主要是介绍Android自定义View仿支付宝输入六位密码功能_Android,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

摘要: 本文讲的是Android自定义View仿支付宝输入六位密码功能_Android,跟选择银行卡界面类似,也是用一个PopupWindow,不过输入密码界面是一个自定义view,当输入六位密码完成后用回调在Activity中获取到输入的密码并以Toast显示密码。效果图如下:自定义view布局效果图及代码如下:

跟选择银行卡界面类似,也是用一个PopupWindow,不过输入密码界面是一个自定义view,当输入六位密码完成后用回调在Activity中获取到输入的密码并以Toast显示密码。效果图如下:

20161017101009829.gif

自定义view布局效果图及代码如下:

20161017101136852.gif

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/bg_pop_window"
android:orientation="vertical">
<LinearLayout
android:id="@+id/ll_main_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#fff"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="50dp">
<ImageView
android:id="@+id/iv_pay_back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:background="@drawable/back_white"/>
<TextView
android:id="@+id/tv_pay_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginLeft="15dp"
android:layout_marginTop="15dp"
android:text="标题"
android:textColor="#333"
android:textSize="18dp"/>
</RelativeLayout>
<View
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:background="#e5e5e5"/>
<!-- 6位密码框布局,需要一个圆角边框的shape作为layout的背景 -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="20dp"
android:background="@drawable/shape_input_area"
android:orientation="horizontal">
<!-- inputType设置隐藏密码明文
textSize设置大一点,否则“点”太小了,不美观 -->
<TextView
android:id="@+id/tv_pass1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:inputType="numberPassword"
android:paddingBottom="5dp"
android:paddingTop="5dp"
android:textSize="32sp"/>
<View
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="#e5e5e5"/>
<TextView
android:id="@+id/tv_pass2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:inputType="numberPassword"
android:paddingBottom="5dp"
android:paddingTop="5dp"
android:textSize="32sp"/>
<View
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="#e5e5e5"/>
<TextView
android:id="@+id/tv_pass3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:inputType="numberPassword"
android:paddingBottom="5dp"
android:paddingTop="5dp"
android:textSize="32sp"/>
<View
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="#e5e5e5"/>
<TextView
android:id="@+id/tv_pass4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:inputType="numberPassword"
android:paddingBottom="5dp"
android:paddingTop="5dp"
android:textSize="32sp"/>
<View
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="#e5e5e5"/>
<TextView
android:id="@+id/tv_pass5"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:inputType="numberPassword"
android:paddingBottom="5dp"
android:paddingTop="5dp"
android:textSize="32sp"/>
<View
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="#e5e5e5"/>
<TextView
android:id="@+id/tv_pass6"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:inputType="numberPassword"
android:paddingBottom="5dp"
android:paddingTop="5dp"
android:textSize="32sp"/>
</LinearLayout>
<TextView
android:id="@+id/tv_pay_forgetPwd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_margin="15dp"
android:text="忘记密码?"
android:textColor="#354EEF"/>
<!-- 输入键盘 -->
<GridView
android:id="@+id/gv_keybord"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/ll_main_password"
android:layout_marginTop="30dp"
android:horizontalSpacing="0.5dp"
android:background="#8E8E8E"
android:numColumns="3"
android:verticalSpacing="0.5dp"/>
</LinearLayout>
</RelativeLayout>

java代码

/**
* Created by zhpan on 2016/9/25.
*/
public class PayView extends RelativeLayout{
private MainActivity mContext;
private String mStringPassword; //输入的密码
private TextView[] mTextViewPsw; // 用数组保存6个TextView
private GridView mGridView; //支付键盘布局
private ArrayList<Map<String, String>> valueList;
private ImageView mImageViewCancel;
private TextView mTextViewForgetPsw;
private int currentIndex = -1;// 用于记录当前输入密码格位置
private View mView;
private TextView mTextViewTitle;
private TextView mTextViewDel;
public PayView(Context context) {
super(context, null);
}
public PayView(Context context, AttributeSet attrs) {
super(context, attrs);
mContext = (MainActivity) context;
mView = View.inflate(context, R.layout.pay_view, null);
valueList = new ArrayList<>();
mTextViewPsw = new TextView[6];
mImageViewCancel = (ImageView) mView.findViewById(R.id.iv_pay_back);
mTextViewPsw[0] = (TextView) mView.findViewById(R.id.tv_pass1);
mTextViewPsw[1] = (TextView) mView.findViewById(R.id.tv_pass2);
mTextViewPsw[2] = (TextView) mView.findViewById(R.id.tv_pass3);
mTextViewPsw[3] = (TextView) mView.findViewById(R.id.tv_pass4);
mTextViewPsw[4] = (TextView) mView.findViewById(R.id.tv_pass5);
mTextViewPsw[5] = (TextView) mView.findViewById(R.id.tv_pass6);
mGridView = (GridView) mView.findViewById(R.id.gv_keybord);
mTextViewTitle = (TextView) mView.findViewById(R.id.tv_pay_title);
mTextViewForgetPsw = (TextView) mView.findViewById(R.id.tv_pay_forgetPwd);
setView();
addView(mView); //必须要,不然不显示控件
}
// 初始化按钮上应该显示的数字
private void setView() {
for (int i = 1; i < 13; i++) {
Map<String, String> map = new HashMap<>();
if (i < 10) {
map.put("name", String.valueOf(i));
} else if (i == 10) {
map.put("name", "");
} else if (i == 11) {
map.put("name", String.valueOf(0));
} else if (i == 12) {
map.put("name", "<-");
}
valueList.add(map);
}
mGridView.setAdapter(adapter);
}
/**
* 设置监听方法,在第6位输入完后触发
*/
public void setOnFinishInput(final OnPasswordInputFinish pass) {
mTextViewPsw[5].addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
if (s.toString().length() == 1) {
mStringPassword = ""; //每次触发都要将mStringPassword置空再重新获取,避免由于输入删除再输入造成混乱
for (int i = 0; i < 6; i++) {
mStringPassword += mTextViewPsw[i].getText().toString().trim();
}
pass.inputFinish();//接口中要实现的方法,完成密码输入完成后的响应逻辑
}
}
});
}
/**
* 获取输入的密码
*/
public String getPassword() {
return mStringPassword;
}
/**
* 返回取消支付的ImageView
*/
public ImageView getCancel() {
return mImageViewCancel;
}
/**
* 返回忘记密码的TextView
*/
public TextView getForgetPsw() {
return mTextViewForgetPsw;
}
/**
* 返回标题的TextView
*/
public TextView getTitle() {
return mTextViewTitle;
}
// GridView的适配器
BaseAdapter adapter = new BaseAdapter() {
@Override
public int getCount() {
return valueList.size();
}
@Override
public Object getItem(int position) {
return valueList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = View.inflate(mContext, R.layout.item_pay_gride, null);
holder = new ViewHolder();
holder.btnKey = (TextView) convertView.findViewById(R.id.btn_keys);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.btnKey.setText(valueList.get(position).get("name"));
if (position == 9) {
holder.btnKey.setBackgroundResource(R.drawable.selector_key_del);
}
if (position == 11) {
mTextViewDel = holder.btnKey;
holder.btnKey.setBackgroundResource(R.drawable.selector_key_del);
}
holder.btnKey.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (position < 11 && currentIndex != 9&&position!=9) { //点击0-9按钮
if (currentIndex >= -1 && currentIndex < 5) { //判断输入位置
mTextViewPsw[++currentIndex].setText(valueList.get(position).get("name"));
}
} else {
if (position == 11) { //点击退格键
if (currentIndex - 1 >= -1) { // 判断是否删除完毕
mTextViewPsw[currentIndex--].setText("");
}
}
if(position==9){
}
}
}
});
return convertView;
}
};
static class ViewHolder {
public TextView btnKey;
}
}

PopupWindow中直接使用该控件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.example.zhpan.mypayui.PayView
android:id="@+id/pv_pop_win"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>

MainActivity中显示PupupWindow

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private TextView mTextView;
private PopupWindow mPopupWindow;
private View popView;
private PayView mPayView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
setListener();
}
private void initView() {
mTextView = (TextView) findViewById(R.id.tv_main_pay);
}
private void setListener() {
mTextView.setOnClickListener(this);
}
// 显示弹窗
public void showPopupWindow() {
// 初始化弹窗
popView = View.inflate(this, R.layout.pop_window, null);
mPopupWindow = new PopupWindow(popView, ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT);
mPayView = (PayView) popView.findViewById(R.id.pv_pop_win);
mPayView.getTitle().setText("选择到账银行卡");
// 设置动画
mPopupWindow.setAnimationStyle(R.style.popwin_anim_style);
mPopupWindow.showAsDropDown(findViewById(R.id.ll_main), 0, 0);
mPopupWindow.setOutsideTouchable(true);
mPayView.setOnFinishInput(new OnPasswordInputFinish() {
@Override
public void inputFinish() {
Toast.makeText(MainActivity.this, mPayView.getPassword(), Toast.LENGTH_SHORT).show();
}
});
mPayView.getCancel().setOnClickListener(this);
mPayView.getForgetPsw().setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.tv_main_pay:
showPopupWindow();
break;
case R.id.iv_pay_back:
mPopupWindow.dismiss();
break;
case R.id.tv_pay_forgetPwd:
Toast.makeText(MainActivity.this,"忘记密码",Toast.LENGTH_SHORT).show();
break;
}
}
}

以上所述是小编给大家介绍的Android自定义View仿支付宝输入六位密码功能,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对云栖社区网站的支持!

以上是云栖社区小编为您精心准备的的内容,在云栖社区的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索android 自定义view android 自定义view、android自定义webview、android中自定义view、android的自定义view、android 自定义圆view,以便于您获取更多的相关知识。

这篇关于Android自定义View仿支付宝输入六位密码功能_Android的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



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

相关文章

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

SpringBoot整合DeepSeek实现AI对话功能

《SpringBoot整合DeepSeek实现AI对话功能》本文介绍了如何在SpringBoot项目中整合DeepSeekAPI和本地私有化部署DeepSeekR1模型,通过SpringAI框架简化了... 目录Spring AI版本依赖整合DeepSeek API key整合本地化部署的DeepSeek

Python实现多路视频多窗口播放功能

《Python实现多路视频多窗口播放功能》这篇文章主要为大家详细介绍了Python实现多路视频多窗口播放功能的相关知识,文中的示例代码讲解详细,有需要的小伙伴可以跟随小编一起学习一下... 目录一、python实现多路视频播放功能二、代码实现三、打包代码实现总结一、python实现多路视频播放功能服务端开

CSS自定义浏览器滚动条样式完整代码

《CSS自定义浏览器滚动条样式完整代码》:本文主要介绍了如何使用CSS自定义浏览器滚动条的样式,包括隐藏滚动条的角落、设置滚动条的基本样式、轨道样式和滑块样式,并提供了完整的CSS代码示例,通过这些技巧,你可以为你的网站添加个性化的滚动条样式,从而提升用户体验,详细内容请阅读本文,希望能对你有所帮助...

css实现图片旋转功能

《css实现图片旋转功能》:本文主要介绍了四种CSS变换效果:图片旋转90度、水平翻转、垂直翻转,并附带了相应的代码示例,详细内容请阅读本文,希望能对你有所帮助... 一 css实现图片旋转90度.icon{ -moz-transform:rotate(-90deg); -webkit-transfo

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

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

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

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

C语言小项目实战之通讯录功能

《C语言小项目实战之通讯录功能》:本文主要介绍如何设计和实现一个简单的通讯录管理系统,包括联系人信息的存储、增加、删除、查找、修改和排序等功能,文中通过代码介绍的非常详细,需要的朋友可以参考下... 目录功能介绍:添加联系人模块显示联系人模块删除联系人模块查找联系人模块修改联系人模块排序联系人模块源代码如下