Android手势密码--设置和校验
程序员文章站
2022-08-27 12:32:23
GestureEditActivity: 1 import android.app.Activity; 2 import android.content.Context; 3 import android.content.SharedPreferences; 4 import android.os. ......
1 private void setGesturePassword() { 2 toggleMore.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 3 @Override 4 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 5 if (isChecked) { 6 // UIUtils.toast("开启了手势密码", false); 7 // sp.edit().putBoolean("isOpen", true).commit(); 8 String inputCode = sp.getString("inputCode", ""); 9 if (TextUtils.isEmpty(inputCode)) {//之前没有设置过 10 new AlertDialog.Builder(MoreFragment.this.getActivity()) 11 .setTitle("设置手势密码") 12 .setMessage("是否现在设置手势密码") 13 .setPositiveButton("确定", new DialogInterface.OnClickListener() { 14 @Override 15 public void onClick(DialogInterface dialog, int which) { 16 UIUtils.toast("现在设置手势密码", false); 17 sp.edit().putBoolean("isOpen", true).commit(); 18 // toggleMore.setChecked(true); 19 //开启新的activity: 20 ((BaseActivity) MoreFragment.this.getActivity()).goToActivity(GestureEditActivity.class, null); 21 } 22 }) 23 .setNegativeButton("取消", new DialogInterface.OnClickListener() { 24 @Override 25 public void onClick(DialogInterface dialog, int which) { 26 UIUtils.toast("取消了现在设置手势密码", false); 27 sp.edit().putBoolean("isOpen", false).commit(); 28 toggleMore.setChecked(false); 29 30 } 31 }) 32 .show(); 33 } else { 34 UIUtils.toast("开启手势密码", false); 35 sp.edit().putBoolean("isOpen", true).commit(); 36 // toggleMore.setChecked(true); 37 } 38 } else { 39 UIUtils.toast("关闭了手势密码", false); 40 sp.edit().putBoolean("isOpen", false).commit(); 41 // toggleMore.setChecked(false); 42 43 } 44 } 45 }); 46 } 47 48 private void resetGesture() { 49 tvMoreReset.setOnClickListener(new View.OnClickListener() { 50 @Override 51 public void onClick(View v) { 52 boolean checked = toggleMore.isChecked(); 53 if (checked) { 54 ((BaseActivity) MoreFragment.this.getActivity()).goToActivity(GestureEditActivity.class, null); 55 } else { 56 UIUtils.toast("手势密码操作已关闭,请开启后再设置", false); 57 } 58 } 59 }); 60 }
GestureEditActivity:
1 import android.app.Activity; 2 import android.content.Context; 3 import android.content.SharedPreferences; 4 import android.os.Bundle; 5 import android.text.Html; 6 import android.text.TextUtils; 7 import android.util.Log; 8 import android.view.View; 9 import android.view.animation.Animation; 10 import android.view.animation.AnimationUtils; 11 import android.widget.FrameLayout; 12 import android.widget.TextView; 13 import android.widget.Toast; 14 15 import com.atguigu.gesturelock.widget.GestureContentView; 16 import com.atguigu.gesturelock.widget.GestureDrawline; 17 import com.atguigu.gesturelock.widget.LockIndicator; 18 import com.atguigu.p2pinvest0828.R; 19 20 public class GestureEditActivity extends Activity implements View.OnClickListener { 21 /** 22 * 手机号码 23 */ 24 public static final String PARAM_PHONE_NUMBER = "PARAM_PHONE_NUMBER"; 25 /** 26 * 意图 27 */ 28 public static final String PARAM_INTENT_CODE = "PARAM_INTENT_CODE"; 29 /** 30 * 首次提示绘制手势密码,可以选择跳过 31 */ 32 public static final String PARAM_IS_FIRST_ADVICE = "PARAM_IS_FIRST_ADVICE"; 33 private TextView mTextTitle; 34 private TextView mTextCancel; 35 private LockIndicator mLockIndicator; 36 private TextView mTextTip; 37 private FrameLayout mGestureContainer; 38 private GestureContentView mGestureContentView; 39 private TextView mTextReset; 40 private String mParamSetUpcode = null; 41 private String mParamPhoneNumber; 42 private boolean mIsFirstInput = true; 43 private String mFirstPassword = null; 44 private String mConfirmPassword = null; 45 private int mParamIntentCode; 46 47 private SharedPreferences mSharedPreferences = null; 48 49 @Override 50 public void onCreate(Bundle savedInstanceState) { 51 super.onCreate(savedInstanceState); 52 setContentView(R.layout.activity_gesture_edit); 53 setUpViews(); 54 setUpListeners(); 55 } 56 57 private void setUpViews() { 58 mTextTitle = (TextView) findViewById(R.id.text_title); 59 mTextCancel = (TextView) findViewById(R.id.text_cancel); 60 mTextReset = (TextView) findViewById(R.id.text_reset); 61 mTextReset.setClickable(false); 62 mLockIndicator = (LockIndicator) findViewById(R.id.lock_indicator); 63 mTextTip = (TextView) findViewById(R.id.text_tip); 64 mGestureContainer = (FrameLayout) findViewById(R.id.gesture_container); 65 mSharedPreferences = this.getSharedPreferences("secret_protect", Context.MODE_PRIVATE); 66 // 初始化一个显示各个点的viewGroup 67 mGestureContentView = new GestureContentView(this, false, "", new GestureDrawline.GestureCallBack() { 68 @Override 69 public void onGestureCodeInput(String inputCode) { 70 71 if (!isInputPassValidate(inputCode)) { 72 mTextTip.setText(Html.fromHtml("<font color='#c70c1e'>最少链接4个点, 请重新输入</font>")); 73 mGestureContentView.clearDrawlineState(0L); 74 75 return; 76 } 77 78 if (mIsFirstInput) { 79 mFirstPassword = inputCode; 80 81 updateCodeList(inputCode); 82 83 mGestureContentView.clearDrawlineState(0L); 84 mTextReset.setClickable(true); 85 mTextReset.setText(getString(R.string.reset_gesture_code)); 86 } else { 87 88 if (inputCode.equals(mFirstPassword)) { 89 90 Toast.makeText(GestureEditActivity.this, "设置成功", Toast.LENGTH_SHORT).show(); 91 mGestureContentView.clearDrawlineState(0L); 92 GestureEditActivity.this.finish(); 93 } else { 94 mTextTip.setText(Html.fromHtml("<font color='#c70c1e'>与上一次绘制不一致,请重新绘制</font>")); 95 96 // 左右移动动画 97 Animation shakeAnimation = AnimationUtils.loadAnimation(GestureEditActivity.this, R.anim.shake); 98 mTextTip.startAnimation(shakeAnimation); 99 100 // 保持绘制的线,1.5秒后清除 101 mGestureContentView.clearDrawlineState(1300L); 102 } 103 } 104 105 mIsFirstInput = false; 106 } 107 108 @Override 109 public void checkedSuccess() { 110 111 } 112 113 @Override 114 public void checkedFail() { 115 116 } 117 }); 118 119 // 设置手势解锁显示到哪个布局里面 120 mGestureContentView.setParentView(mGestureContainer); 121 122 updateCodeList(""); 123 } 124 125 private void setUpListeners() { 126 mTextCancel.setOnClickListener(this); 127 mTextReset.setOnClickListener(this); 128 } 129 130 private void updateCodeList(String inputCode) { 131 132 // 更新选择的图案 133 mLockIndicator.setPath(inputCode); 134 mSharedPreferences.edit().putString("inputCode",inputCode).commit(); 135 Log.e("TAG", "inputCode = " + inputCode); 136 } 137 138 @Override 139 public void onClick(View v) { 140 141 switch (v.getId()) { 142 case R.id.text_cancel: 143 this.finish(); 144 break; 145 146 case R.id.text_reset: 147 mIsFirstInput = true; 148 updateCodeList(""); 149 mTextTip.setText(getString(R.string.set_gesture_pattern)); 150 break; 151 152 153 default: 154 break; 155 } 156 } 157 158 private boolean isInputPassValidate(String inputPassword) { 159 160 if (TextUtils.isEmpty(inputPassword) || inputPassword.length() < 4) { 161 return false; 162 } 163 164 return true; 165 } 166 }
我的界面中:
//判断一下,是否开启了手势密码。如果开启:先输入手势密码 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:
1 import android.app.Activity; 2 import android.content.Context; 3 import android.content.SharedPreferences; 4 import android.os.Bundle; 5 import android.text.Html; 6 import android.text.TextUtils; 7 import android.view.View; 8 import android.view.animation.Animation; 9 import android.view.animation.AnimationUtils; 10 import android.widget.FrameLayout; 11 import android.widget.ImageView; 12 import android.widget.RelativeLayout; 13 import android.widget.TextView; 14 import android.widget.Toast; 15 16 import com.atguigu.gesturelock.widget.GestureContentView; 17 import com.atguigu.gesturelock.widget.GestureDrawline; 18 import com.atguigu.p2pinvest0828.R; 19 20 public class GestureVerifyActivity extends Activity implements View.OnClickListener { 21 /** 手机号码*/ 22 public static final String PARAM_PHONE_NUMBER = "PARAM_PHONE_NUMBER"; 23 /** 意图 */ 24 public static final String PARAM_INTENT_CODE = "PARAM_INTENT_CODE"; 25 private RelativeLayout mTopLayout; 26 private TextView mTextTitle; 27 private TextView mTextCancel; 28 private ImageView mImgUserLogo; 29 private TextView mTextPhoneNumber; 30 private TextView mTextTip; 31 private FrameLayout mGestureContainer; 32 private GestureContentView mGestureContentView; 33 private TextView mTextForget; 34 private TextView mTextOther; 35 private String mParamPhoneNumber; 36 private long mExitTime = 0; 37 private int mParamIntentCode; 38 private SharedPreferences mSharedPreferences; 39 40 @Override 41 public void onCreate(Bundle savedInstanceState) { 42 super.onCreate(savedInstanceState); 43 44 setContentView(R.layout.activity_gesture_verify); 45 46 ObtainExtraData(); 47 48 setUpViews(); 49 50 setUpListeners(); 51 } 52 53 private void ObtainExtraData() { 54 mParamPhoneNumber = getIntent().getStringExtra(PARAM_PHONE_NUMBER); 55 mParamIntentCode = getIntent().getIntExtra(PARAM_INTENT_CODE, 0); 56 mSharedPreferences = this.getSharedPreferences("secret_protect", Context.MODE_PRIVATE); 57 } 58 59 private void setUpViews() { 60 mTopLayout = (RelativeLayout) findViewById(R.id.top_layout); 61 mTextTitle = (TextView) findViewById(R.id.text_title); 62 mTextCancel = (TextView) findViewById(R.id.text_cancel); 63 mImgUserLogo = (ImageView) findViewById(R.id.user_logo); 64 mTextPhoneNumber = (TextView) findViewById(R.id.text_phone_number); 65 mTextTip = (TextView) findViewById(R.id.text_tip); 66 mGestureContainer = (FrameLayout) findViewById(R.id.gesture_container); 67 mTextForget = (TextView) findViewById(R.id.text_forget_gesture); 68 mTextOther = (TextView) findViewById(R.id.text_other_account); 69 70 String inputCode = mSharedPreferences.getString("inputCode","1235789"); 71 // 初始化一个显示各个点的viewGroup 72 mGestureContentView = new GestureContentView(this, true, inputCode, 73 new GestureDrawline.GestureCallBack() { 74 75 @Override 76 public void onGestureCodeInput(String inputCode) { 77 78 } 79 80 @Override 81 public void checkedSuccess() { 82 83 mGestureContentView.clearDrawlineState(0L); 84 85 Toast.makeText(GestureVerifyActivity.this, "密码正确", Toast.LENGTH_SHORT).show(); 86 87 GestureVerifyActivity.this.finish(); 88 } 89 90 @Override 91 public void checkedFail() { 92 93 mGestureContentView.clearDrawlineState(1300L); 94 mTextTip.setVisibility(View.VISIBLE); 95 mTextTip.setText(Html.fromHtml("<font color='#c70c1e'>密码错误</font>")); 96 97 // 左右移动动画 98 Animation shakeAnimation = AnimationUtils.loadAnimation(GestureVerifyActivity.this, R.anim.shake); 99 mTextTip.startAnimation(shakeAnimation); 100 } 101 }); 102 103 // 设置手势解锁显示到哪个布局里面 104 mGestureContentView.setParentView(mGestureContainer); 105 } 106 107 private void setUpListeners() { 108 mTextCancel.setOnClickListener(this); 109 mTextForget.setOnClickListener(this); 110 mTextOther.setOnClickListener(this); 111 } 112 113 private String getProtectedMobile(String phoneNumber) { 114 115 if (TextUtils.isEmpty(phoneNumber) || phoneNumber.length() < 11) { 116 return ""; 117 } 118 119 StringBuilder builder = new StringBuilder(); 120 builder.append(phoneNumber.subSequence(0,3)); 121 builder.append("****"); 122 builder.append(phoneNumber.subSequence(7,11)); 123 124 return builder.toString(); 125 } 126 127 128 129 @Override 130 public void onClick(View v) { 131 132 switch (v.getId()) { 133 case R.id.text_cancel: 134 this.finish(); 135 break; 136 137 default: 138 break; 139 } 140 } 141 }
上一篇: 如何实施大数据工程?
下一篇: 宇通客车物联网战略探秘