Android实现支付宝手势密码功能
程序员文章站
2023-12-16 20:45:22
今天就给大家分享android实现支付宝手势密码,很常见,像现在用微信支付,支付宝支付的时候都要自己设置的4位pin码,然后输入pin码后立即调用支付接口去支付,毫无疑问的...
今天就给大家分享android实现支付宝手势密码,很常见,像现在用微信支付,支付宝支付的时候都要自己设置的4位pin码,然后输入pin码后立即调用支付接口去支付,毫无疑问的安全问题,不会觉得手机被人拿了后却不知道自己网银的钱的去向,这也是正常安全逻辑性问题。
好,下面我们来看下具体的实现:
入口,启动页面就简单的几个按钮,布局贴出来,代码如下:
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <linearlayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margintop="20dp" android:orientation="horizontal"> <button android:id="@+id/button_enable_pin" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="设置" /> <button android:id="@+id/button_change_pin" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/button_enable_pin" android:text="改变" /> <button android:id="@+id/button_unlock_pin" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/button_change_pin" android:text="检验" /> <button android:id="@+id/button_compat_locked" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/button_unlock_pin" android:text="pin页面" /> </linearlayout> </relativelayout>
我们看下四个按钮的操作的事情,代码如下:
package com.github.orangegangsters.lollipin; import android.content.intent; import android.os.bundle; import android.view.view; import android.widget.toast; import com.github.orangegangsters.lollipin.lib.pinactivity; import com.github.orangegangsters.lollipin.lib.managers.applock; import lollipin.orangegangsters.github.com.lollipin.r; /** * created by laiyingtang on 2016/06/21. */ public class mainactivity extends pinactivity implements view.onclicklistener { private static final int request_code_enable = 11; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); this.findviewbyid(r.id.button_enable_pin).setonclicklistener(this); this.findviewbyid(r.id.button_change_pin).setonclicklistener(this); this.findviewbyid(r.id.button_unlock_pin).setonclicklistener(this); this.findviewbyid(r.id.button_compat_locked).setonclicklistener(this); } /** * 四个按钮的点击处理 * @param v */ @override public void onclick(view v) { intent intent = new intent(mainactivity.this, custompinactivity.class); switch (v.getid()) { //设置密码 case r.id.button_enable_pin: intent.putextra(applock.extra_type, applock.enable_pinlock); startactivityforresult(intent, request_code_enable); break; //改变密码 case r.id.button_change_pin: intent.putextra(applock.extra_type, applock.change_pin); startactivity(intent); break; //校验密码 case r.id.button_unlock_pin: intent.putextra(applock.extra_type, applock.unlock_pin); startactivity(intent); break; //填入手势密码进入pin页面 case r.id.button_compat_locked: intent intent2 = new intent(mainactivity.this, lockedcompatactivity.class); startactivity(intent2); break; } } //回调 @override protected void onactivityresult(int requestcode, int resultcode, intent data) { super.onactivityresult(requestcode, resultcode, data); switch (requestcode){ case request_code_enable: toast.maketext(this, "启用pin码", toast.length_short).show(); break; } } }
都看的明白,以下会把custompinactivity,lockedcompatactivity贴出来,前面的操作大概就是这样,分别给每个按钮设置不同的putextra值,第一个设置按钮利用的是startactivityforresult启动页面,因为设置完密码后是存入到appcation中的,而且拿到请求码回到主页面的时候,吐司了“启动pin码”,但是你想改变和检验密码必须要先设置密码,这是很正常的逻辑,我把appcation先贴出来,比较简单,几句代码:
package com.github.orangegangsters.lollipin; import android.app.application; import com.github.orangegangsters.lollipin.lib.managers.lockmanager; import lollipin.orangegangsters.github.com.lollipin.r; /** * created by laiyingtang on 2016/06/21. */ public class customapplication extends application { @suppresswarnings("unchecked") @override public void oncreate() { super.oncreate(); lockmanager<custompinactivity> lockmanager = lockmanager.getinstance(); lockmanager.enableapplock(this, custompinactivity.class); lockmanager.getapplock().setlogoid(r.drawable.security_lock); } }
lockmanager是采用网友的library。下面我把custompinactivity贴出来:
package com.github.orangegangsters.lollipin; import android.content.res.resources; import android.graphics.color; import android.graphics.typeface; import android.graphics.drawable.colordrawable; import android.widget.toast; import com.github.orangegangsters.lollipin.lib.managers.applockactivity; import lollipin.orangegangsters.github.com.lollipin.r; import uk.me.lewisdeane.ldialogs.basedialog; import uk.me.lewisdeane.ldialogs.customdialog; /** * 对话框页面 * created by laiyingtang on 2016/06/21. */ public class custompinactivity extends applockactivity { @override public void showforgotdialog() { resources res = getresources(); //创建对话框 customdialog.builder builder = new customdialog.builder(this, res.getstring(r.string.activity_dialog_title), res.getstring(r.string.activity_dialog_accept)); builder.content(res.getstring(r.string.activity_dialog_content)); builder.negativetext(res.getstring(r.string.activity_dialog_decline)); //设置样式 builder.darktheme(false); builder.typeface(typeface.sans_serif); builder.positivecolor(res.getcolor(r.color.light_blue_500)); // 设置按钮的字体颜色(确定) builder.negativecolor(res.getcolor(r.color.light_blue_500));// 设置按钮的字体颜色(取消) builder.righttoleft(false); // 按钮是否从左到右的顺序排列 builder.titlealignment(basedialog.alignment.center);//居中 builder.buttonalignment(basedialog.alignment.center); builder.setbuttonstacking(false); //设置字体的大小 builder.titletextsize((int) res.getdimension(r.dimen.activity_dialog_title_size)); builder.contenttextsize((int) res.getdimension(r.dimen.activity_dialog_content_size)); builder.positivebuttontextsize((int) res.getdimension(r.dimen.activity_dialog_positive_button_size)); builder.negativebuttontextsize((int) res.getdimension(r.dimen.activity_dialog_negative_button_size)); //创建对话框 customdialog customdialog = builder.build(); customdialog.setcanceledontouchoutside(false); customdialog.getwindow().setbackgrounddrawable(new colordrawable(color.transparent));//透明 customdialog.setclicklistener(new customdialog.clicklistener() { @override public void onconfirmclick() { toast.maketext(getapplicationcontext(), "yes", toast.length_short).show(); } @override public void oncancelclick() { toast.maketext(getapplicationcontext(), "cancel", toast.length_short).show(); } }); //显示对话框 customdialog.show(); } @override public void onpinfailure(int attempts) { } @override public void onpinsuccess(int attempts) { } @override public int getpinlength() { return super.getpinlength();//可重修修改默认的长度 } }
customdialog也是用第三方,而没有用系统自带的alertdialog,因为太丑了,想我这种追求完美的,都是丑拒的。设置密码的view是自定义的,包括keyboard。
lockedcompatactivity类如下:
package com.github.orangegangsters.lollipin; import android.os.bundle; import android.support.v7.widget.toolbar; import android.view.view; import android.widget.imageview; import android.widget.toast; import com.github.orangegangsters.lollipin.lib.pincompatactivity; import lollipin.orangegangsters.github.com.lollipin.r; /** * pin页面 * created by laiyingtang on 2016/06/21. */ public class lockedcompatactivity extends pincompatactivity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_compat_locked); initview(); } private void initview() { toolbar toolbar = (toolbar) findviewbyid(r.id.id_toolbar); imageview iv_image= (imageview) findviewbyid(r.id.iv_image); setsupportactionbar(toolbar); toolbar.settitle("title"); toolbar.settitletextcolor(getresources().getcolor(android.r.color.white)); toolbar.setsubtitletextcolor(getresources().getcolor(android.r.color.white)); toolbar.setlogo(r.drawable.ic_launcher); iv_image.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { toast.maketext(lockedcompatactivity.this,"可继续做相应的操作",toast.length_short).show(); } }); } }
该类是输入正确的pin码后才能访问的页面,从而在该页面可以操作你想操作的东西,比如调用支付接口,直接执行支付操作,这也是一个不错的例子。错误的pin码振动keyboard。
简单的讲述了下主要实现的代码,看下总体的效果:
最后附上源码,拿走不谢!下载地址
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。