Android SharedPreferences实现记住密码和自动登录
程序员文章站
2023-10-22 16:42:15
本文实例为大家分享了android sharedpreferences实现记住密码和自动登录,供大家参考,具体内容如下
效果图:
第一次进入进来
勾选记住密码和自...
本文实例为大家分享了android sharedpreferences实现记住密码和自动登录,供大家参考,具体内容如下
效果图:
第一次进入进来
勾选记住密码和自动登录成功后,第二次进来
说明:中间存在的图片或者多余的其他部分可删掉。留下最主要的填写部分和登陆按钮即可。功能还是可以实现的。
xml文件
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:background="@drawable/bj" tools:context="com.example.application.mainactivity"> <imageview android:layout_margintop="50dp" android:layout_width="100dp" android:layout_height="100dp" android:src="@drawable/login_tx_1" android:layout_gravity="center" /> <framelayout android:layout_width="match_parent" android:layout_height="wrap_content"> <edittext android:layout_width="300dp" android:layout_height="wrap_content" android:layout_gravity="center" android:focusable="true" android:focusableintouchmode="true" android:clickable="true" android:hint="请输入账号" android:gravity="center" android:paddingright="100dp" android:id="@+id/login_uname" /> <textview android:layout_width="38dp" android:layout_height="33dp" android:layout_marginleft="30dp" android:padding="6dp" android:gravity="center" android:drawableleft="@drawable/uname" /> </framelayout> <framelayout android:layout_width="match_parent" android:layout_height="wrap_content"> <edittext android:layout_width="300dp" android:layout_height="wrap_content" android:layout_gravity="center" android:focusable="true" android:focusableintouchmode="true" android:clickable="true" android:hint="请输入密码" android:gravity="center" android:paddingright="100dp" android:password="true" android:id="@+id/login_upass" /> <textview android:layout_width="38dp" android:layout_height="33dp" android:layout_marginleft="30dp" android:padding="6dp" android:gravity="center" android:drawableleft="@drawable/upass" /> </framelayout> <linearlayout android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" > <checkbox android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="记住密码" android:id="@+id/login_auto" /> <checkbox android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginleft="50dp" android:text="自动登录" android:id="@+id/login_btn" /> </linearlayout> <linearlayout android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:layout_margin="15dp" > <imagebutton android:layout_width="40dp" android:layout_height="40dp" android:src="@drawable/login_qq" /> <imagebutton android:layout_width="40dp" android:layout_height="40dp" android:src="@drawable/login_weixin" android:layout_marginleft="60dp" /> <imagebutton android:layout_width="40dp" android:layout_height="40dp" android:src="@drawable/login_xinlan" android:layout_marginleft="60dp" android:id="@+id/login_xinlan" /> </linearlayout> <linearlayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <button android:layout_width="240dp" android:layout_height="wrap_content" android:layout_gravity="center" android:background="@drawable/a_radio_button_selector_1" android:gravity="center" android:text="登录" android:id="@+id/login_login" /> <button android:layout_width="240dp" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_margintop="20dp" android:background="@drawable/a_radio_button_selector_1" android:gravity="center" android:text="忘记密码" android:id="@+id/login_find" /> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_margintop="20dp" android:text="没有账号,立即注册" android:textcolor="#6efafa" android:textsize="15dp" android:onclick="jumpregister" /> </linearlayout> </linearlayout>
java文件
package com.example.application; import android.content.intent; import android.content.sharedpreferences; import android.database.cursor; import android.database.sqlite.sqlitedatabase; import android.support.v7.app.appcompatactivity; import android.os.bundle; import android.util.log; import android.view.view; import android.view.window; import android.widget.button; import android.widget.checkbox; import android.widget.edittext; import android.widget.imagebutton; import android.widget.toast; import com.sun.util.dbhelper; import java.util.hashmap; import cn.sharesdk.framework.platform; import cn.sharesdk.framework.platformactionlistener; import cn.sharesdk.framework.sharesdk; import cn.sharesdk.sina.weibo.sinaweibo; public class mainactivity extends appcompatactivity { public static string loginuid=null; public static string loginname=null; private button login_login; private button login_find; private edittext login_uname; private edittext login_upass; private checkbox login_auto; private checkbox login_btn; private sharedpreferences sp; private imagebutton login_xinlan; private platform weibo; private dbhelper dbhelper; private sqlitedatabase sqlitedatabase; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); this.requestwindowfeature(window.feature_no_title); setcontentview(r.layout.activity_main); //获取控件 login_uname = (edittext) findviewbyid(r.id.login_uname); login_upass = (edittext) findviewbyid(r.id.login_upass); login_auto = (checkbox) findviewbyid(r.id.login_auto); //记住密码 login_btn = (checkbox) findviewbyid(r.id.login_btn); //自动登录 login_login = (button) findviewbyid(r.id.login_login); //登录 login_find = (button) findviewbyid(r.id.login_find); login_xinlan = (imagebutton) findviewbyid(r.id.login_xinlan); //调用数据库 dbhelper = new dbhelper(this,"dtb.db",null,1); sqlitedatabase = dbhelper.getwritabledatabase(); //第三方登录 weibo = sharesdk.getplatform(sinaweibo.name); login_xinlan.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { //回调信息,可以在这里获取基本的授权返回的信息,但是注意如果做提示和ui操作要传到主线程handler里去执行 weibo.setplatformactionlistener(new platformactionlistener () { @override public void oncomplete(platform platform, int i, hashmap<string, object> hashmap) { string openid = platform.getdb().getuserid(); string nickname = platform.getdb().getusername(); cursor cursor=sqlitedatabase.rawquery("select * from dtb_users where uname=?",new string[]{openid}); loginuid=cursor.getstring(cursor.getcolumnindex("uid")); if(cursor.movetonext()){ log.i("test","已经注册过!"); }else{ sqlitedatabase.execsql("insert into dtb_users(uname,upass,name,levelnumber) values('"+openid+"','null','"+nickname+"','"+1+"')"); } //跳转 mainactivity.loginname=openid; intent intent=new intent(mainactivity.this,mainmianactivity.class); startactivity(intent); } @override public void onerror(platform arg0, int arg1, throwable arg2) { // todo auto-generated method stub arg2.printstacktrace(); } @override public void oncancel(platform arg0, int arg1) { // todo auto-generated method stub } }); //authorize与showuser单独调用一个即可 weibo.authorize();//单独授权,oncomplete返回的hashmap是空的 weibo.showuser(null);//授权并获取用户信息 //移除授权 // weibo.removeaccount(true); } }); //自动登录判断 sp = this.getsharedpreferences("userinfo",0); string name=sp.getstring("user_name", ""); string pass =sp.getstring("password", ""); boolean choseremember =sp.getboolean("remember", false); boolean choseautologin =sp.getboolean("autologin", false); //如果上次选了记住密码,那进入登录页面也自动勾选记住密码,并填上用户名和密码 if(choseremember){ login_uname.settext(name); login_upass.settext(pass); login_auto.setchecked(true); } //如果上次登录选了自动登录,那进入登录页面也自动勾选自动登录 if(choseautologin){ login_btn.setchecked(true); cursor cursor= sqlitedatabase.rawquery("select * from dtb_users where uname=? and upass=?",new string[]{name,pass}); if(cursor.movetonext()){ new loginthread().start(); loginname=name; loginuid=cursor.getstring(cursor.getcolumnindex("uid")); } } // 登录监听事件 现在默认为用户名为:admin 密码:123 login_login.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { string username=login_uname.gettext().tostring(); string userpass=login_upass.gettext().tostring(); sharedpreferences.editor editor =sp.edit(); cursor cursor= sqlitedatabase.rawquery("select * from dtb_users where uname=? and upass=?",new string[]{username,userpass}); if(cursor.movetonext()){//判断是否查询到此数据 toast.maketext(mainactivity.this,"登录成功", toast.length_short).show(); loginname=username; loginuid=cursor.getstring(cursor.getcolumnindex("uid")); //是否记住密码 //记住用户名、密码、 editor.putstring("user_name", username); editor.putstring("password",userpass); if(login_auto.ischecked()){ editor.putboolean("remember", true); }else{ editor.putboolean("remember", false); } //是否自动登录 if(login_btn.ischecked()){ editor.putboolean("autologin", true); }else{ editor.putboolean("autologin", false); } editor.commit(); //跳转界面 intent intent = new intent(mainactivity.this,mainmianactivity.class); startactivity(intent); toast.maketext(mainactivity.this, "登录成功!", toast.length_short).show(); // finish(); }else{ toast.maketext(mainactivity.this,"用户名或密码错误,请重新登录", toast.length_long).show(); } } }); } public void jumpregister(view view){ intent intent=new intent(this,registeractivity.class); startactivity(intent); finish(); } //子线程 控制自动睡眠2秒钟后自动登录 class loginthread extends thread{ @override public void run() { try { sleep(2000); intent intent = new intent(mainactivity.this,mainmianactivity.class); startactivity(intent); } catch (interruptedexception e) { e.printstacktrace(); } } } }
demo下载:记住密码和自动登录
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: 胃病饮食有什么禁忌吗
下一篇: 固定的工作