sp存储

sp存储专门用来存储一些单一的小数据

存储的数据类型为:boolean,float,int,long,String

数据保存的路径:/data/data/packageNmae/shared_prefs/yyy.xml

可以设置数据只能当前应用读取,而别的应用不可以

应用卸载会删除数据

相关API文档

1.SharedPreferences:对应的sp文件的接口

——context.getSharedPreferences(String name,int mode):得到sp对象

  name:文件名(不带.xml)

  mode:生成文件模式(是否是私有,其他应用是否可以访问)

——Edit sp.edit()得到Editor对象

——xxx sp.getxxx(name,defaultValue):根据name得到对应的数据

2.Editor:能更新sp文件接口

——Editor put(name,value):保存一个键值对,没有真正保存到文件中

——Editor remove(name)

——commit():提交,数据真正保存到文件中

示例:

 

 

 在两个输入框中分别输入key 和value值,按保存,即可保存。读取:是输入key点击读取即可在第二个框中显示对应的value值

直接上代码:

activity_sp.xml

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:orientation="vertical">
 6 
 7     <EditText
 8         android:id="@+id/et_sp_key"
 9         android:layout_width="match_parent"
10         android:layout_height="wrap_content"
11         android:layout_margin="8dp"
12         android:hint="请输入KEY"
13         android:textSize="25sp" />
14 
15     <EditText
16         android:id="@+id/et_sp_value"
17         android:layout_width="match_parent"
18         android:layout_height="wrap_content"
19         android:layout_margin="8dp"
20         android:layout_marginTop="8dp"
21         android:hint="请输入VALUE"
22         android:textSize="25sp" />
23 
24     <LinearLayout
25         android:layout_width="match_parent"
26         android:layout_height="wrap_content"
27         android:layout_margin="8dp"
28         android:orientation="horizontal">
29 
30         <Button
31             android:layout_width="wrap_content"
32             android:layout_height="wrap_content"
33             android:layout_weight="1"
34             android:gravity="center_horizontal"
35             android:onClick="btn_save"
36             android:text="保存"
37             android:textSize="25sp" />
38 
39         <Button
40             android:layout_width="wrap_content"
41             android:layout_height="wrap_content"
42             android:layout_weight="1"
43             android:gravity="center_horizontal"
44             android:onClick="btn_read"
45             android:text="读取"
46             android:textSize="25sp" />
47     </LinearLayout>
48 </LinearLayout>

java文件

 1 package com.example.a13320.teststage;
 2 
 3 import android.app.Activity;
 4 import android.content.Context;
 5 import android.content.SharedPreferences;
 6 import android.graphics.Color;
 7 import android.os.Bundle;
 8 import android.view.View;
 9 import android.widget.EditText;
10 import android.widget.Toast;
11 
12 /**
13  * Created by 13320 on 2020/3/5.
14  */
15 public class SP_Activity extends Activity {
16     private EditText et_sp_key,et_sp_value;
17     private SharedPreferences sp;
18     @Override
19     protected void onCreate(Bundle savedInstanceState) {
20         super.onCreate(savedInstanceState);
21         setContentView(R.layout.activity_sp);
22         et_sp_key=(EditText)findViewById(R.id.et_sp_key);
23         et_sp_value=(EditText)findViewById(R.id.et_sp_value);
24         sp=getSharedPreferences("haha", Context.MODE_PRIVATE);
25     }
26     public void btn_save(View v){
27         //获得edit对象
28        SharedPreferences.Editor dt=sp.edit();
29         //获取edittext内容
30         String key=et_sp_key.getText().toString();
31         String value=et_sp_value.getText().toString();
32         //使用edit对象保存key value.     commit别忘了
33         dt.putString(key,value).commit();
34         //提示
35         Toast.makeText(SP_Activity.this,"保存成功",Toast.LENGTH_SHORT).show();
36     }
37     public void btn_read(View v){
38         //获取key值
39         String key=et_sp_key.getText().toString();
40         //获取value
41         String value=sp.getString(key,null);
42         //显示
43         if (value==null){
44             Toast.makeText(SP_Activity.this,"没有找到对应的value",Toast.LENGTH_SHORT).show();
45         }else{
46             et_sp_value.setText(value);
47         }
48     }
49 }

 

posted @ 2020-03-05 20:10  东功  阅读(1455)  评论(0编辑  收藏  举报