欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android SharedPreferences实现记住密码和自动登录界面

程序员文章站 2023-12-15 17:52:34
sharedpreferences介绍: sharedpreferences是android平台上一个轻量级的存储类,主要是保存一些常用的配置参数,它是采用xml文件存放...

sharedpreferences介绍:

sharedpreferences是android平台上一个轻量级的存储类,主要是保存一些常用的配置参数,它是采用xml文件存放数据的,文件存放在"/data/data<package name>/shared_prefs"目录下。

sharedpreferences的用法:

由于sharedpreferences是一个接口,而且在这个接口里没有提供写入数据和读取数据的能力。但它是通过其editor接口中的一些方法来操作sharedpreference的,用法见下面代码:

context.getsharedpreferences(string name,int mode)来得到一个sharedpreferences实例

name:是指文件名称,不需要加后缀.xml,系统会自动为我们添加上。

mode:是指定读写方式,其值有三种,分别为:

context.mode_private:指定该sharedpreferences数据只能被本应用程序读、写

context.mode_world_readable:指定该sharedpreferences数据能被其他应用程序读,但不能写

context.mode_world_writeable:指定该sharedpreferences数据能被其他应用程序读写。

结果截图:

Android SharedPreferences实现记住密码和自动登录界面

Android SharedPreferences实现记住密码和自动登录界面

Android SharedPreferences实现记住密码和自动登录界面

工程目录:

Android SharedPreferences实现记住密码和自动登录界面

java代码

loginactivity.java

package com.liu.activity; 
 
import android.app.activity; 
import android.app.backup.sharedpreferencesbackuphelper; 
import android.content.context; 
import android.content.intent; 
import android.content.sharedpreferences; 
import android.content.sharedpreferences.editor; 
import android.os.bundle; 
import android.text.spannable; 
import android.view.view; 
import android.view.view.onclicklistener; 
import android.view.window; 
import android.widget.button; 
import android.widget.checkbox; 
import android.widget.compoundbutton; 
import android.widget.compoundbutton.oncheckedchangelistener; 
import android.widget.edittext; 
import android.widget.imagebutton; 
import android.widget.toast; 
 
public class loginactivity extends activity { 
 
 private edittext username, password; 
 private checkbox rem_pw, auto_login; 
 private button btn_login; 
 private imagebutton btnquit; 
 private string usernamevalue,passwordvalue; 
 private sharedpreferences sp; 
 
 public void oncreate(bundle savedinstancestate) { 
 super.oncreate(savedinstancestate); 
  
 //去除标题 
 this.requestwindowfeature(window.feature_no_title); 
 setcontentview(r.layout.login); 
  
 //获得实例对象 
 sp = this.getsharedpreferences("userinfo", context.mode_world_readable); 
 username = (edittext) findviewbyid(r.id.et_zh); 
 password = (edittext) findviewbyid(r.id.et_mima); 
 rem_pw = (checkbox) findviewbyid(r.id.cb_mima); 
 auto_login = (checkbox) findviewbyid(r.id.cb_auto); 
 btn_login = (button) findviewbyid(r.id.btn_login); 
 btnquit = (imagebutton)findviewbyid(r.id.img_btn); 
  
  
 //判断记住密码多选框的状态 
 if(sp.getboolean("ischeck", false)) 
 { 
  //设置默认是记录密码状态 
  rem_pw.setchecked(true); 
  username.settext(sp.getstring("user_name", "")); 
  password.settext(sp.getstring("password", "")); 
  //判断自动登陆多选框状态 
  if(sp.getboolean("auto_ischeck", false)) 
  { 
   //设置默认是自动登录状态 
   auto_login.setchecked(true); 
  //跳转界面 
  intent intent = new intent(loginactivity.this,logoactivity.class); 
  loginactivity.this.startactivity(intent); 
   
  } 
 } 
  
 // 登录监听事件 现在默认为用户名为:liu 密码:123 
 btn_login.setonclicklistener(new onclicklistener() { 
 
  public void onclick(view v) { 
  usernamevalue = username.gettext().tostring(); 
  passwordvalue = password.gettext().tostring(); 
   
  if(usernamevalue.equals("liu")&&passwordvalue.equals("123")) 
  { 
   toast.maketext(loginactivity.this,"登录成功", toast.length_short).show(); 
   //登录成功和记住密码框为选中状态才保存用户信息 
   if(rem_pw.ischecked()) 
   { 
   //记住用户名、密码、 
   editor editor = sp.edit(); 
   editor.putstring("user_name", usernamevalue); 
   editor.putstring("password",passwordvalue); 
   editor.commit(); 
   } 
   //跳转界面 
   intent intent = new intent(loginactivity.this,logoactivity.class); 
   loginactivity.this.startactivity(intent); 
   //finish(); 
   
  }else{ 
   
   toast.maketext(loginactivity.this,"用户名或密码错误,请重新登录", toast.length_long).show(); 
  } 
   
  } 
 }); 
 
 //监听记住密码多选框按钮事件 
 rem_pw.setoncheckedchangelistener(new oncheckedchangelistener() { 
  public void oncheckedchanged(compoundbutton buttonview,boolean ischecked) { 
  if (rem_pw.ischecked()) { 
   
   system.out.println("记住密码已选中"); 
   sp.edit().putboolean("ischeck", true).commit(); 
   
  }else { 
   
   system.out.println("记住密码没有选中"); 
   sp.edit().putboolean("ischeck", false).commit(); 
   
  } 
 
  } 
 }); 
  
 //监听自动登录多选框事件 
 auto_login.setoncheckedchangelistener(new oncheckedchangelistener() { 
  public void oncheckedchanged(compoundbutton buttonview,boolean ischecked) { 
  if (auto_login.ischecked()) { 
   system.out.println("自动登录已选中"); 
   sp.edit().putboolean("auto_ischeck", true).commit(); 
 
  } else { 
   system.out.println("自动登录没有选中"); 
   sp.edit().putboolean("auto_ischeck", false).commit(); 
  } 
  } 
 }); 
  
 btnquit.setonclicklistener(new onclicklistener() { 
  
  @override 
  public void onclick(view v) { 
  finish(); 
  } 
 }); 
 
 } 
}

logoactivity.java

package com.liu.activity; 
import android.app.activity; 
import android.content.intent; 
import android.content.sharedpreferences; 
import android.content.sharedpreferences.editor; 
import android.opengl.etc1; 
import android.os.bundle; 
import android.view.view; 
import android.view.view.onclicklistener; 
import android.view.window; 
import android.view.animation.alphaanimation; 
import android.view.animation.animation; 
import android.view.animation.animation.animationlistener; 
import android.widget.button; 
import android.widget.imagebutton; 
import android.widget.imageview; 
import android.widget.progressbar; 
 
public class logoactivity extends activity { 
 private progressbar progressbar; 
 private button backbutton; 
 
 protected void oncreate(bundle savedinstancestate) { 
 super.oncreate(savedinstancestate); 
 // 去除标题 
 this.requestwindowfeature(window.feature_no_title); 
 setcontentview(r.layout.logo); 
 
 progressbar = (progressbar) findviewbyid(r.id.pgbar); 
 backbutton = (button) findviewbyid(r.id.btn_back); 
 
 intent intent = new intent(this, welcomeavtivity.class); 
 logoactivity.this.startactivity(intent); 
 
 backbutton.setonclicklistener(new onclicklistener() { 
 
  @override 
  public void onclick(view v) { 
  finish(); 
 
  } 
 }); 
 
 } 
 
} 

welcomeactivity.java

package com.liu.activity;
import android.app.activity; 
import android.os.bundle; 
 
public class welcomeavtivity extends activity { 
 
 @override 
 protected void oncreate(bundle savedinstancestate) { 
 // todo auto-generated method stub 
 super.oncreate(savedinstancestate); 
 setcontentview(r.layout.welcome); 
 } 
 
}

布局文件:

login.xml文件

<?xml version="1.0" encoding="utf-8"?> 
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" 
 android:layout_width="fill_parent" 
 android:layout_height="fill_parent" 
 android:background="@drawable/logo_bg" 
 android:orientation="vertical" > 
 
 <relativelayout 
 android:layout_width="fill_parent" 
 android:layout_height="wrap_content" > 
 <imagebutton 
  android:id="@+id/img_btn" 
  android:layout_width="wrap_content" 
  android:layout_height="wrap_content" 
  android:layout_alignparentright="true" 
  android:background="@drawable/quit"/> 
 
 <textview 
  android:id="@+id/tv_zh" 
  android:layout_width="wrap_content" 
  android:layout_height="35dip" 
  android:layout_marginleft="12dip" 
  android:layout_margintop="10dip" 
  android:gravity="bottom" 
  android:text="帐号:" 
  android:textcolor="#000000" 
  android:textsize="18sp" /> 
 
 <edittext 
  android:id="@+id/et_zh" 
  android:layout_width="fill_parent" 
  android:layout_height="40dip" 
  android:layout_below="@id/tv_zh" 
  android:layout_marginleft="12dip" 
  android:layout_marginright="10dip" /> 
 
 <textview 
  android:id="@+id/tv_mima" 
  android:layout_width="wrap_content" 
  android:layout_height="35dip" 
  android:layout_below="@id/et_zh" 
  android:layout_marginleft="12dip" 
  android:layout_margintop="10dip" 
  android:gravity="bottom" 
  android:text="密码:" 
  android:textcolor="#000000" 
  android:textsize="18sp" /> 
 
 <edittext 
  android:id="@+id/et_mima" 
  android:layout_width="fill_parent" 
  android:layout_height="40dip" 
  android:layout_below="@id/tv_mima" 
  android:layout_marginleft="12dip" 
  android:layout_marginright="10dip" 
  android:maxlines="200" 
  android:password="true" 
  android:scrollhorizontally="true" /> 
 
 <checkbox 
  android:id="@+id/cb_mima" 
  android:layout_width="wrap_content" 
  android:layout_height="wrap_content" 
  android:layout_below="@id/et_mima" 
  android:layout_marginleft="12dip" 
  android:text="记住密码" 
  android:textcolor="#000000" /> 
 
 <checkbox 
  android:id="@+id/cb_auto" 
  android:layout_width="wrap_content" 
  android:layout_height="wrap_content" 
  android:layout_below="@id/cb_mima" 
  android:layout_marginleft="12dip" 
  android:text="自动登录" 
  android:textcolor="#000000" /> 
 <button 
  android:id="@+id/btn_login" 
  android:layout_width="80dip" 
  android:layout_height="40dip" 
  android:layout_below="@id/et_mima" 
  android:layout_alignparentright="true" 
  android:layout_aligntop="@id/cb_auto" 
  android:layout_marginright="10dip" 
  android:gravity="center" 
  android:text="登录" 
  android:textcolor="#000000" 
  android:textsize="18sp"/> 
 
  
 </relativelayout> 
 
 
 
</linearlayout> 

logo.xml文件

<?xml version="1.0" encoding="utf-8"?> 
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" 
 android:layout_width="fill_parent" 
 android:layout_height="fill_parent" 
 android:background="@drawable/logo_bg" 
 android:orientation="vertical" > 
 
 <relativelayout 
 android:layout_width="fill_parent" 
 android:layout_height="wrap_content" 
 android:layout_weight="3"> 
 
 <progressbar 
  android:id="@+id/pgbar" 
  android:layout_width="wrap_content" 
  android:layout_height="wrap_content" 
  android:layout_centerinparent="true" /> 
 
 <textview 
  android:id="@+id/tv1" 
  android:layout_width="wrap_content" 
  android:layout_height="wrap_content" 
  android:layout_below="@id/pgbar" 
  android:layout_centerhorizontal="true" 
  android:text="正在登录..." 
  android:textcolor="#000000" 
  android:textsize="18sp" /> 
 </relativelayout> 
 
 <linearlayout 
 android:layout_width="fill_parent" 
 android:layout_height="wrap_content" 
 android:layout_weight="1" 
 android:gravity="center" 
 android:orientation="vertical" > 
 
 <button 
  android:id="@+id/btn_back" 
  android:layout_width="70dip" 
  android:layout_height="35dip" 
  android:text="取消" 
  android:textcolor="#000000" 
  android:textsize="12sp" /> 
 </linearlayout> 
 
 
</linearlayout> 

welcome.xml

<?xml version="1.0" encoding="utf-8"?> 
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" 
 android:layout_width="fill_parent" 
 android:layout_height="fill_parent" 
 android:layout_gravity="center" 
 android:background="@drawable/login_bg" 
 android:orientation="vertical" > 
 
 <textview 
 android:layout_width="fill_parent" 
 android:layout_height="wrap_content" 
 android:gravity="center" 
 android:text="登陆成功,进入用户界面" 
 android:textcolor="#000000" 
 android:textsize="20sp" /> 
 
</linearlayout>

工程下载连接:android-sharedpreferences_jb51.rar

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

上一篇:

下一篇: