1、SharedPreferences存储

在文章Android持久化之文件的读取与写入中,介绍了Android持久化技术的文件的读取与写入。在本文中,继续介绍Android持久化技术另外一个SharedPreferences存储。

(1)SharedPreferences存储方式是基于key-value的,通过key可以找到对应的value。

(2)支持多种数据类型存储,比如字符串、整形、布尔型等,并有对应的存储与获取方法。

(3)获取SharedPreferences对象有多种方式。

使用Context类的getSharedPreferences方法。

使用Activity类的getPreferences方法

使用PreferenceManager类的getDefaultSharedPreferences方法

(4)当存储时,需要通过SharedPreferences对象获取SharedPreferences.Editor对象

(5)默认存储路径为:/data/data/包名/shared_prefs/目录

(6)存储文件类型为xml文件

2、示例

场景:点击保存按钮,存储数据;点击恢复按钮,恢复数据。

(1)activity_main.xml

<?xml version="1.0" encoding="utf-8"?><TableLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"     android:stretchColumns="1"    >    <TableRow        android:id="@+id/tableRow1"        android:layout_width="wrap_content"        android:layout_height="wrap_content" >        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="Account:" />        <EditText            android:id="@+id/account"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:hint="Input your account here"            android:ems="10" >        </EditText>    </TableRow>   <TableRow        android:id="@+id/tableRow2"        android:layout_width="wrap_content"        android:layout_height="wrap_content" >        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="Password:"                        />        <EditText            android:id="@+id/password"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:ems="10"             android:inputType="textPassword"            >        </EditText>    </TableRow>        <TableRow        android:id="@+id/tableRow3"        android:layout_width="wrap_content"        android:layout_height="wrap_content" >       <Button        android:id="@+id/login"android:layout_span="2"        android:layout_height="wrap_content"        android:text="save data" />    </TableRow><TextView     android:layout_width="wrap_content"     android:layout_height="20dp"    android:background="#ff0000"    android:text="我是万恶的分割线"    android:textSize="20sp"    android:gravity="center"    />     <TableRow        android:id="@+id/tableRow4"        android:layout_width="wrap_content"        android:layout_height="wrap_content" >        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="Account:" />        <EditText            android:id="@+id/account2"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:ems="10" >        </EditText>    </TableRow>   <TableRow        android:id="@+id/tableRow5"        android:layout_width="wrap_content"        android:layout_height="wrap_content" >        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="Password:"                        />        <EditText            android:id="@+id/password2"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:ems="10"             android:inputType="textPassword"            >        </EditText>    </TableRow>        <TableRow        android:id="@+id/tableRow6"        android:layout_width="wrap_content"        android:layout_height="wrap_content" >       <Button        android:id="@+id/login2"android:layout_span="2"        android:layout_height="wrap_content"        android:text="restore data" />    </TableRow>        </TableLayout>
(2)MainActivity.java

package com.example.testsharedpreferences;import android.app.Activity;import android.content.SharedPreferences;import android.os.Bundle;import android.view.Menu;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.EditText;import android.widget.Toast;/** * Android 持久化技术-----SharedPreferences存储 * @author yy * */public class MainActivity extends Activity {private EditText accountEdit;private EditText passwordEdit;private Button saveButton;private Button restoreButton;private SharedPreferences pref;private SharedPreferences.Editor editor;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);//存储按钮saveButton = (Button) findViewById(R.id.login);//为存储按钮添加点击事件saveButton.setOnClickListener(new OnClickListener() {@Override    public void onClick(View arg0) {//获取SharedPreferences对象//第一个参数:文件名,没有则新建。第二个参数:写入模式-覆盖pref = getSharedPreferences("second", MODE_PRIVATE);//获取SharedPreferences.Editor对象editor = pref.edit();//获取输入的账号内容accountEdit = (EditText) findViewById(R.id.account);String account = accountEdit.getText().toString();//获取输入的密码内容passwordEdit = (EditText) findViewById(R.id.password);String password = passwordEdit.getText().toString();//存储用户名和密码editor.putString("account", account);editor.putString("password", password);//提交editor.commit();Toast.makeText(getApplicationContext(), "保存成功", Toast.LENGTH_SHORT).show();}});//获取恢复按钮对象restoreButton = (Button) findViewById(R.id.login2);//添加事件restoreButton.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View arg0) {//获取SharedPreference对象pref = getSharedPreferences("second", MODE_PRIVATE);//读取内容String account = pref.getString("account", "this is default value");String password = pref.getString("password", "this is default value");//设置到响应位置EditText editText2 = (EditText)findViewById(R.id.account2);editText2.setText(account);EditText passwordText2 = (EditText) findViewById(R.id.password2);passwordText2.setText(password);Toast.makeText(getApplicationContext(), "恢复成功", Toast.LENGTH_SHORT).show();}});}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);return true;}}
3、结果

输入内容后,当点击“save data”按钮后,存储文件为second.xml,如下:

对应内容:

下面是效果图:

Android持久化之SharedPreferences_第1张图片




更多相关文章

  1. Button 按钮的几个属性
  2. Android Studio按钮响应事件(一)
  3. textview中自动换行显示文本内容
  4. Android ToggleButton Example--开关按钮

随机推荐

  1. python数据存储系列教程——python中mysq
  2. linux下mysql相关命令
  3. MongoDB备份恢复与导入导出
  4. mysql 数据库备份和还原 实践
  5. 面试39 MySQL读写分离
  6. 优惠券使用限制的数据库设计
  7. 如何用sql语言只获得数据库当前日期,且格
  8. MemSQL Start[c]UP 2.0 - Round 1 E - Th
  9. PHP获取MySQL执行sql语句的查询时间
  10. Python爬虫数据存储MySQL【1】连接方式