android sharedpreferences简单使用,Android 中 SharedPreferences 的简单用法试例

SharedPreferences 是一个轻量级的存储类,主要是保存一些小的数据,一些状态信息

实现:

1、确定要保存的是什么数据后,写个 SharedPreferences  存储类

2、获取到数据后,调用 SharedPreferences  存储类将数据保存

3、调用 SharedPreferences  存储类将数据取出,并展示出来

效果:

获取数据保存

2b0c54c7e4c9369f3db434624699cd79.png

获取数据展示

d5c99624d74d00f5f44ccddec3baf536.png

实现详情:

1、确定要保存的是什么数据后,写个 SharedPreferences  存储类

我的构思是将注册时的信息保存在 SharedPreferences  中,在其他地方还需要用到

注册信息有 :账号、密码、性别、年龄

如果还有其他数据需要保存,也只要根据需求添加 get、set 方法就好了import android.content.Context;

import android.content.SharedPreferences;

/**

* Created by Administrator on 2018/3/22 0022.

* 保存一些简单的数据

*/

public class MySharedPreferences {

//创建一个SharedPreferences    类似于创建一个数据库,库名为 data

public static SharedPreferences share(Context context){

SharedPreferences sharedPreferences = context.getSharedPreferences("date", Context.MODE_PRIVATE);

return sharedPreferences;

}

//name 账号

//调用上面的 share(Context context) 方法 获取标识为 "name" 的数据

public static Object getName(Context context){

return share(context).getString("name",null);

}

//调用上面的 share(Context context) 方法 将数据存入,并标识为 "name"

//使用 commit() 方法保存会给出反应(保存成功或失败)

public static boolean setName(String name, Context context){

SharedPreferences.Editor e = share(context).edit();

e.putString("name",name);

Boolean bool = e.commit();

return bool;

}

//pswd 密码

public static String getPswd(Context context){

return share(context).getString("pswd",null);

}

//这里使用的是 apply() 方法保存,将不会有返回值

public static void setPswd(String pswd, Context context){

SharedPreferences.Editor e = share(context).edit();

e.putString("pswd",pswd);

e.apply();

}

/**

* 可以根据需求选择用那种方式保存数据

* (需不需要告诉你有没有保存成功)

*/

//sex 性别

public static String getSex(Context context){

return share(context).getString("sex",null);

}

public static void setSex(String sex, Context context){

SharedPreferences.Editor e = share(context).edit();

e.putString("sex",sex);

e.apply();

}

//age 年龄

public static String getAge(Context context){

return share(context).getString("age",null);

}

public static void setAge(String age, Context context){

SharedPreferences.Editor e = share(context).edit();

e.putString("age",age);

e.apply();

}

}

从上面可以看到,我创建了一个 MySharedPreferences  类,

在类中创建了SharedPreferences  后,为每个需要存储的都写了 set 、get 方法

通过 set 、 get方法来 保存和获取 需要的数据。

同时,我的试例中有两种保存方法: commit() 和 apply()

commit() 方法会返回保存状态(成功返回 true,失败返回 false)

apply() 方法则没有任何返回

可以根据需要自由使用

2、获取到数据后,调用 SharedPreferences  存储类将数据保存

第二步分为获取数据和保存数据

获取数据:

我这里是通过在 EditText 输入获取数据,其实数据来源可以根据自己需求来<?xml  version="1.0" encoding="utf-8"?>

xmlns:tools="http://schemas.android.com/tools"

android:id="@+id/activity_main"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:paddingBottom="@dimen/activity_vertical_margin"

android:paddingLeft="@dimen/activity_horizontal_margin"

android:paddingRight="@dimen/activity_horizontal_margin"

android:paddingTop="@dimen/activity_vertical_margin"

android:orientation="vertical"

tools:context="com.win.sharedprefences.MainActivity">

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:textSize="20dp"

android:textColor="#000000"

android:text="保存数据"/>

android:id="@+id/name"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:hint="账号:"/>

android:id="@+id/pswd"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:hint="密码:"/>

android:id="@+id/sex"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:hint="性别:"/>

android:id="@+id/age"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:hint="年龄:"/>

android:id="@+id/submit"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="提交保存"/>

这个界面就是上面展示的“获取数据保存”界面

保存数据:

通过用户输入,我们获取到了将要保存起来的数据,接下来我们把它们保存起来

第一步在 MySharedPreferences类 定义了保存数据的 set方法,调用就好了//点击事件(获取数据,存入SharedPreferences)

submit.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View view) {

//获取输入框的内容

String names = name.getText().toString();

String pswds = pswd.getText().toString();

String sexs = sex.getText().toString();

String ages = age.getText().toString();

//在 MySharedPreferences类里定义好存取方法后,就可以调用了

//这里将数据保存进去  注意:(name 我是定义了有返回值的,试试看)

Boolean bool = MySharedPreferences.setName(names, MainActivity.this);

MySharedPreferences.setPswd(pswds, MainActivity.this);

MySharedPreferences.setSex(sexs,   MainActivity.this);

MySharedPreferences.setAge(ages,   MainActivity.this);

//看看保存成功没

if(bool)

Toast.makeText(MainActivity.this, "保存成功!", Toast.LENGTH_SHORT).show();

else

Toast.makeText(MainActivity.this, "保存失败!", Toast.LENGTH_SHORT).show();

//跳转到展示界面

startActivity(new Intent(MainActivity.this, Main2Activity.class));

}

});

这里就展示核心就好了

3、调用 SharedPreferences  存储类将数据取出,并展示出来

从第二步的代码中可以看到,当点击了保存数据按钮后,就会跳到展示界面去

展示界面就是上面效果中的“获取数据展示”所示<?xml  version="1.0" encoding="utf-8"?>

xmlns:tools="http://schemas.android.com/tools"

android:id="@+id/activity_main2"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:paddingBottom="@dimen/activity_vertical_margin"

android:paddingLeft="@dimen/activity_horizontal_margin"

android:paddingRight="@dimen/activity_horizontal_margin"

android:paddingTop="@dimen/activity_vertical_margin"

android:orientation="vertical"

tools:context="com.win.sharedprefences.Main2Activity">

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:textSize="20dp"

android:textColor="#000000"

android:text="使用数据"/>

android:id="@+id/obtain"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="获取数据"/>

android:id="@+id/one"

android:layout_width="match_parent"

android:layout_height="wrap_content" />

android:id="@+id/two"

android:layout_width="match_parent"

android:layout_height="wrap_content" />

android:id="@+id/three"

android:layout_width="match_parent"

android:layout_height="wrap_content" />

android:id="@+id/four"

android:layout_width="match_parent"

android:layout_height="wrap_content" />

布局大家应该都会

我的定义是,点击展示按钮就获取数据,将数据展示在 TextView 中

获取数就调用 MySharedPreferences类中定义好的 get 方法//获取保存好的数据并展示出来

obtain.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View view) {

//通过MySharedPreferences类里定义好的存取方法,获取保存在里面的数据

String names = (String) MySharedPreferences.getName(Main2Activity.this);

String pswds = (String) MySharedPreferences.getPswd(Main2Activity.this);

String sexs =  (String)  MySharedPreferences.getSex(Main2Activity.this);

String ages =  (String)  MySharedPreferences.getAge(Main2Activity.this);

//将数据放到 TextView 展示出来

one.setText("账号:" + names);

two.setText("密码:" + pswds);

three.setText("性别:" + sexs);

four.setText("年龄:" + ages);

}

});

注释: SharedPreferences 的使用可以根据自己需求灵活运用,

里面保存的值需要改变的时候,在调用一下 set 方法就行了

同样的标识会被覆盖掉

源码:https://github.com/iscopy/SharedPreferences

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值