数据存储——SharedPreferences存储

一.特点

   1.存储单一数据,例如数值,字符串,布尔

   2.存放的文件:/data/data/包名/shared_prefs/xxx.xml

     <map><string name="key">value</string></map>;

   3.以键值对的形式存储

   4.可以设置不被其他应用操作

二.API

   1.SharedPreferences

     1-获取实例  context.getSharedPreferences( )

        1>name  存储文件名

        2>mode  操作模式

           1°MODE_PRIVATE,不能被别的应用访问,覆盖模式;

           2°MODE_APPEND,不能被别的应用访问,追加模式。

     2-启动编辑器

        edit( );返回Editor。

     3-读取Value

         1>getString(key,defValue  缺省值)

         2>getAll( ) ,返回所有键值对的Map集合。

   2.Editor

      1-存放数据

         putString( key,value);putLong(key,value);putInt;putFloat;putBoolean;putStringSet。

      2-提交数据

      3-clear( )清除

      4-remove(String key)  移除指定key的键值对

存储代码展示:

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     xmlns:tools="http://schemas.android.com/tools"
 4     android:layout_width="match_parent"
 5     android:layout_height="match_parent"
 6     android:paddingBottom="@dimen/activity_vertical_margin"
 7     android:paddingLeft="@dimen/activity_horizontal_margin"
 8     android:paddingRight="@dimen/activity_horizontal_margin"
 9     android:paddingTop="@dimen/activity_vertical_margin"
10     tools:context="com.hanqi.testapp3.MainActivity"
11     android:orientation="vertical">
12 
13     <TextView
14         android:layout_width="wrap_content"
15         android:layout_height="wrap_content"
16         android:text="Hello World!" />
17 
18     <Button
19         android:layout_width="match_parent"
20         android:layout_height="wrap_content"
21         android:text="SP存储"
22         android:onClick="bt_OnClick"/>
23 
24     <Button
25         android:layout_width="match_parent"
26         android:layout_height="wrap_content"
27         android:text="SP读取"
28         android:onClick="bt1_OnClick"/>
29 </LinearLayout>
.xml
 1 package com.hanqi.testapp3;
 2 
 3 import android.content.SharedPreferences;
 4 import android.support.v7.app.AppCompatActivity;
 5 import android.os.Bundle;
 6 import android.view.View;
 7 import android.widget.Toast;
 8 
 9 
10 public class MainActivity extends AppCompatActivity {
11 
12     @Override
13     protected void onCreate(Bundle savedInstanceState) {
14         super.onCreate(savedInstanceState);
15         setContentView(R.layout.activity_main);
16     }
17 
18     public void bt_OnClick(View v)
19     {
20         //1.得到对象
21         SharedPreferences sharedPreferences=getSharedPreferences("abc",MODE_APPEND);
22 
23         //2.得到编辑器
24         SharedPreferences.Editor editor=sharedPreferences.edit();
25 
26         //3.使用Editor添加数据
27 //        editor.putString("b","xxxxx");
28 //        editor.putLong("long",123456);
29 
30         editor.remove("a");
31 
32         //4.提交保存
33         editor.commit();
34 
35         Toast.makeText(MainActivity.this, "保存数据成功", Toast.LENGTH_SHORT).show();
36     }
37 
38     public void  bt1_OnClick(View v)
39     {
40         SharedPreferences sp=getSharedPreferences("abc",MODE_PRIVATE);
41 
42         String str=sp.getString("ab", "默认值");
43 
44         Toast.makeText(MainActivity.this, "key=b "+" value="+str, Toast.LENGTH_SHORT).show();
45     }
46 }
.java

 

原文地址:https://www.cnblogs.com/arxk/p/5521976.html