当前位置: 移动技术网 > 移动技术>移动开发>Android > android中SharedPreferences实现存储用户名功能

android中SharedPreferences实现存储用户名功能

2019年07月24日  | 移动技术网移动技术  | 我要评论

1. 简介

sharedpreferences是一种轻型的数据存储方式,通过key-value键值对的方式将数据存储在xml文件中,常用于存储简单的配置信息。

2. 使用方式

2.1 获取sharedpreferences对象

android中可通过以下三种方式获取sharedpreferences对象:

2.2.1 context类中的getsharedpreferences()

接收两个参数,第一个参数指定存储数据的文件,若指定文件不存在,则新建该文件,存放目录为"/data/data/package_name/shared_prefs/",其中package_name为包名。

第二个参数则为操作模式,主要有两种:

mode_private:私有模式,默认情况下的模式,与直接传入0作为参数效果一样,表示只有当前程序可对这个文件进行操作。

mode_multi_process:多进程模式,允许多个进程对该文件进行操作。

2.2.2 activity类中的getpreferences()

这个方法与上一个方法比较相似,不同之处在于它只接收一个参数,用于指定操作模式,而无需指定文件名,这个方法默认将当前activity的类名作为存储数据的文件名。

2.2.3 preferencemanager类中的getdefaultsharedpreferences()

这是一个静态方法,接收一个context参数,使用当前应用程序的包名作为存储数据的文件名。

2.2 获取sharedpreferences.editor对象

sharedpreferences对象本身是只可以读取而不能保存数据的,需要保存数据则要调用sharedpreferences对象的edit()方法获取一个editor对象。

2.3 通过putxxx方法存储数据

得到editor对象后,则可调用它的putxxx方法添加数据,这里的xxx指的是添加的数据类型,例如存储字符串数据则调用putstring()方法。这个方法接收两个参数,第一个参数为key值,第二个参数为数据的值,即一个键值对。

2.4 提交变化

添加或移除(remove方法)数据后,需要调用editor对象的commit()方法将所作变化提交。

2.5 获取存储的数据

获取已经存储的数据较为简单,直接调用sharedpreferences对象的getxxx方法即可,使用方法与editor对象的putxxx类似。这个方法也是接收两个参数,第一个参数指定要获取的数据的key值,第二个参数指定当获取的数据不存在时所返回的默认值。

3. 范例-实现保存用户名的功能

布局:

<?xml version="1.0" encoding="utf-8"?>
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:gravity="center_horizontal"
 android:orientation="vertical"
 tools:context="com.studying.myapplication.mainactivity">

 <!--用户名-->
 <linearlayout
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:orientation="horizontal">

  <textview
   android:layout_width="0dp"
   android:layout_height="wrap_content"
   android:layout_weight="1"
   android:gravity="center"
   android:text="用户名" />

  <edittext
   android:id="@+id/username"
   android:layout_width="0dp"
   android:layout_height="wrap_content"
   android:layout_weight="4" />
 </linearlayout>

 <!--密码-->
 <linearlayout
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:orientation="horizontal">

  <textview
   android:layout_width="0dp"
   android:layout_height="wrap_content"
   android:layout_weight="1"
   android:gravity="center"
   android:text="密码" />

  <edittext
   android:id="@+id/passward"
   android:layout_width="0dp"
   android:layout_height="wrap_content"
   android:layout_weight="4"
   android:inputtype="textpassword" />
 </linearlayout>

 <!--是否记住用户名-->
 <checkbox
  android:id="@+id/remember"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:checked="false"
  android:text="记住用户名" />

 <!--登录-->
 <button
  android:id="@+id/login"
  android:layout_width="200dp"
  android:layout_height="35dp"
  android:text="登录"
  android:textsize="12sp" />

</linearlayout>

活动类:

public class mainactivity extends activity implements view.onclicklistener {

 private sharedpreferences mpref;
 private sharedpreferences.editor meditor;
 private edittext musername;
 private edittext mpassword;
 private checkbox misremember;
 private button mlogin;

 @override
 protected void oncreate(bundle savedinstancestate) {
  super.oncreate(savedinstancestate);
  setcontentview(r.layout.activity_main);

  init();
 }

 private void init() {
  musername = (edittext) findviewbyid(r.id.username);
  mpassword = (edittext) findviewbyid(r.id.passward);
  misremember = (checkbox) findviewbyid(r.id.remember);
  mlogin = (button) findviewbyid(r.id.login);
  mlogin.setonclicklistener(this);

  mpref = getsharedpreferences("user_data", mode_private);
  meditor = mpref.edit();

  //若之前曾设置过记住用户名,则读取并设置用户名
  if (mpref.getboolean("is_remember", false)) {
   musername.settext(mpref.getstring("user_name", ""));
  }
 }

 @override
 public void onclick(view v) {
  switch (v.getid()) {
   case r.id.login:
    string username = musername.gettext().tostring().trim();
    string password = mpassword.gettext().tostring().trim();
    //测试用账号
    if ("admin".equals(username) && "123456".equals(password)) {
     toast.maketext(this, "登录成功!", toast.length_short).show();
     //若勾选了记住用户名,则保存数据
     if (misremember.ischecked()) {
      meditor.putstring("user_name", username);
      meditor.putboolean("is_remember", true);
      meditor.commit();
     }
    } else {
     toast.maketext(this, "用户名或密码错误!", toast.length_short).show();
    }
    break;
  }
 }
}

本文作学习交流用,如有错误,欢迎指正!希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网