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

Android SharedPreferences实现记住密码和自动登录界面,第1张

概述SharedPreferences介绍:SharedPreferences是Android平台上一个轻量级的存储类,主要是保存一些常用的配置参数,它是采用xml文件存放数据的,文件存放在\"/data/data<packagename>/shared_prefs\"目录下。

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数据能被其他应用程序读写。

结果截图:



工程目录:

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

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

总结

以上是内存溢出为你收集整理的Android SharedPreferences实现记住密码和自动登录界面全部内容,希望文章能够帮你解决Android SharedPreferences实现记住密码和自动登录界面所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1146919.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存