一、数据存储
共享参数
文件流操作
SQLite应用
XML与JSON解析
二、共享参数
1、共享参数概述
安卓提供了一种简单的数据存储方式SharedPreferences,这是一种轻量级的数据保存方式,用来存储一些简单的配置信息,以键值对的方式存储在一个XML文件中。
2、利用共享参数读写文件步骤
三、案例演示:读写数据
1、创建安卓应用【ShareData】
基于Empty Activity模板创建安卓应用 - ShareData

Android 内存数据共享 安卓共享数据_Android 内存数据共享


单击【finish】按钮

2、准备图片素材

将背景图片拷贝到drawable目录

Android 内存数据共享 安卓共享数据_android_02

 

3、字符串资源文件strings.xml

Android 内存数据共享 安卓共享数据_开发语言_03


<resources>

    <string name="app_name">多窗口共享数据</string>

    <string name="write_data">写入数据</string>

    <string name="read_data">读取文件</string>

    <string name="jump_to_second">跳转第二个窗口</string>

</resources>


4、创建两个界面类

将MainActivity更名为FirstActivity,对应的布局资源文件activity.xml改为activity_first.xml 

Android 内存数据共享 安卓共享数据_java_04

 

新建一个SecondActivity,对应布局资源文件activity_second.xml
5、第一界面布局文件
第一界面布局文件activity_first.xml

Android 内存数据共享 安卓共享数据_开发语言_05

 

<?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:background="@drawable/background1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center"
    tools:context=".FirstActivity">

    <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" />

    <Button
        android:id="@+id/btnReadData"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:onClick="doJumpToSecond"
        android:text="@string/jump_to_second"
        android:textSize="18sp" />

</LinearLayout>
6、第二界面布局文件
第二界面布局文件activity_second.xml

Android 内存数据共享 安卓共享数据_xml_06

 

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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/background2"
    android:gravity="center"
    tools:context=".SecondActivity">

    <Button
        android:id="@+id/btn_read_data"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/read_data"
        android:textSize="20sp"/>

</FrameLayout>

7、第一界面类实现
第一界面类 - FirstActivity

Android 内存数据共享 安卓共享数据_开发语言_07

声明变量和常量

Android 内存数据共享 安卓共享数据_开发语言_08

 

获取共享参数对象

Android 内存数据共享 安卓共享数据_java_09

 

获取编辑器对象

Android 内存数据共享 安卓共享数据_Android 内存数据共享_10

 

编写【写入数据】按钮单击事件处理方法
package net.xyx.share_data;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

public class FirstActivity extends AppCompatActivity {

    private static final String NAME = "person_info.xml";//配置文件名
    private static final int MODE = Context.MODE_PRIVATE;//文件访问模式
    private SharedPreferences sp;//共享参数对象
    private SharedPreferences.Editor editor;//编辑器对象

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //利用布局资源文件设置用户界面
        setContentView(R.layout.activity_first);

        //获取共享参数对象
        sp = getSharedPreferences(NAME,MODE);
        //获取编辑器对象
        editor = sp.edit();
    }

    /**
     * 【写入数据】按钮单击事件处理方法
     * 
     * @param view
     */
    public void doWriteData(View view){
        // 将数据写入编辑器
        editor.putString("name","易烊千玺");
        editor.putString("gender","男");
        editor.putInt("age",22);
        editor.putString("hobby","音乐,街舞,戏剧");
        //提交数据,写入到指定的文件
        if(editor.commit()){
            Toast.makeText(this,"恭喜,数据写入文件成功!",Toast.LENGTH_SHORT).show();
            findViewById(R.id.btn_jump_to_second).setEnabled(true);//让【跳转到第二个窗口】按钮可用
        }else {
            Toast.makeText(this,"遗憾,数据写入文件失败!",Toast.LENGTH_SHORT).show();
        }
    }

    /**
     * 
     * @param view
     */
    public void doJumpToSecond(View view){
        //创建跳转到第二个窗口的意图
        Intent intent = new Intent(this, SecondActivity.class);
        //按意图启动第二个窗口
        startActivity(intent);
    }
}

8、第二界面类实现功能
第二界面类 - SecondActivity

Android 内存数据共享 安卓共享数据_开发语言_11


定义常量和变量 

Android 内存数据共享 安卓共享数据_Android 内存数据共享_12


通过控件资源标识符获得控件实例 

Android 内存数据共享 安卓共享数据_java_13


获取共享参数对象 

Android 内存数据共享 安卓共享数据_android_14


编写【读取数据】按钮单击事件处理方法

 

Android 内存数据共享 安卓共享数据_android_15