效果圖如下,大家感覺不錯請參考實現(xiàn)代碼
10年積累的成都做網(wǎng)站、網(wǎng)站設(shè)計、外貿(mào)營銷網(wǎng)站建設(shè)經(jīng)驗,可以快速應(yīng)對客戶對網(wǎng)站的新想法和需求。提供各種問題對應(yīng)的解決方案。讓選擇我們的客戶得到更好、更有力的網(wǎng)絡(luò)服務(wù)。我雖然不認(rèn)識你,你也不認(rèn)識我。但先網(wǎng)站制作后付款的網(wǎng)站建設(shè)流程,更有安居免費網(wǎng)站建設(shè)讓你可以放心的選擇與我們合作。
具體代碼如下所示:
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)) {//之前沒有設(shè)置過 new AlertDialog.Builder(MoreFragment.this.getActivity()) .setTitle("設(shè)置手勢密碼") .setMessage("是否現(xiàn)在設(shè)置手勢密碼") .setPositiveButton("確定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { UIUtils.toast("現(xiàn)在設(shè)置手勢密碼", 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("取消了現(xiàn)在設(shè)置手勢密碼", 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("關(guān)閉了手勢密碼", 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("手勢密碼操作已關(guān)閉,請開啟后再設(shè)置", 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 { /** * 手機(jī)號碼 */ 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("最少鏈接4個點, 請重新輸入")); 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, "設(shè)置成功", Toast.LENGTH_SHORT).show(); mGestureContentView.clearDrawlineState(0L); GestureEditActivity.this.finish(); } else { mTextTip.setText(Html.fromHtml("與上一次繪制不一致,請重新繪制")); // 左右移動動畫 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() { } }); // 設(shè)置手勢解鎖顯示到哪個布局里面 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 { /** 手機(jī)號碼*/ 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("密碼錯誤")); // 左右移動動畫 Animation shakeAnimation = AnimationUtils.loadAnimation(GestureVerifyActivity.this, R.anim.shake); mTextTip.startAnimation(shakeAnimation); } }); // 設(shè)置手勢解鎖顯示到哪個布局里面 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; } } }
總結(jié)
以上所述是小編給大家介紹的Android手勢密碼--設(shè)置和校驗功能的實現(xiàn)代碼,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對創(chuàng)新互聯(lián)網(wǎng)站的支持!