Android持久化技术之SharedPreferences存储
来源:互联网 发布:mysql base64 解码 编辑:程序博客网 时间:2024/05/13 03:15
上一篇文章讲了文件存储,这篇文章准备讲一下SharedPreference存储。SharedPreference是通过键值对的方式存储数据的,使用SharedPreference来存储数据很简单,首先通过Context类中的getSharedPreferences()方法获取SharedPreferences对象,然后在通过SharedPreferences.edit()得到SharedPreferences.Editor对象,再向SharedPreferences.Editor中添加数据,最后再进行提交。下面举个例子吧。activity_main.xml中的代码如下:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <Button android:id="@+id/button" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="通过SharedPreference持久化数据" android:textAllCaps="false"/></LinearLayout>
这里添加了个按钮,用于触发通过SharedPreferences保存数据事件
MainActivity中的代码如下:
package com.tangliang.shareepreferencepersisitence;import android.content.SharedPreferences;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.Button;public class MainActivity extends AppCompatActivity { private Button button; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button = (Button) findViewById(R.id.button); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { SharedPreferences.Editor editor = getSharedPreferences("data", MODE_PRIVATE).edit(); editor.putString("name", "汤亮"); editor.putString("sex", "男"); editor.putInt("age", 21); editor.putBoolean("hasGirlFriend", false); editor.apply(); } }); }}
从SharedPreferences中读取数据更简单,只需要通过SharedPreferences对象中的一系列的get方法就可以了,下面的例子在前面的例子中添加了一些东西,用于把保存的数据读取出来并在EditText中显示出来。
activity_main.xml中的代码如下:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <Button android:id="@+id/button" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="通过SharedPreference持久化数据" android:textAllCaps="false"/> <Button android:id="@+id/restore_data" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="从SharedPreference恢复数据" android:textAllCaps="false"/> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="name:" android:textSize="25sp"/> <EditText android:id="@+id/name" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="sex:" android:textSize="25sp"/> <EditText android:id="@+id/sex" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="age:" android:textSize="25sp"/> <EditText android:id="@+id/age" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="hasGirlFriend:" android:textSize="25sp"/> <EditText android:id="@+id/hasGirlFriend" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1"/> </LinearLayout></LinearLayout>
MainActivity中的代码如下:
package com.tangliang.shareepreferencepersisitence;import android.content.SharedPreferences;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.EditText;public class MainActivity extends AppCompatActivity { private Button button; private Button restoreButton; private EditText nameText; private EditText sexText; private EditText ageText; private EditText hasGirlFriendText; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button = (Button) findViewById(R.id.button); restoreButton = (Button) findViewById(R.id.restore_data); nameText = (EditText) findViewById(R.id.name); sexText = (EditText) findViewById(R.id.sex); ageText = (EditText) findViewById(R.id.age); hasGirlFriendText = (EditText) findViewById(R.id.hasGirlFriend); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { SharedPreferences.Editor editor = getSharedPreferences("data", MODE_PRIVATE).edit(); editor.putString("name", "汤亮"); editor.putString("sex", "男"); editor.putInt("age", 21); editor.putBoolean("hasGirlFriend", false); editor.apply(); } }); restoreButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { SharedPreferences pref = getSharedPreferences("data", MODE_PRIVATE); String name = pref.getString("name", ""); String sex = pref.getString("sex", ""); int age = pref.getInt("age", 0); boolean hasGirlFriend = pref.getBoolean("hasGirlFriend", false); nameText.setText(name); sexText.setText(sex); ageText.setText("" + age); hasGirlFriendText.setText(" " + hasGirlFriend); } }); }}
阅读全文
1 0
- Android持久化技术之SharedPreferences存储
- 持久化存储技术之SharedPreferences存储
- android--持久化技术之SharedPreferences
- 持久化技术SharedPreferences存储
- 浅谈Android的三种持久化技术之SharedPreferences存储
- Android数据持久化之二:SharedPreferences 存储(上)
- Android数据持久化之二:SharedPreferences 存储(下)
- Android之数据持久化(SharedPreferences 存储)
- Android持久化之SharedPreferences
- android--持久化技术之文件存储
- Android--持久化技术之文件存储
- Android持久化技术之文件存储
- Android数据持久化存储-SharedPreferences
- #android#数据持久化--SharedPreferences存储
- 6.1 持久化技术简介 & 6.2 文件存储&6.3 SharedPreferences
- 利用SharedPreferences(持久化技术)实现密码的存储功能
- Android数据持久化之SharedPreferences
- android数据持久化之SharedPreferences
- LA 4015 树形DP 在路程不超过k的情况下,求最多可到达多少节点
- 1127: 矩阵乘积
- LeetCode | 66. Plus One
- Knight Tournament
- 2017山东省赛B题(SDUT3894打表找规律!!!!!!!!!!!!!)
- Android持久化技术之SharedPreferences存储
- oracle查询一列数据,合并成字符串,以,分割
- :=和?=
- Web常用编码以及攻击绕过笔记
- zookeeper使用和原理探究(一)
- AVL树实现(插入删除)
- 带有多个except 的try语句,处理多个异常的except语句
- 【DNA计算】DNA编码----笔记1
- 实习总结一