实验九 使用SharedPreferences存储简单数据
阅读原文时间:2021年04月20日阅读:1

实验九 使用SharedPreferences存储简单数据

一、实验要求和目的

  1. 理解SharedPreferences的基本概念

  2. 掌握使用SharedPreferences保存程序参数、选项等简单数据的方法。
    二、实验环境

  3. 部署有Android Studio和Android SDK的主机;

  4. 建议在机房的HelloWorld例子上完成。
    5.




    <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="25sp"
    android:text="请输入用户名:"
    android:id="@+id/tv2"

        ></TextView>
    <EditText
        android:layout_width="100dp"
        android:layout_height="50dp"
        android:id="@+id/et"
        android:layout_below="@+id/tv2"
        android:layout_marginLeft="100dp"
        android:layout_marginTop="35dp"
        ></EditText>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/et"
        android:layout_marginLeft="100dp"
        android:layout_marginTop="15dp"
        android:text="确定"
        android:textSize="20sp"
        android:id="@+id/bt1"
        ></Button>

Mainactivity

package com.example.shiyan9;

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.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    private SharedPreferences preferences = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //为“参数设置”按钮绑定监听器
        Button btn_set = findViewById(R.id.bt);
        btn_set.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent it = new Intent(MainActivity.this, SetActivity.class);
                startActivity(it);
            }
        });
    }
    @Override
    protected void onStart() {
        super.onStart();
        preferences = getSharedPreferences("set", Context.MODE_PRIVATE);
        String user = preferences.getString("user", "");
        TextView tv_welcome = findViewById(R.id.tv);
        tv_welcome.setText("欢迎 " + user + " 来到我的家园");
    }
}

SetActivity

package com.example.shiyan9;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class SetActivity extends AppCompatActivity {
    private SharedPreferences preferences = null;
    private SharedPreferences.Editor editor = null;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_set);
        //利用SharedPreferences读取数据并显示
        preferences = getSharedPreferences("set", Context.MODE_PRIVATE);
        //获取haredPreferences.Editor对象,尝试写数据
        editor = preferences.edit();
        //为“确定”按钮绑定监听器

        Button btn_ok = findViewById(R.id.bt1);
        final EditText et_user = findViewById(R.id.et);
        btn_ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String user = et_user.getText().toString();
                editor.putString("user", user);
                editor.apply();
                finish();
            }
        });
    }
}