时间:2021-05-20
效果图如下,大家感觉不错请参考实现代码
具体代码如下所示:
private void setGesturePassword() { toggleMore.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if (isChecked) {// UIUtils.toast("开启了手势密码", false);// sp.edit().putBoolean("isOpen", true).commit(); String inputCode = sp.getString("inputCode", ""); if (TextUtils.isEmpty(inputCode)) {//之前没有设置过 new AlertDialog.Builder(MoreFragment.this.getActivity()) .setTitle("设置手势密码") .setMessage("是否现在设置手势密码") .setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { UIUtils.toast("现在设置手势密码", false); sp.edit().putBoolean("isOpen", true).commit();// toggleMore.setChecked(true); //开启新的activity: ((BaseActivity) MoreFragment.this.getActivity()).goToActivity(GestureEditActivity.class, null); } }) .setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { UIUtils.toast("取消了现在设置手势密码", false); sp.edit().putBoolean("isOpen", false).commit(); toggleMore.setChecked(false); } }) .show(); } else { UIUtils.toast("开启手势密码", false); sp.edit().putBoolean("isOpen", true).commit();// toggleMore.setChecked(true); } } else { UIUtils.toast("关闭了手势密码", false); sp.edit().putBoolean("isOpen", false).commit();// toggleMore.setChecked(false); } } });}private void resetGesture() { tvMoreReset.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { boolean checked = toggleMore.isChecked(); if (checked) { ((BaseActivity) MoreFragment.this.getActivity()).goToActivity(GestureEditActivity.class, null); } else { UIUtils.toast("手势密码操作已关闭,请开启后再设置", false); } } });}GestureEditActivity:
import android.app.Activity;import android.content.Context;import android.content.SharedPreferences;import android.os.Bundle;import android.text.Html;import android.text.TextUtils;import android.util.Log;import android.view.View;import android.view.animation.Animation;import android.view.animation.AnimationUtils;import android.widget.FrameLayout;import android.widget.TextView;import android.widget.Toast;import com.atguigu.gesturelock.widget.GestureContentView;import com.atguigu.gesturelock.widget.GestureDrawline;import com.atguigu.gesturelock.widget.LockIndicator;import com.atguigu.p2pinvest0828.R;public class GestureEditActivity extends Activity implements View.OnClickListener { /** * 手机号码 */ public static final String PARAM_PHONE_NUMBER = "PARAM_PHONE_NUMBER"; /** * 意图 */ public static final String PARAM_INTENT_CODE = "PARAM_INTENT_CODE"; /** * 首次提示绘制手势密码,可以选择跳过 */ public static final String PARAM_IS_FIRST_ADVICE = "PARAM_IS_FIRST_ADVICE"; private TextView mTextTitle; private TextView mTextCancel; private LockIndicator mLockIndicator; private TextView mTextTip; private FrameLayout mGestureContainer; private GestureContentView mGestureContentView; private TextView mTextReset; private String mParamSetUpcode = null; private String mParamPhoneNumber; private boolean mIsFirstInput = true; private String mFirstPassword = null; private String mConfirmPassword = null; private int mParamIntentCode; private SharedPreferences mSharedPreferences = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_gesture_edit); setUpViews(); setUpListeners(); } private void setUpViews() { mTextTitle = (TextView) findViewById(R.id.text_title); mTextCancel = (TextView) findViewById(R.id.text_cancel); mTextReset = (TextView) findViewById(R.id.text_reset); mTextReset.setClickable(false); mLockIndicator = (LockIndicator) findViewById(R.id.lock_indicator); mTextTip = (TextView) findViewById(R.id.text_tip); mGestureContainer = (FrameLayout) findViewById(R.id.gesture_container); mSharedPreferences = this.getSharedPreferences("secret_protect", Context.MODE_PRIVATE); // 初始化一个显示各个点的viewGroup mGestureContentView = new GestureContentView(this, false, "", new GestureDrawline.GestureCallBack() { @Override public void onGestureCodeInput(String inputCode) { if (!isInputPassValidate(inputCode)) { mTextTip.setText(Html.fromHtml("<font color='#c70c1e'>最少链接4个点, 请重新输入</font>")); mGestureContentView.clearDrawlineState(0L); return; } if (mIsFirstInput) { mFirstPassword = inputCode; updateCodeList(inputCode); mGestureContentView.clearDrawlineState(0L); mTextReset.setClickable(true); mTextReset.setText(getString(R.string.reset_gesture_code)); } else { if (inputCode.equals(mFirstPassword)) { Toast.makeText(GestureEditActivity.this, "设置成功", Toast.LENGTH_SHORT).show(); mGestureContentView.clearDrawlineState(0L); GestureEditActivity.this.finish(); } else { mTextTip.setText(Html.fromHtml("<font color='#c70c1e'>与上一次绘制不一致,请重新绘制</font>")); // 左右移动动画 Animation shakeAnimation = AnimationUtils.loadAnimation(GestureEditActivity.this, R.anim.shake); mTextTip.startAnimation(shakeAnimation); // 保持绘制的线,1.5秒后清除 mGestureContentView.clearDrawlineState(1300L); } } mIsFirstInput = false; } @Override public void checkedSuccess() { } @Override public void checkedFail() { } }); // 设置手势解锁显示到哪个布局里面 mGestureContentView.setParentView(mGestureContainer); updateCodeList(""); } private void setUpListeners() { mTextCancel.setOnClickListener(this); mTextReset.setOnClickListener(this); } private void updateCodeList(String inputCode) { // 更新选择的图案 mLockIndicator.setPath(inputCode); mSharedPreferences.edit().putString("inputCode",inputCode).commit(); Log.e("TAG", "inputCode = " + inputCode); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.text_cancel: this.finish(); break; case R.id.text_reset: mIsFirstInput = true; updateCodeList(""); mTextTip.setText(getString(R.string.set_gesture_pattern)); break; default: break; } } private boolean isInputPassValidate(String inputPassword) { if (TextUtils.isEmpty(inputPassword) || inputPassword.length() < 4) { return false; } return true; }}我的界面中:
//判断一下,是否开启了手势密码。如果开启:先输入手势密码SharedPreferences sp = this.getActivity().getSharedPreferences("secret_protect", Context.MODE_PRIVATE);boolean isOpen = sp.getBoolean("isOpen", false);if(isOpen){ ((BaseActivity)this.getActivity()).goToActivity(GestureVerifyActivity.class,null); return;}GestureVerifyActivity:
import android.app.Activity;import android.content.Context;import android.content.SharedPreferences;import android.os.Bundle;import android.text.Html;import android.text.TextUtils;import android.view.View;import android.view.animation.Animation;import android.view.animation.AnimationUtils;import android.widget.FrameLayout;import android.widget.ImageView;import android.widget.RelativeLayout;import android.widget.TextView;import android.widget.Toast;import com.atguigu.gesturelock.widget.GestureContentView;import com.atguigu.gesturelock.widget.GestureDrawline;import com.atguigu.p2pinvest0828.R;public class GestureVerifyActivity extends Activity implements View.OnClickListener { /** 手机号码*/ public static final String PARAM_PHONE_NUMBER = "PARAM_PHONE_NUMBER"; /** 意图 */ public static final String PARAM_INTENT_CODE = "PARAM_INTENT_CODE"; private RelativeLayout mTopLayout; private TextView mTextTitle; private TextView mTextCancel; private ImageView mImgUserLogo; private TextView mTextPhoneNumber; private TextView mTextTip; private FrameLayout mGestureContainer; private GestureContentView mGestureContentView; private TextView mTextForget; private TextView mTextOther; private String mParamPhoneNumber; private long mExitTime = 0; private int mParamIntentCode; private SharedPreferences mSharedPreferences; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_gesture_verify); ObtainExtraData(); setUpViews(); setUpListeners(); } private void ObtainExtraData() { mParamPhoneNumber = getIntent().getStringExtra(PARAM_PHONE_NUMBER); mParamIntentCode = getIntent().getIntExtra(PARAM_INTENT_CODE, 0); mSharedPreferences = this.getSharedPreferences("secret_protect", Context.MODE_PRIVATE); } private void setUpViews() { mTopLayout = (RelativeLayout) findViewById(R.id.top_layout); mTextTitle = (TextView) findViewById(R.id.text_title); mTextCancel = (TextView) findViewById(R.id.text_cancel); mImgUserLogo = (ImageView) findViewById(R.id.user_logo); mTextPhoneNumber = (TextView) findViewById(R.id.text_phone_number); mTextTip = (TextView) findViewById(R.id.text_tip); mGestureContainer = (FrameLayout) findViewById(R.id.gesture_container); mTextForget = (TextView) findViewById(R.id.text_forget_gesture); mTextOther = (TextView) findViewById(R.id.text_other_account); String inputCode = mSharedPreferences.getString("inputCode","1235789"); // 初始化一个显示各个点的viewGroup mGestureContentView = new GestureContentView(this, true, inputCode, new GestureDrawline.GestureCallBack() { @Override public void onGestureCodeInput(String inputCode) { } @Override public void checkedSuccess() { mGestureContentView.clearDrawlineState(0L); Toast.makeText(GestureVerifyActivity.this, "密码正确", Toast.LENGTH_SHORT).show(); GestureVerifyActivity.this.finish(); } @Override public void checkedFail() { mGestureContentView.clearDrawlineState(1300L); mTextTip.setVisibility(View.VISIBLE); mTextTip.setText(Html.fromHtml("<font color='#c70c1e'>密码错误</font>")); // 左右移动动画 Animation shakeAnimation = AnimationUtils.loadAnimation(GestureVerifyActivity.this, R.anim.shake); mTextTip.startAnimation(shakeAnimation); } }); // 设置手势解锁显示到哪个布局里面 mGestureContentView.setParentView(mGestureContainer); } private void setUpListeners() { mTextCancel.setOnClickListener(this); mTextForget.setOnClickListener(this); mTextOther.setOnClickListener(this); } private String getProtectedMobile(String phoneNumber) { if (TextUtils.isEmpty(phoneNumber) || phoneNumber.length() < 11) { return ""; } StringBuilder builder = new StringBuilder(); builder.append(phoneNumber.subSequence(0,3)); builder.append("****"); builder.append(phoneNumber.subSequence(7,11)); return builder.toString(); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.text_cancel: this.finish(); break; default: break; } }}总结
以上所述是小编给大家介绍的Android手势密码--设置和校验功能的实现代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
本文实例为大家分享了Android实现手势密码功能的具体代码,供大家参考,具体内容如下首先声明一下,九宫格布局是从网上扒了一个大神写好的,大家在项目中实现的话可
Android实现截屏功能的实例实现代码:publicclassScreenShot{//获取指定Activity的截屏,保存到png文件privatestat
Android开发之实现手势滑动的功能首先得Activity必须实现OnGestureListener接口,该接口提供了关于手势操作的一些方法,onDown方法
PHP5.5之后引入PasswordhashingAPI用于创建和校验哈希密码,它属于内核自带,无需进行任何扩展安装和配置。它主要提供了四个函数以供使用:●pa
大家在一些网站上经常可以看到数码密码会根据输入的密码长度显示安全条功能,此功能基于js如何实现的呢?下面看下实现代码,具体代码如下所示://根据密码长度显示安全