SharedPreferences的用法 
 
  
 
 
      它是一个轻量级应用程序内部轻量级的存储方案,能够存放数据和读取数据,SharedPreferences只能保存简单类型的数据,例如,String、int等。一般会将复杂类型的数据转换成Base64编码,然后将转换后的数据以字符串的形式保存在 XML文件中,再用SharedPreferences保存,SharedPreferences的本质是基于XML文件存储key-value键值对数据 
 
用法 
 
 
存放数据: 
[Java] 纯文本查看 复制代码  //打开Preferences,名称为content   如果不存在则创建新的Preferences   第二个参数:访问应用程序私有文件的权限
        SharedPreferences settings = getSharedPreferences("content",MODE_PRIVATE);
        //让content处于编辑状态
        SharedPreferences.Editor edit = settings.edit();
        //存放数据
        edit.putString("name", "Android");
        //完成提交
        edit.commit(); 
取数据: 
[Java] 纯文本查看 复制代码 //文件名字要和存放时设置的名字保持一致
        SharedPreferences preferences=getSharedPreferences("content", MODE_PRIVATE);
        String name = preferences.getString("name",""); 
 
举个例子(两个页面间的存值取值): 
activity_main.xml: 
[XML] 纯文本查看 复制代码 <?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:gravity="center"
    android:orientation="vertical">
    <EditText
        android:id="@+id/et"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_margin="20dp"
        android:hint="请输入内容" />
    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="存值" />
</LinearLayout>
 
MainActivity: 
[Java] 纯文本查看 复制代码 package com.company.helloworld.firstapplication;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button button = (Button) findViewById(R.id.btn);
        final EditText et = (EditText) findViewById(R.id.et);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SharedPreferences settings = getSharedPreferences("content", MODE_PRIVATE);
                SharedPreferences.Editor edit = settings.edit();
                if (!TextUtils.isEmpty(et.getText().toString())) {
                    edit.putString("name", et.getText().toString());
                } else {
                    Toast.makeText(MainActivity.this, "请输入内容", Toast.LENGTH_SHORT).show();
                }
                edit.commit();
                Intent intent = new Intent(MainActivity.this, SecondActivity.class);
                startActivity(intent);
            }
        });
    }
}
 
activity_second.xml: 
[XML] 纯文本查看 复制代码 <?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:gravity="center"
    android:orientation="vertical">
    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="20dp" />
    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="取值" />
</LinearLayout>
 
SecondActivity: 
[Java] 纯文本查看 复制代码 package com.company.helloworld.firstapplication;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class SecondActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
        final TextView tv = (TextView) findViewById(R.id.tv);
        Button button = (Button) findViewById(R.id.btn);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //文件名字要和存放时设置的名字保持一致
                SharedPreferences preferences = getSharedPreferences("content", MODE_PRIVATE);
                String name = preferences.getString("name", "");
                tv.setText("获取到的值是----" + name);
            }
        });
    }
}
 
 
运行效果: 
 
        
 
 
 
 |