多窗口共享数据

目录

理论:

1、共享参数概述   

2、利用共享参数读写文件步骤

案例演示:

基于Empty Activity模板创建安卓应用ShareData

 将背景图片拷贝到drawable目录

将MainActivity更名为FirstActivity,对应的布局文件actiivty_main.xml更名为activity_first.xml

 基于Empty Activity模板创建第二界面类

更改字符串资源文件strings.xml

打开布局资源文件activity_first.xml写入代码:

打开布局资源文件 activity_second.xml输入代码:

打开界面类 FirstActivity输入:

打开界面类 SecondActivity输入:

启动应用查看效果:

查看保存数据的文件

 将个人信息文件另存到本地电脑

利用记事本打开:


理论:

1、共享参数概述   


安卓提供了一种简单的数据存储方式SharedPreferences [共享偏好],这是一种轻量级的数据保存方式,用来存储一些简单的配置信息,以键值对的方式存储在一个XML文件中。


2、利用共享参数读写文件步骤


利用Activity的getPreferences(name, mode)方法得到SharedPreferences对象
使用SharedPreferences对象的edit()得到Editor对象
利用Editor对象的putXxx()方法实现数据写入;利用SharedPreferences对象的getXxx()实现数据读取
对于写入操作,利用Editor对象的commit()方法提交数据到指定的文件里


案例演示:

基于Empty Activity模板创建安卓应用ShareData

 将背景图片拷贝到drawable目录

MainActivity更名为FirstActivity,对应的布局文件actiivty_main.xml更名为activity_first.xml

 

 基于Empty Activity模板创建第二界面类

 

更改字符串资源文件strings.xml

 具体代码:

<resources>
    <string name="app_name">共享参数演示——读写数据</string>
    <string name="write_data">写入数据</string>
    <string name="read_data">读取数据</string>
    <string name="jump_to_second">跳转第二个窗口</string>
</resources>

打开布局资源文件activity_first.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/background1"
    android:orientation="vertical"
    android:gravity="center"
    tools:context=".FirstActivity">

    <Button
        android:id="@+id/btn_write_data"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:onClick="doWriteData"
        android:text="@string/write_data"
        android:textSize="20sp" />

    <Button
        android:id="@+id/btn_jump_to_second"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:onClick="doJumpToSecond"
        android:text="@string/jump_to_second"
        android:enabled="false"
        android:textSize="20sp" />
</LinearLayout>

打开布局资源文件 activity_second.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/background2"
    android:gravity="center"
    android:orientation="vertical"
    tools:context=".SecondActivity">
    <Button
        android:id="@+id/btn_read_date"
        android:layout_width="220dp"
        android:layout_height="wrap_content"
        android:text="@string/read_data"
        android:onClick="doReadData"
        android:textSize="20sp"/>
    <TextView
        android:id="@+id/tv_person_info"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:textSize="20sp"/>

</LinearLayout>

打开界面类 FirstActivity输入:

 具体代码:

package net.zyt.share_date;

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";//配置文件界面
    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();
    }
    //写入数据,按钮数据单击处理方法
    public void doWriteData(View view){

        editor.putString("name","小红");
        editor.putString("gender","女");
        editor.putInt("age",50);
        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();
        }

    }

    //【跳转到第二个窗口】单击事件处理方法

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

}

打开界面类 SecondActivity输入:

具体代码:

package net.zyt.share_date;

import androidx.appcompat.app.AppCompatActivity;

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

public class SecondActivity extends AppCompatActivity {
    private static final String NAME="person_info";//配置文件界面
    private static final int MODE= Context.MODE_PRIVATE;//文件访问模式
    private SharedPreferences sp;//共享参数对象
    private TextView tvPersonInfo;//个人信息标签

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //利用布局资源文件设置用户界面
        setContentView(R.layout.activity_second);
        //通过控件资源标识符获得控件实例
        tvPersonInfo=findViewById(R.id.tv_person_info);
        //获取共享参数对象
        sp=getSharedPreferences(NAME,MODE);
    }
    //读取数据】单击事件处理方法
    public void doReadData(View view){
        //通过共享参数对象获取文件数据
        String name=sp.getString("name","");
        String gender=sp.getString("gender","");
        int age=sp.getInt("age",0);
        String hobby=sp.getString("hobby","");
        // 创建个人信息字符串生成器
        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();
        // 将个人信息显示在标签里
        tvPersonInfo.setText(personInfo);
    }
}

启动应用查看效果:

查看保存数据的文件

 

 

 将个人信息文件另存到本地电脑

 

利用记事本打开:

 

猜你喜欢

转载自blog.csdn.net/hollow_future/article/details/128256471