Android开发笔记SQLite优化记住密码功能
程序员文章站
2023-01-26 23:10:40
本文实例为大家分享了android sqlite优化记住密码功能的具体代码,供大家参考,具体内容如下
package com.example.alimjan.he...
本文实例为大家分享了android sqlite优化记住密码功能的具体代码,供大家参考,具体内容如下
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("请选择用户类型"); 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("请输入密码"); btn_forget.settext("忘记密码"); ck_remember.setvisibility(view.visible); } else if (checkedid == r.id.rb_verifycode) { tv_password.settext(" 验证码:"); et_password.sethint("请输入验证码"); btn_forget.settext("获取验证码"); ck_remember.setvisibility(view.invisible); } } } private string[] typearray = {"个人用户", "公司用户"}; 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, "请输入正确的手机号", 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("请记住验证码"); builder.setmessage("手机号"+phone+",本次验证码是"+mverifycode+",请输入验证码"); 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, "请输入正确的手机号", toast.length_short).show(); return; } if (rb_password.ischecked() == true) { if (et_password.gettext().tostring().equals(mpassword) != true) { toast.maketext(this, "请输入正确的密码", toast.length_short).show(); return; } else { loginsuccess(); } } else if (rb_verifycode.ischecked() == true) { if (et_password.gettext().tostring().equals(mverifycode) != true) { toast.maketext(this, "请输入正确的验证码", 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"); } } //从修改密码页面返回登录页面,要清空密码的输入框 @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("您的手机号码是%s,类型是%s。恭喜你通过登录验证,点击“确定”按钮返回上个页面", 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); } } //为什么光标进入密码框事件不选onclick?因为要点两下才会触发onclick动作(第一下是切换焦点动作) @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="验证码登录" 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="手机号码:" 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="请输入手机号码" 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="请输入密码" 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, "请输入正确的手机号", toast.length_short).show(); return; } mverifycode = string.format("%06d", (int) (math.random() * 1000000 % 1000000)); alertdialog.builder builder = new alertdialog.builder(this); builder.settitle("请记住验证码"); builder.setmessage("手机号"+mphone+",本次验证码是"+mverifycode+",请输入验证码"); 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, "请输入正确的新密码", 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, "请输入正确的验证码", 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="请输入新密码" 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="确认新密码:" 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="请再次输入新密码" 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=" 验证码:" 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="请输入验证码" 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="获取验证码" 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>
当输入完手机号之后,点击密码编辑框时,从数据库查看内容,如果含有该号的密码则自动添加,如果没有则空。勾选记住密码选项之后,如果登陆成功则保存到数据库。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。