Android開發(fā)筆記SQLite優(yōu)化記住密碼功能
本文實(shí)例為大家分享了Android SQLite優(yōu)化記住密碼功能的具體代碼,供大家參考,具體內(nèi)容如下
package com.example.alimjan.hello_world; /** * Created by alimjan on 7/4/2017. */ import com.example.alimjan.hello_world.bean.UserInfo; import com.example.alimjan.hello_world.dataBase.UserDBHelper; import com.example.alimjan.hello_world.Utils.DateUtil; import android.app.AlertDialog; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.text.Editable; import android.text.TextWatcher; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.view.View.OnFocusChangeListener; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.CheckBox; import android.widget.CompoundButton; import android.widget.EditText; import android.widget.RadioButton; import android.widget.RadioGroup; import android.widget.Spinner; import android.widget.TextView; import android.widget.Toast; import android.widget.AdapterView.OnItemSelectedListener; public class class_4_2_3 extends AppCompatActivity implements OnClickListener, OnFocusChangeListener { private RadioGroup rg_login; private RadioButton rb_password; private RadioButton rb_verifycode; private EditText et_phone; private TextView tv_password; private EditText et_password; private Button btn_forget; private CheckBox ck_remember; private Button btn_login; private int mRequestCode = 0; private int mType = 0; private boolean bRemember = false; private String mPassword = "111111"; private String mVerifyCode; private UserDBHelper mHelper; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.code_4_2_3); rg_login = (RadioGroup) findViewById(R.id.rg_login); rb_password = (RadioButton) findViewById(R.id.rb_password); rb_verifycode = (RadioButton) findViewById(R.id.rb_verifycode); et_phone = (EditText) findViewById(R.id.et_phone); tv_password = (TextView) findViewById(R.id.tv_password); et_password = (EditText) findViewById(R.id.et_password); btn_forget = (Button) findViewById(R.id.btn_forget); ck_remember = (CheckBox) findViewById(R.id.ck_remember); btn_login = (Button) findViewById(R.id.btn_login); rg_login.setOnCheckedChangeListener(new RadioListener()); ck_remember.setOnCheckedChangeListener(new CheckListener()); et_phone.addTextChangedListener(new HideTextWatcher(et_phone)); et_password.addTextChangedListener(new HideTextWatcher(et_password)); btn_forget.setOnClickListener(this); btn_login.setOnClickListener(this); et_password.setOnFocusChangeListener(this); ArrayAdapter<String> typeAdapter = new ArrayAdapter<String>(this, R.layout.item_select, typeArray); typeAdapter.setDropDownViewResource(R.layout.item_dropdown); Spinner sp_type = (Spinner) findViewById(R.id.sp_type); sp_type.setPrompt("請(qǐng)選擇用戶類型"); sp_type.setAdapter(typeAdapter); sp_type.setSelection(mType); sp_type.setOnItemSelectedListener(new TypeSelectedListener()); } private class RadioListener implements RadioGroup.OnCheckedChangeListener { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { if (checkedId == R.id.rb_password) { tv_password.setText("登錄密碼:"); et_password.setHint("請(qǐng)輸入密碼"); btn_forget.setText("忘記密碼"); ck_remember.setVisibility(View.VISIBLE); } else if (checkedId == R.id.rb_verifycode) { tv_password.setText(" 驗(yàn)證碼:"); et_password.setHint("請(qǐng)輸入驗(yàn)證碼"); btn_forget.setText("獲取驗(yàn)證碼"); ck_remember.setVisibility(View.INVISIBLE); } } } private String[] typeArray = {"個(gè)人用戶", "公司用戶"}; class TypeSelectedListener implements OnItemSelectedListener { public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) { mType = arg2; } public void onNothingSelected(AdapterView<?> arg0) { } } private class CheckListener implements CompoundButton.OnCheckedChangeListener { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if (buttonView.getId() == R.id.ck_remember) { bRemember = isChecked; } } } private class HideTextWatcher implements TextWatcher { private EditText mView; private int mMaxLength; private CharSequence mStr; public HideTextWatcher(EditText v) { super(); mView = v; mMaxLength = ViewUtil.getMaxLength(v); } @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { } @Override public void onTextChanged(CharSequence s, int start, int before, int count) { mStr = s; } @Override public void afterTextChanged(Editable s) { if (mStr == null || mStr.length() == 0) return; if ((mStr.length() == 11 && mMaxLength == 11) || (mStr.length() == 6 && mMaxLength == 6)) { ViewUtil.hideOneInputMethod(class_4_2_3.this, mView); } } } @Override public void onClick(View v) { String phone = et_phone.getText().toString(); if (v.getId() == R.id.btn_forget) { if (phone==null || phone.length()<11) { Toast.makeText(this, "請(qǐng)輸入正確的手機(jī)號(hào)", Toast.LENGTH_SHORT).show(); return; } if (rb_password.isChecked() == true) { Intent intent = new Intent(this, class_4_2_3_1.class); intent.putExtra("phone", phone); startActivityForResult(intent, mRequestCode); } else if (rb_verifycode.isChecked() == true) { mVerifyCode = String.format("%06d", (int)(Math.random()*1000000%1000000)); AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("請(qǐng)記住驗(yàn)證碼"); builder.setMessage("手機(jī)號(hào)"+phone+",本次驗(yàn)證碼是"+mVerifyCode+",請(qǐng)輸入驗(yàn)證碼"); builder.setPositiveButton("好的", null); AlertDialog alert = builder.create(); alert.show(); } } else if (v.getId() == R.id.btn_login) { if (phone==null || phone.length()<11) { Toast.makeText(this, "請(qǐng)輸入正確的手機(jī)號(hào)", Toast.LENGTH_SHORT).show(); return; } if (rb_password.isChecked() == true) { if (et_password.getText().toString().equals(mPassword) != true) { Toast.makeText(this, "請(qǐng)輸入正確的密碼", Toast.LENGTH_SHORT).show(); return; } else { loginSuccess(); } } else if (rb_verifycode.isChecked() == true) { if (et_password.getText().toString().equals(mVerifyCode) != true) { Toast.makeText(this, "請(qǐng)輸入正確的驗(yàn)證碼", Toast.LENGTH_SHORT).show(); return; } else { loginSuccess(); } } } } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == mRequestCode && data!=null) { //用戶密碼已改為新密碼 mPassword = data.getStringExtra("new_password"); } } //從修改密碼頁(yè)面返回登錄頁(yè)面,要清空密碼的輸入框 @Override protected void onRestart() { et_password.setText(""); super.onRestart(); } @Override protected void onResume() { super.onResume(); mHelper = UserDBHelper.getInstance(this, 2); mHelper.openWriteLink(); } @Override protected void onPause() { super.onPause(); mHelper.closeLink(); } private void loginSuccess() { String desc = String.format("您的手機(jī)號(hào)碼是%s,類型是%s。恭喜你通過登錄驗(yàn)證,點(diǎn)擊“確定”按鈕返回上個(gè)頁(yè)面", et_phone.getText().toString(), typeArray[mType]); AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("登錄成功"); builder.setMessage(desc); builder.setPositiveButton("確定返回", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { finish(); } }); builder.setNegativeButton("我再看看", null); AlertDialog alert = builder.create(); alert.show(); if (bRemember) { UserInfo info = new UserInfo(); info.phone = et_phone.getText().toString(); info.password = et_password.getText().toString(); info.update_time = DateUtil.getCurDateStr("yyyy-MM-dd HH:mm:ss"); mHelper.insert(info); } } //為什么光標(biāo)進(jìn)入密碼框事件不選onClick?因?yàn)橐c(diǎn)兩下才會(huì)觸發(fā)onClick動(dòng)作(第一下是切換焦點(diǎn)動(dòng)作) @Override public void onFocusChange(View v, boolean hasFocus) { String phone = et_phone.getText().toString(); if (v.getId() == R.id.et_password) { if (phone.length() > 0 && hasFocus == true) { UserInfo info = mHelper.queryByPhone(phone); if (info != null) { et_password.setText(info.password); }else{ et_password.setText(""); } } } } public static void startHome(Context mContext) { Intent intent = new Intent(mContext, class_4_2_3.class); mContext.startActivity(intent); } }
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:focusable="true" android:focusableInTouchMode="true" android:orientation="vertical" android:padding="5dp" > <RadioGroup android:id="@+id/rg_login" android:layout_width="match_parent" android:layout_height="60dp" android:orientation="horizontal" > <RadioButton android:id="@+id/rb_password" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:checked="true" android:gravity="left|center" android:text="密碼登錄" android:textColor="@color/black" android:textSize="17sp" /> <RadioButton android:id="@+id/rb_verifycode" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:checked="false" android:gravity="left|center" android:text="驗(yàn)證碼登錄" android:textColor="@color/black" android:textSize="17sp" /> </RadioGroup> <RelativeLayout android:layout_width="match_parent" android:layout_height="60dp" > <TextView android:id="@+id/tv_type" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_alignParentLeft="true" android:gravity="center" android:text=" 我是:" android:textColor="@color/black" android:textSize="17sp" /> <Spinner android:id="@+id/sp_type" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_toRightOf="@+id/tv_type" android:gravity="left|center" android:spinnerMode="dialog" /> </RelativeLayout> <RelativeLayout android:layout_width="match_parent" android:layout_height="60dp" > <TextView android:id="@+id/tv_phone" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_alignParentLeft="true" android:gravity="center" android:text="手機(jī)號(hào)碼:" android:textColor="@color/black" android:textSize="17sp" /> <EditText android:id="@+id/et_phone" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginBottom="5dp" android:layout_marginTop="5dp" android:layout_toRightOf="@+id/tv_phone" android:background="@drawable/editext_selector" android:gravity="left|center" android:hint="請(qǐng)輸入手機(jī)號(hào)碼" android:inputType="number" android:maxLength="11" android:textColor="@color/black" android:textColorHint="@color/grey" android:textCursorDrawable="@drawable/text_cursor" android:textSize="17sp" /> </RelativeLayout> <RelativeLayout android:layout_width="match_parent" android:layout_height="60dp" > <TextView android:id="@+id/tv_password" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_alignParentLeft="true" android:gravity="center" android:text="登錄密碼:" android:textColor="@color/black" android:textSize="17sp" /> <FrameLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_toRightOf="@+id/tv_password" > <EditText android:id="@+id/et_password" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginBottom="5dp" android:layout_marginTop="5dp" android:background="@drawable/editext_selector" android:gravity="left|center" android:hint="請(qǐng)輸入密碼" android:inputType="numberPassword" android:maxLength="6" android:textColor="@color/black" android:textColorHint="@color/grey" android:textCursorDrawable="@drawable/text_cursor" android:textSize="17sp" /> <Button android:id="@+id/btn_forget" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_gravity="right" android:gravity="center" android:text="忘記密碼" android:textColor="@color/black" android:textSize="17sp" /> </FrameLayout> </RelativeLayout> <CheckBox android:id="@+id/ck_remember" android:layout_width="match_parent" android:layout_height="wrap_content" android:button="@drawable/checkbox_selector" android:checked="false" android:padding="10dp" android:text="記住密碼" android:textColor="@color/black" android:textSize="17sp" /> <Button android:id="@+id/btn_login" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="登錄" android:textColor="@color/black" android:textSize="22sp" /> </LinearLayout>
package com.example.alimjan.hello_world; /** * Created by alimjan on 7/4/2017. */ import android.app.Activity; import android.app.AlertDialog; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.view.View.OnClickListener; import android.widget.EditText; import android.widget.Toast; public class class_4_2_3_1 extends AppCompatActivity implements OnClickListener { private EditText et_password_first; private EditText et_password_second; private EditText et_verifycode; private String mVerifyCode; private String mPhone; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.code_4_2_3_1); et_password_first = (EditText) findViewById(R.id.et_password_first); et_password_second = (EditText) findViewById(R.id.et_password_second); et_verifycode = (EditText) findViewById(R.id.et_verifycode); findViewById(R.id.btn_verifycode).setOnClickListener(this); findViewById(R.id.btn_confirm).setOnClickListener(this); mPhone = getIntent().getStringExtra("phone"); } @Override public void onClick(View v) { if (v.getId() == R.id.btn_verifycode) { if (mPhone==null || mPhone.length()<11) { Toast.makeText(this, "請(qǐng)輸入正確的手機(jī)號(hào)", Toast.LENGTH_SHORT).show(); return; } mVerifyCode = String.format("%06d", (int) (Math.random() * 1000000 % 1000000)); AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("請(qǐng)記住驗(yàn)證碼"); builder.setMessage("手機(jī)號(hào)"+mPhone+",本次驗(yàn)證碼是"+mVerifyCode+",請(qǐng)輸入驗(yàn)證碼"); builder.setPositiveButton("好的", null); AlertDialog alert = builder.create(); alert.show(); } else if (v.getId() == R.id.btn_confirm) { String password_first = et_password_first.getText().toString(); String password_second = et_password_second.getText().toString(); if (password_first==null || password_first.length()<6 || password_second==null || password_second.length()<6) { Toast.makeText(this, "請(qǐng)輸入正確的新密碼", Toast.LENGTH_SHORT).show(); return; } if (password_first.equals(password_second) != true) { Toast.makeText(this, "兩次輸入的新密碼不一致", Toast.LENGTH_SHORT).show(); return; } if (et_verifycode.getText().toString().equals(mVerifyCode) != true) { Toast.makeText(this, "請(qǐng)輸入正確的驗(yàn)證碼", Toast.LENGTH_SHORT).show(); return; } else { Toast.makeText(this, "密碼修改成功", Toast.LENGTH_SHORT).show(); Intent intent = new Intent(); intent.putExtra("new_password", password_first); setResult(Activity.RESULT_OK, intent); finish(); } } } public static void startHome(Context mContext) { Intent intent = new Intent(mContext, class_4_2_3_1.class); mContext.startActivity(intent); } }
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:focusable="true" android:focusableInTouchMode="true" android:orientation="vertical" android:padding="5dp" > <RelativeLayout android:layout_width="match_parent" android:layout_height="60dp" > <TextView android:id="@+id/tv_password_first" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_alignParentLeft="true" android:gravity="center" android:text="輸入新密碼:" android:textColor="@color/black" android:textSize="17sp" /> <EditText android:id="@+id/et_password_first" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginBottom="5dp" android:layout_marginTop="5dp" android:layout_toRightOf="@+id/tv_password_first" android:background="@drawable/editext_selector" android:gravity="left|center" android:hint="請(qǐng)輸入新密碼" android:inputType="numberPassword" android:maxLength="11" android:textColor="@color/black" android:textColorHint="@color/grey" android:textCursorDrawable="@drawable/text_cursor" android:textSize="17sp" /> </RelativeLayout> <RelativeLayout android:layout_width="match_parent" android:layout_height="60dp" > <TextView android:id="@+id/tv_password_second" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_alignParentLeft="true" android:gravity="center" android:text="確認(rèn)新密碼:" android:textColor="@color/black" android:textSize="17sp" /> <EditText android:id="@+id/et_password_second" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginBottom="5dp" android:layout_marginTop="5dp" android:layout_toRightOf="@+id/tv_password_second" android:background="@drawable/editext_selector" android:gravity="left|center" android:hint="請(qǐng)?jiān)俅屋斎胄旅艽a" android:inputType="numberPassword" android:maxLength="11" android:textColor="@color/black" android:textColorHint="@color/grey" android:textCursorDrawable="@drawable/text_cursor" android:textSize="17sp" /> </RelativeLayout> <RelativeLayout android:layout_width="match_parent" android:layout_height="60dp" > <TextView android:id="@+id/tv_verifycode" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_alignParentLeft="true" android:gravity="center" android:text=" 驗(yàn)證碼:" android:textColor="@color/black" android:textSize="17sp" /> <FrameLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_toRightOf="@+id/tv_verifycode" > <EditText android:id="@+id/et_verifycode" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginBottom="5dp" android:layout_marginTop="5dp" android:background="@drawable/editext_selector" android:gravity="left|center" android:hint="請(qǐng)輸入驗(yàn)證碼" android:inputType="numberPassword" android:maxLength="6" android:textColor="@color/black" android:textColorHint="@color/grey" android:textCursorDrawable="@drawable/text_cursor" android:textSize="17sp" /> <Button android:id="@+id/btn_verifycode" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_gravity="right" android:gravity="center" android:text="獲取驗(yàn)證碼" android:textColor="@color/black" android:textSize="17sp" /> </FrameLayout> </RelativeLayout> <Button android:id="@+id/btn_confirm" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="確定" android:textColor="@color/black" android:textSize="22sp" /> </LinearLayout>
當(dāng)輸入完手機(jī)號(hào)之后,點(diǎn)擊密碼編輯框時(shí),從數(shù)據(jù)庫(kù)查看內(nèi)容,如果含有該號(hào)的密碼則自動(dòng)添加,如果沒有則空。勾選記住密碼選項(xiàng)之后,如果登陸成功則保存到數(shù)據(jù)庫(kù)。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android實(shí)現(xiàn)記住密碼功能
- Android SharedPreferences實(shí)現(xiàn)記住密碼和自動(dòng)登錄
- Android實(shí)現(xiàn)用戶登錄記住密碼功能
- Android sharedPreferences實(shí)現(xiàn)記住密碼功能
- Android 使用SharedPreferrences儲(chǔ)存密碼登錄界面記住密碼功能
- Android實(shí)現(xiàn)登錄界面記住密碼的存儲(chǔ)
- Android SharedPreferences實(shí)現(xiàn)記住密碼和自動(dòng)登錄界面
- Android實(shí)現(xiàn)帶有記住密碼功能的登陸界面
- Android通過記住密碼功能學(xué)習(xí)數(shù)據(jù)存儲(chǔ)類SharedPreferences詳解及實(shí)例
- Android實(shí)現(xiàn)登陸界面的記住密碼功能
相關(guān)文章
Android實(shí)現(xiàn)軟件列表的點(diǎn)擊啟動(dòng)另外一個(gè)程序功能【附demo源碼下載】
這篇文章主要介紹了Android實(shí)現(xiàn)軟件列表的點(diǎn)擊啟動(dòng)另外一個(gè)程序功能,涉及Android針對(duì)應(yīng)用程序的讀取、加載、啟動(dòng)等操作相關(guān)技巧,需要的朋友可以參考下2016-07-07Flutter質(zhì)感設(shè)計(jì)之模態(tài)底部面板
這篇文章主要為大家詳細(xì)介紹了Flutter質(zhì)感設(shè)計(jì)之模態(tài)底部面板,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-08-08解決Android ListView數(shù)據(jù)為空及加載錯(cuò)誤的方法
這篇文章主要為大家提供了一個(gè)解決Android ListView數(shù)據(jù)為空及加載錯(cuò)誤的方法,感興趣的小伙伴們可以參考一下2016-02-02Android實(shí)現(xiàn)獲取短信驗(yàn)證碼并自動(dòng)填寫功能
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)獲取短信驗(yàn)證碼并自動(dòng)填寫功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07Android 開發(fā)中使用Linux Shell實(shí)例詳解
這篇文章主要介紹了Android 開發(fā)中使用Linux Shell實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-03-03解決Android Studio Log.v和Log.d不顯示的問題
這篇文章主要介紹了解決Android Studio Log.v和Log.d不顯示的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來(lái)看看吧2020-08-08Android自定義流式布局的實(shí)現(xiàn)示例
這篇文章主要介紹了Android自定義流式布局的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12android studio與手機(jī)連接調(diào)試步驟詳解
這篇文章主要為大家詳細(xì)介紹了android studio與手機(jī)連接調(diào)試步驟,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-07-07