当前位置: 首页 > 编程笔记 >

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

梁研
2023-03-14
本文向大家介绍Android持久化技术之SharedPreferences存储实例详解,包括了Android持久化技术之SharedPreferences存储实例详解的使用技巧和注意事项,需要的朋友参考一下

本文实例讲述了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.html" target="_blank">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程序设计有所帮助。

 类似资料:
  • 本文向大家介绍Flutter持久化存储之数据库存储(sqflite)详解,包括了Flutter持久化存储之数据库存储(sqflite)详解的使用技巧和注意事项,需要的朋友参考一下 前言 数据库存储是我们常用的存储方式之一,对大批量数据有增、删、改、查操作需求时,我们就会想到使用数据库,Flutter中提供了一个sqflite插件供我们用于大量数据执行CRUD操作。本篇我们就来一起学习sqflite

  • 本文向大家介绍Android本地存储SharedPreferences详解,包括了Android本地存储SharedPreferences详解的使用技巧和注意事项,需要的朋友参考一下 Android本地存储SharedPreferences详解 存储位置 SharedPreferences数据保存在: /data /data/<package_name> /shared_prefs 文件夹下,以X

  • 本文向大家介绍Android SharedPreferences存储用法详解,包括了Android SharedPreferences存储用法详解的使用技巧和注意事项,需要的朋友参考一下 先看Demo运行效果 SharedPreferences详解 SharedPreferences是Android平台上一个轻量级的存储类,用来保存应用的一些常用配置,比如Activity状态,Activity暂停

  • 本平台是通过storageclass来动态创建PV。也就是说咱们依赖于storageclass,如果您的Kubernetes不支持相应的存储试,将无法非常方便的进行挂载。 目前暂不支持挂载多个PVC,或许以后会更新吧。 这里演示的是用的NFS进行演示,实际使用时可根据自己的需求配置相应的provisioner,其他配置是一样的不需要调整,只需要在“模版管理” 调整StorageClass和Pers

  • 本地持久化卷允许用户通过标准 PVC 接口以简单便携的方式访问本地存储。PV 中包含系统用于将 Pod 安排到正确节点的节点亲和性信息。 一旦配置了本地卷,外部静态配置器(provisioner)可用于帮助简化本地存储管理。请注意,本地存储配置器与大多数配置器不同,并且尚不支持动态配置。相反,它要求管理员预先配置每个节点上的本地卷,并且这些卷应该是: Filesystem volumeMode(默

  • 持久化存储的相关配置 这里使用的是NFS的方式进行持久化,如果您有自己的持久化方案可以不使用改方案。 $ kubectl apply -f install/kubernetes/storage/serviceaccount.yaml $ kubectl apply -f install/kubernetes/storage/rbac.yaml $ kubectl apply -f install/

  • 主要内容:一、数据持久化,二、持久化的形式,三、源码分析,四、总结一、数据持久化 redis做为一种内存型数据库,做持久化,个人感觉略有鸡肋的意思。似乎有一种,别人有,自己不有也不行的感觉。以目前Redis主流的应用方式,如果仔细分析,基本上都是在内存中即可完成,对持久化没要求或者说不大。再举一个反例,如果内存中有几百G甚至更多的数据,真要是整体当机,恢复的时间基本就是灾难。 目前基本应用仍然是以关系型数据库或者其它数据库(如Hadoop,Mysql等)为持久化

  • 本文向大家介绍Android编程之SharedPreferences文件存储操作实例分析,包括了Android编程之SharedPreferences文件存储操作实例分析的使用技巧和注意事项,需要的朋友参考一下 本文实例讲述了Android编程之SharedPreferences文件存储操作的方法。分享给大家供大家参考。具体分析如下: SharedPreferences类提供了一种简单的文件存储功