安卓之SharedPreferences存储
SharedPreferences存储
首先关于安卓存储我们在这里介绍一下:
持久化技术介绍:
数据持久化就是指将那些内存中瞬间数据保存到存储设备中,保证即使手机或电脑关机的情况下,这些数据仍然不会丢失。保存在内存中的数据是处于瞬时状态的,而保存在存储设备中的数据是处于持久状态的,持久化技术则提供了一种机制可以让数据在瞬间状态和持久状态之间进行转换。(引用第一行代码中的讲解)
SharedPreferences存储
它是一种轻量级的数据存储方式,是使用键值对的方式来存储数据的。通俗的说,当保存一条数据的时候,需要给这条数据提供一个对应的键,这样在读取数据的时候就可以通过这个键把相应的值取出来。类似与bundle。SharedPreferences支持基本数据类型存储。
(1) 首先需要得到SharedPreferences对象:
getSharedPreferences(String name,int mode)
第一个参数用于指定SharedPreferences文件的名称,第二个参数用于指定访问权限(也就是说用于指定操作模式),目前只有MODE_PRIVATE(表示只能被本应用程序读和写,其中写入的内容会覆盖原文件的内容)这个可选,它默认的操作模式。其他的几种操作模式都已经废除了,在这里我就不介绍了。
(2)调用SharedPreferences对象来获取SharePreferences对象:
SharedPreferences.Editor editor=getSharedPreferences(String name,int mode).edit();
(3)向SharedPreferences.Editor对象中添加数据,比如添加一个字符串型数据就使用putString(),其他的和这个类似。
(4)调用commit()方法提交数据,从而完成数据存储操作。(调用apply()方法也可以)。
我感觉不如来个案例了解的更透彻:
首先我们创建一个安卓项目(具体如何创建我就在这里不在介绍了)。
在activity_main.xml中编写如下代码:
<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"tools:context=".MainActivity"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="应户名:"android:textSize="20sp"/><EditTextandroid:id="@+id/username"android:layout_width="0dip"android:layout_height="wrap_content"android:layout_weight="1"android:inputType="text"android:textSize="20sp"android:layout_marginTop="20sp"><requestFocus/></EditText></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="密 码:"android:textSize="20sp"/><EditTextandroid:id="@+id/password"android:layout_width="0dip"android:layout_height="wrap_content"android:layout_weight="1"android:textSize="20sp"/></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"><CheckBoxandroid:id="@+id/rember_pass"android:layout_width="wrap_content"android:layout_height="wrap_content"android:textSize="18sp"android:text="Remember password"/></LinearLayout><Buttonandroid:id="@+id/login"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="Login"/></LinearLayout>
这是我们创建一个登录界面:
然后在MainActivity中编写如下代码:
public class MainActivity extends AppCompatActivity {private SharedPreferences pref;private SharedPreferences.Editor editor;private EditText username;private EditText passwordEdit;private Button login;private CheckBox rememberPass;private boolean isRemember=false;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);supportRequestWindowFeature(Window.FEATURE_NO_TITLE);setContentView(R.layout.activity_main);pref= getSharedPreferences("mode",MODE_PRIVATE);username=(EditText)findViewById(R.id.username);passwordEdit=(EditText)findViewById(R.id.password);rememberPass=(CheckBox)findViewById(R.id.rember_pass);login=(Button)findViewById(R.id.login);login.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {String accout1=username.getText().toString();String password=passwordEdit.getText().toString();//如果账号是name且密码是123,就认为登录成功if (accout1.equals("name")&&password.equals("123")){editor=pref.edit();if (rememberPass.isChecked()){//检查复选框是否被选中editor.putBoolean("remember_password",true);editor.putString("account",accout1);editor.putString("password",password);}else {editor.putBoolean("remember_password",false);editor.putString("account",accout1);editor.putString("password",password);}editor.commit();Intent intent=new Intent(MainActivity.this,Main2Activity.class);startActivity(intent);}else {Toast.makeText(MainActivity.this,"应户名或密码不对",Toast.LENGTH_SHORT).show();}}});}
}
然后在activity_main2.xml中编写如下代码:
<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:orientation="vertical"tools:context=".Main2Activity"><TextViewandroid:id="@+id/username"android:layout_width="wrap_content"android:layout_height="wrap_content"android:textSize="20sp"/><TextViewandroid:id="@+id/password"android:layout_width="wrap_content"android:layout_height="wrap_content"android:textSize="20sp"/></LinearLayout>
用来显示从第一个activity中传来的数据。
在Main2Activity中编写如下代码:
public class Main2Activity extends AppCompatActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main2);TextView usernameTV=(TextView)findViewById(R.id.username);TextView passwordTV=(TextView)findViewById(R.id.password);if (NavUtils.getParentActivityName(Main2Activity.this)!=null){getSupportActionBar().setDisplayHomeAsUpEnabled(true);}SharedPreferences preferences=getSharedPreferences("mode",MODE_PRIVATE);boolean isRemember=preferences.getBoolean("remember_password",false);if (isRemember){String usename=preferences.getString("account","");String password=preferences.getString("password","");usernameTV.setText(usename);passwordTV.setText(password);}else {String usename=preferences.getString("account","");usernameTV.setText(usename);}}
}
到这里这个案例就做完了,你可以运行试一试看看效果。
*当你输入应户名和密码时,不点击复选按钮时,单击按钮时只会在第二个界面出现用户名,当点击复选按钮时就会在第二个界面出现用户名和密码。
希望这篇文章可以帮助你更好的学习安卓SharePreferences存储。
安卓之SharedPreferences存储相关推荐
- android: SharedPreferences存储
不同于文件的存储方式,SharedPreferences 是使用键值对的方式来存储数据的.也就是 说当保存一条数据的时候,需要给这条数据提供一个对应的键,这样在读取数据的时候就可 以通过这个键把相应的 ...
- Android-02:使用SharedPreferences存储简单数据
Android有许多数据存储的方式: 1 使用SharedPreferences存储数据 2 文件存储数据 3 SQLite数据库存储数据 4 使用ContentProvider存储数据 5 网络存储 ...
- Android入门(九)文件存储与SharedPreferences存储
原文链接:http://www.orlion.ga/578/ Android系统中主要提供了三种方式用于简单地实现数据持久化功能,即文件存储.SharedPreference存储以及数据库存储.当然, ...
- 安卓存储性能测试软件,安卓版PCMark存储性能测试:魅族Pro 5/Pro 6都逆袭S7 edge了...
拼 命 加 载 中 ... PC性能测试中,Futuremark的3DMark.PCMark占据着统治性地位,是最常用的基准测试软件之一.在移动平台上,他们的3DMark及PCMark来得晚了点,前者 ...
- Android——数据存储(课堂代码整理:SharedPreferences存储和手机内部文件存储)...
layout文件: 1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmln ...
- Android之SharedPreferences 存储复杂对象
用SharedPreferences存储一些常用的设置信息很方便,但是我们知道SharedPreferences只能存储一些简单的类型 如何用SharedPreferences来存储一些复杂的对象?下 ...
- 使用SharedPreferences存储和读取数据
转:http://www.worlduc.com/blog2012.aspx?bid=19403392 1.任务目标 (1)掌握Android中SharedPreferences的使用方法. 2.任务 ...
- android qq存储方式,android(5)(模拟QQ登录,文件存储,SD卡存储,SharedPreferences存储)...
模拟QQ登录(记录账号和密码): 1.文件存储到文件中: /** * 文件存储 *@author Administrator * */ public class Utils { /** * 保存用户信 ...
- android SharedPreferences 存储对象
原文地址为: android SharedPreferences 存储对象 我们知道SharedPreferences只能保存简单类型的数据,例如,String.int等. 如果想用SharedPre ...
最新文章
- Mybatis系列(三):Mybatis实现关联表查询
- 2、JDBC连接数据库
- easyUI的中文乱码问题
- Uva12716 素数筛思想的应用
- 在Linux上安装CHM查看工具
- Oracle数据库中闪回恢复的详细分析
- 德勤发布《 2020 亚太四大半导体市场的崛起》报告,美国收入占比达到47%,中国大陆仅占 5%
- 哈啰出行完成新一轮2.8亿美元融资 阿里首次入股
- 最近有啥ML比赛能表现自己的优秀?CVPR2018图像压缩大赛
- Windows下的 Redis 安装教程
- 微信 JS-SDK 说明文档
- 入门SAP MM的学习流程
- C++ boost 解析 Json
- 查询计算机系男生修了,sql练习参考答案
- 计算机专业能当体育老师吗,非师范类专业也能当老师的4个专业,录取率比其它专业还高...
- NVIDIA Jetson TX2 解决奥比中光 Astra pro相机的ros 打不开深度信息/camera/depth/image
- iso管理体系认证条件
- “笨办法”学Python3,Zed A. Shaw,习题15
- 安装包免费下载(持续更新ing…)
- 炎龙骑士团-黄金城之迷剧情