随时随地阅读更多技术实战干货,获取项目源码、学习资料,请关注源代码社区公众号(ydmsq666)
1.SharedPreferences保存的数据主要是类似于配置信息格式的数据,因此它保存的数据主要是简单类型的键值对。SharedPreferences接口提供了如下常用方法来访问SharedPreferences中的键值对:
2.SharedPreferences接口本身没有提供写入数据的能力,而是通过它的内部接口,即SharedPreferences调用edit()方法获取它对应的Editor对象。Editor对象提供了如下方法来写入数据:
3.SharedPreferences本身是一个接口,无法直接创建实例,只能通过Context的getSharedPreferences(String name,int mode)方法来获取实例,第二个参数支持如下几个值:
下面通过一个实例来演示SharedPreferences的使用,实现对字符文本、文件文本的写入和读取,代码如下:
Activity:
package com.lovo;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import android.app.Activity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class PreferencesTestActivity extends Activity {
private EditText test;
private TextView showText;
private TextView showFile;
// 声明SharedPreferences 对象
private SharedPreferences sp;
// 声明editor 对象
private Editor editor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_preferences_test);
test = (EditText) findViewById(R.id.activity_preferences_test_et);
showText = (TextView) findViewById(R.id.activity_preferences_test_tv_showtext);
showFile = (TextView) findViewById(R.id.activity_preferences_test_tv_showfile);
// 获得只能被本应用程序读、写的SharedPreferences对象
sp = this.getSharedPreferences("testpreferences", MODE_PRIVATE);
// 获得Editor对象
editor = sp.edit();
}
public void click(View view) {
switch (view.getId()) {
case R.id.activity_preferences_test_btn_savetext:
String name = test.getText().toString();
// 将数据以键值对方式存入editor
editor.putString("name", name);
// 提交保存数据
editor.commit();
Toast.makeText(this, "保存成功", Toast.LENGTH_SHORT).show();
break;
case R.id.activity_preferences_test_btn_readtext:
// 读取SharedPreferences里的键为name的值
String value = sp.getString("name", null);
showText.setText(value);
break;
case R.id.activity_preferences_test_btn_writefile:
try {
// MODE_APPEND:表示以追加方式打开文件输出流
FileOutputStream fos = this.openFileOutput("test.txt",
MODE_APPEND);
fos.write(test.getText().toString().getBytes());
fos.flush();// 刷新流
fos.close();// 关闭流
Toast.makeText(this, "文件写入成功", Toast.LENGTH_SHORT).show();
} catch (Exception e) {
e.printStackTrace();
}
break;
case R.id.activity_preferences_test_btn_readfile:
StringBuffer sb = new StringBuffer();
// 获取项目的files目录对应的File类对象
File file = this.getFilesDir();
sb.append(file.getAbsolutePath() + " ");
// 获取或创建目录
// File file1=this.getDir("abc", MODE_PRIVATE);
// sb.append(file1.getAbsolutePath()+" ");
try {
// 打开文件输入流
FileInputStream fis = this.openFileInput("test.txt");
BufferedReader br = new BufferedReader(new InputStreamReader(
fis));
String data = null;
while ((data = br.readLine()) != null) {
sb.append(data);
}
showFile.setText(sb.toString());
fis.close();
} catch (Exception e) {
e.printStackTrace();
}
break;
}
}
}
布局XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:text="输入文本" />
<EditText
android:id="@+id/activity_preferences_test_et"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="@+id/activity_preferences_test_btn_savetext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="click"
android:text="保存文本" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="@+id/activity_preferences_test_btn_readtext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="click"
android:text="读取文本" />
<TextView
android:id="@+id/activity_preferences_test_tv_showtext"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="@+id/activity_preferences_test_btn_writefile"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="click"
android:text="写入文件" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="@+id/activity_preferences_test_btn_readfile"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="click"
android:text="读取文件" />
<TextView
android:id="@+id/activity_preferences_test_tv_showfile"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
附上界面效果: