SharedPreferences

SharedPreferences:一个轻量级的存储类,适合存储少量数据如应用配置信息

  • SharedPreferences存储的数据是以键值对(key,value )存储的
  • 存放在/data/data//shared_prefs目录下
  • value 数据类型有:int、boolean、float、long、String、StringSet

SharedPreferences使用方法

读操作核心类

  • Activity类的getSharedPreferences(String name,int mode)得到SharedPreferences对象
    • name:保存键值对的文件名
    • mode:操作模式
  • SharedPreferences对象的getXxx(String key,Xxx value)获得键值对

关键代码

1
2
SharedPreferences sp = getSharedPreferences(“SharedPreferences”, MODE_PRIVATE);
String str = sp.getString(“name”,null);

写操作核心类

  • Activity类的getSharedPreferences(String name,int mode)得到SharedPreferences对象
    • name:保存键值对的文件名
    • mode:操作模式
  • SharedPreferences的edit()获得SharedPreferences.edit()对象
  • SharedPreferences.edit().putXxx(String key,Xxx value)写入键值对
  • SharedPreferences.edit().commit()保存键值对

关键代码

1
2
3
4
SharedPreferences sp = getSharedPreferences(“SharedPreferences”, MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
editor.putString(“name”,”data”);
editor.commit();

例子

做一个仿照qq的登录页面,输入用户名密码,通过SharedPreferences实现存储输入信息,并能读取存储的的信息;这个demon实现的功能很简单,只是实现了基本的存取数据,如果在多个数据中取某个特定数据,还需要添加功能

样式文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >

<ImageView
android:id="@+id/iv"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_centerHorizontal="true"
android:layout_margin="40dp"
android:background="@drawable/head"/>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/iv"
android:layout_centerHorizontal="true"
android:layout_marginBottom="5dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="15dp"
android:orientation="vertical">

<EditText
android:id="@+id/et1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:ems="10"
android:hint="QQ号/手机号/邮箱"
android:inputType="textPersonName"
android:textColor="#615C5C"
android:textColorHint="#FFC4BABA"
android:textSize="18sp"/>

<EditText
android:id="@+id/et2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:ems="10"
android:hint="密码"
android:inputType="textPassword"
android:selectAllOnFocus="false"
android:singleLine="false"
android:textColor="#FFC4BABA"
android:textColorHint="#FFC4BABA"/>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">

<Button
android:id="@+id/bt1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="50dp"
android:layout_marginRight="10dp"
android:layout_weight="1"
android:onClick="save"
android:text="登录" />

<Button
android:id="@+id/bt2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="50dp"
android:layout_marginRight="10dp"
android:layout_weight="1"
android:onClick="read"
android:text="新用户注册" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>

Activity

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package com.example.myapplication1;

import androidx.appcompat.app.AppCompatActivity;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;


public class MainActivity extends AppCompatActivity {
TextView et1,et2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et1=(TextView)this.findViewById(R.id.et1);
et2=(TextView)this.findViewById(R.id.et2);

Button bt1=(Button)this.findViewById(R.id.bt1);
Button bt2=(Button)this.findViewById(R.id.bt2);
}
public void save(View v){
String ac=et1.getText().toString();
String pw=et2.getText().toString();
Log.e("et1",ac);
Log.e("et2",pw);
SharedPreferences sp=getSharedPreferences("SharedPreferences",MODE_PRIVATE);
SharedPreferences.Editor editor=sp.edit();
editor.putString("账号",ac);
editor.putString("密码",pw);
editor.commit();
Toast.makeText(this,"导入成功",Toast.LENGTH_SHORT).show();
}
public void read(View v){
SharedPreferences sp=getSharedPreferences("SharedPreferences",MODE_PRIVATE);
String ac=sp.getString("账号",null);
String pw=sp.getString("密码",null);
Log.e("et1",ac);
Log.e("et2",pw);
et1.setText(ac);
et2.setText(pw);
Toast.makeText(this,"读取成功",Toast.LENGTH_SHORT).show();
}
}

manifest配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:dist="http://schemas.android.com/apk/distribution"
package="com.example.myapplication1">

<dist:module dist:instant="true" />

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="myqq"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>

结果

可以看到路径下生成了对应的文件

文件显示正确