Android学习之SharedPerference存储


`SharedPerference不同同于文件存储,它是使用键值的方式来存储数据,对于保存的每一条数据都会给一个键值,这样在读取数据时直接通过键值取出相应数据。amdroid提供了三个方法来获取实例:
1.Context类中的getSharePreferences()方法
它接收两个参数,第一个是文件名;第二个是操作模式,目前只有MODE_PRIVATE可选,这是默认的操作模式,表示只有当前的应用可以对文件进行操作。
2.Activity类中的getPreference()方法
它只接收一个操作模式参数,因为使用这个方法会自动将类名SharedPreference作为文件名。
3.PreferenceManager类中的getDefaultSharedPreference()方法
主要由三步来实现:
  (1)调用SharedPreferences对象的edit()方法来获取一个SharedPreferences.Editor对象。
  (2)向SharedPreferences.Editor对象中添加数据,比如添加一个布尔型数据就使用putBoolean()方法,依次论推。
  (3)调用apply()方法将添加的数据提交,从而完成数据操作。`

使用案例

MainActivity:

package com.example.sharedpreferences;

import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    private Button button;
    private Button restore_btn;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button = (Button)findViewById(R.id.save_data);
        button.setOnClickListener(this);
        restore_btn = (Button)findViewById(R.id.restore_data);
        restore_btn.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.save_data:
                saveData();
                Toast.makeText(MainActivity.this,"保存成功!",Toast.LENGTH_SHORT).show();
                break;
            case R.id.restore_data:
                restorData();
                Toast.makeText(MainActivity.this,"恢复成功",Toast.LENGTH_SHORT).show();
                break;
            default:
                break;
        }
    }
    public void saveData(){
        SharedPreferences.Editor editor = getSharedPreferences("data",MODE_PRIVATE).edit();
        editor.putString("name","Tom");
        editor.putInt("age",18);
        editor.putBoolean("married",false);
        editor.apply();
    }
    public void restorData(){
        SharedPreferences preferences = getSharedPreferences("data",MODE_PRIVATE);
        String name = preferences.getString("name","");
        int age = preferences.getInt("age",0);
        boolean married = preferences.getBoolean("marred",false);
        Log.d("主活动: ","获取到名字: "+name);
        Log.d("主活动: ","获取到年龄: "+age);
        Log.d("主活动: ","获取到婚配: "+married);
    }
}

布局文件

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.sharedpreferences.MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/save_data"
            android:text="保存数据"/>
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/restore_data"
            android:text="恢复数据"/>
    </LinearLayout>

</android.support.constraint.ConstraintLayout>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值