导包
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
存储数据
// 创建应用级首选项
SharedPreferences sharedPreferences = getSharedPreferences("mySharedFile", MODE_PRIVATE);
Editor edit = sharedPreferences.edit();
edit.putBoolean("bollean", false);
edit.putFloat("float", 10F);
edit.putLong("long", 10L);
edit.putString("string", "10");
// edit.clear();//清空文件的内容 如:退出登录
// edit.remove(“int”); // 根据key移除某一个信息
edit.commit();
取数据
// 获取首选项中的数据
SharedPreferences sharedPreferences1 = getSharedPreferences("mySharedFile", MODE_PRIVATE);
boolean bollean1 = sharedPreferences1.getBoolean("boolean",false);
float float1 = sharedPreferences1.getFloat("float", 0f);
int int1 = sharedPreferences1.getInt("int", 0);
long long1 = sharedPreferences1.getLong("logn", 0L);
String string = sharedPreferences1.getString("string"," ");
System.out.println(bollean1);
System.out.println(float1);
System.out.println(int1);
System.out.println(long1);
System.out.println(string);