一.基本知识点
- 安卓提供了一种简单的数据存储方式SharedPreferences
- 这是一种轻量级的数据保存方式,用来存储一些简单的配置信息
- 以键值对的方式存储在一个XML文件中。
二.通过实训案例来了解
1.建立安卓项目
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/8d625b3d17cf744ed652e5aea119ef2a.png)
2.准备图片资源
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/bcb13c35ad63d0a0a164f304472f2dbe.png)
3.字符串资源文件定义变量string.xml
<resources>
<string name="app_name">共享参数演示——读写数据</string>
<string name="write_data">写入数据</string>
<string name="read_data">读取数据</string>
</resources>
4.设计主布局资源文件activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background"
android:orientation="vertical"
android:gravity="center"
tools:context=".MainActivity">
<Button
android:id="@+id/btnWriteData"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:onClick="doWriteData"
android:text="@string/write_data"
android:textSize="18sp"
tools:ignore="OnClick" />
<Button
android:id="@+id/btnReadData"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:onClick="doReadData"
android:text="@string/read_data"
android:textSize="18sp"
tools:ignore="OnClick" />
</LinearLayout>
5.主界面类MainActivity.java
package net.yuanjing.readwritedate;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private static final String NAME = "person_info";
private static final int MODE = Context.MODE_PRIVATE;
private SharedPreferences sharedPreferences;
private SharedPreferences.Editor editor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sharedPreferences = getSharedPreferences(NAME, MODE);
editor = sharedPreferences.edit();
}
public void doWriteData(View view) {
editor.putString("name", "YJ");
editor.putString("gender", "女");
editor.putInt("age", 20);
editor.putString("hobby", "旅游、音乐、舞蹈、美食、编程");
if (editor.commit()) {
Toast.makeText(this, "恭喜,数据写入文件成功!", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "遗憾,数据写入文件失败!", Toast.LENGTH_SHORT).show();
}
}
public void doReadData(View view) {
String name = sharedPreferences.getString("name", "");
String gender = sharedPreferences.getString("gender", "");
int age = sharedPreferences.getInt("age", 0);
String hobby = sharedPreferences.getString("hobby", "");
if (name.equals("")) {
Toast.makeText(this, "温馨提示:你还没有写入数据到文件呢!", Toast.LENGTH_SHORT).show();
} else {
StringBuilder builder = new StringBuilder();
builder.append("姓名:" + name + "\n")
.append("性别:" + gender + "\n")
.append("年龄:" + age + "\n")
.append("爱好:" + hobby + "\n");
String personInfo = builder.toString();
Toast.makeText(this, personInfo, Toast.LENGTH_SHORT).show();
}
}
}
6.最终实现的效果
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/f37202bdbfec9942b9036d807f44103c.gif)