Android持久化技术之SharedPreferences存储实例详解

作者:残缺的孤独 时间:2023-03-25 03:17:10 

本文实例讲述了Android持久化技术之SharedPreferences存储。分享给大家供大家参考,具体如下:

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;
 @Override
 protected 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() {
     @Override
     public 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();
     }
   });
 }
 @Override
 public 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存储实例详解

对应内容:

Android持久化技术之SharedPreferences存储实例详解

下面是效果图:

Android持久化技术之SharedPreferences存储实例详解 Android持久化技术之SharedPreferences存储实例详解

希望本文所述对大家Android程序设计有所帮助。

标签:Android,持久化,SharedPreferences
0
投稿

猜你喜欢

  • 如何自动生成Mybatis的Mapper文件详解

    2023-07-28 08:20:54
  • Spring基于常用AspectJ切点表达式使用介绍

    2023-12-08 19:58:37
  • Java常量池详解

    2023-05-19 19:36:27
  • Java实现简单无界面五子棋

    2022-07-10 12:06:02
  • 解决Weblogic部署war找不到spring配置文件的问题

    2022-12-29 07:03:08
  • 拉钩网java笔试题分享

    2022-02-13 08:48:25
  • Spring boot项目使用thymeleaf模板过程详解

    2022-10-20 22:30:58
  • Java实现单链表基础操作

    2021-08-07 13:46:04
  • Android自定义view实现输入控件

    2022-12-11 10:08:06
  • Java使用原型模式展现每日生活应用案例详解

    2023-03-08 04:27:08
  • Java Thread之Sleep()使用方法及总结

    2023-11-16 10:38:35
  • Android 中使用EditText 点击全选再次点击取消全选功能

    2023-09-08 00:08:44
  • 完美解决Android App启动页有白屏闪过的问题

    2021-11-18 02:12:31
  • 详解C#如何读写config配置文件

    2023-09-23 01:18:34
  • 关于Jedis的用法以及Jedis使用Redis事务

    2023-06-28 07:22:56
  • C#实现的文件操作封装类完整实例【删除,移动,复制,重命名】

    2022-09-14 20:41:14
  • 详谈.net中的垃圾回收机制

    2022-12-04 03:52:27
  • Java多线程之线程同步

    2023-01-24 18:39:00
  • Android编程之高效开发App的10个建议

    2021-08-28 15:55:46
  • 超简洁java实现双色球若干注随机号码生成(实例代码)

    2023-12-03 22:39:08
  • asp之家 软件编程 m.aspxhome.com