Android之SharedPreferences轻量数据存储

  1. 最后生成xml文件 k-v形式
  2. SharedPreferences
  3. SharedPreferences.Editor

主线
DataStorageActivity以及对应的xml

package com.example.test0508.dataStorage;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import com.example.test0508.R;

public class DataStorageActivity extends AppCompatActivity implements View.OnClickListener {

    private Button mBtnSharedPreferences;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_data_storage);
        mBtnSharedPreferences = findViewById(R.id.btn_shared_preferences);
        mBtnSharedPreferences.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        Intent intent = null;
        switch (view.getId()){
            case R.id.btn_shared_preferences:
                intent = new Intent(DataStorageActivity.this,SharedPreferencesActivity.class);
                break;
        }
        startActivity(intent);
    }
}

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="10dp"
    >

    <Button
        android:id="@+id/btn_shared_preferences"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="SharedPreferences"
        android:textAllCaps="false"
        />

    <Button
        android:id="@+id/btn_file"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="File"
        android:textAllCaps="false"
        />
</LinearLayout>

在这里插入图片描述


SharedPreferencesActivity以及对应的xml
在这里插入图片描述

package com.example.test0508.dataStorage;

import androidx.appcompat.app.AppCompatActivity;

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

import com.example.test0508.R;

public class SharedPreferencesActivity extends AppCompatActivity {

    private EditText mEtName;
    private Button mBtnSave,mBtnShow;
    private TextView mTvContent;

    //准备
    private SharedPreferences mSharedPreferences;
    private SharedPreferences.Editor mEditor;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shared_preferences);
        mEtName = findViewById(R.id.edit_sp_name);
        mBtnSave = findViewById(R.id.btn_sp_save);
        mBtnShow = findViewById(R.id.btn_sp_show);
        mTvContent = findViewById(R.id.tv_sp_content);

        mSharedPreferences = this.getSharedPreferences("data",MODE_PRIVATE);
        mEditor = mSharedPreferences.edit();

        //两个按钮设置点击事件
        mBtnSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //保存通过editor
                mEditor.putString("name",mEtName.getText().toString());
                //commit是同步存储 apply是异步存储
                mEditor.apply();
            }
        });
        mBtnShow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //读取出来
                mTvContent.setText(mSharedPreferences.getString("name",""));
            }
        });
    }
}

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="15dp"
    >

    <EditText
        android:id="@+id/edit_sp_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请输入内容"
        />

    <Button
        android:id="@+id/btn_sp_save"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="保存"
        android:layout_marginTop="10dp"
        />

    <Button
        android:id="@+id/btn_sp_show"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="显示"
        android:layout_marginTop="10dp"
        />


    <TextView
        android:id="@+id/tv_sp_content"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
    />

</LinearLayout>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值