《第一行代码》第六章 SharedPreferences存储Test

本文介绍如何使用SharedPreferences在Android应用中存储和读取数据。通过三种获取SharedPreferences对象的方法,配合Editor对象实现数据的存储,再利用SharedPreferences对象进行数据读取。提供了一个简单的示例代码,演示了存储和读取的基本流程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

编者按:刚开始学习安卓,比较基础。从头到尾看郭神的书,一开始也是什么的都不会,比较着急。很多的方法什么的,代码什么的,import过程什么的,老是出错。然而回过头来看看,原来这么简单!很多人都是从这本书开始安卓生涯的。别说我,吻我,因为我只是代码的搬运工!

参考:《第一行代码》

SharedPreferences存储为Sharedpreferences文件,首先是怎么进行存储,然后是怎么进行数据取出。
存储的时候我们首先拿到SharedPreferences对象,三种方式。

  1. Context类的getSharedPreferences(a,b),,其中a为String型的包名(不包含路径,路径是默认存储的);b是操作模式,有两种(其他的已经废除),一种是MODE_PRIVATE(默认),一种是MODE_PROCESS(多个进程对同一文件进行读写的情况)。
  2. Activity中的getPreferences(a)方法,其中a只是操作模式参数,自动将当前类名作为SharedPreferences的文件名。
  3. PreferenceManager类中的getDefaultSharedPreference(a)方法,这是静态方法,只是接受Context参数,自动使用报名为文件名

哪个相对简单就用哪个,第一种相对比较麻烦,因为在读文件时,你还要输入给定的文件名进行匹配,还有就是三种方式不能交叉使用,因为各自生成的文件名不同。读出时找不到文件。这里交叉使用的意思是:存储和读出文件内容时使用的方法不一样。

拿到SharedPreferences对象之后,调用edit()方法,拿到Editor对象,并使用该对象的各种put()方法重载,用键值对的方式存储数据。

文件内容读出时,只需拿到SharedPreferences对象就可以了,调用对应put重载的get重载方法取出内容。

这里实现和文件存储一样的功能:
java:

package com.example.databasetest;

import android.app.Activity;
import android.app.Fragment.SavedState;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.text.TextUtils;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {
    private EditText edt1, edt2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        edt1 = (EditText) findViewById(R.id.edt_1);
        edt2 = (EditText) findViewById(R.id.edt_2);
        String inputString = load();//加载文件内容
        if (!TextUtils.isEmpty(inputString)) {//判断是否为空,不为空则直接显示文件中加载的内容,为空没有逻辑,本身就是空白
            edt1.setText(inputString);
            edt1.setSelection(inputString.length());
            Toast.makeText(this, "You succeed in SharedPreference",
                    Toast.LENGTH_SHORT).show();
        }
        edt2.setText(edt2.getText());
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        save();//销毁活动的时刻进行存储
    }

    public void save() {//存储数据逻辑
        String edt1String = edt1.getText().toString();
        SharedPreferences preferences = getSharedPreferences("data1",
                MODE_PRIVATE);
        Editor preEditor = preferences.edit();
        preEditor.putString("content", edt1String);
        preEditor.commit();
    }

    public String load() {//加载数据逻辑
        SharedPreferences pref=getSharedPreferences("data1", MODE_PRIVATE);
        String str = pref.getString("content", "");
        return str;
    }
}

布局文件

<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:orientation="vertical" >

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:text="@string/sharedpreferences" />

    <EditText
        android:id="@+id/edt_1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/type_something_"
        android:singleLine="true" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="30dp"
        android:text="@string/no_sharedpreferences" />

    <EditText
        android:id="@+id/edt_2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:hint="@string/type_something_"
        android:singleLine="true" />

</LinearLayout>
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值