SharedPreferences是Android平台上一个轻量级的存储类,主要是保存一些经常使用的配置好比窗口状态,它提供了Android平台常规的Long长整形、Int整形、String字符串型的保存。SharedPreferences不支持多线程。例如,能够经过它保存上一次用户所作的修改或者自定义参数设定,当再次启动程序后依然保持原有的设置。 html
另外的数据存储方式还有SQLite、Content Provider、File... java
用法:SharedPreferences对象自己只能获取数据而不支持存储和修改,存储修改是经过Editor对象实现的。 android
存放: git
1.得到SharedPreferences 的实例对象,经过getSharedPreferences()传递存储时的名称和模式 多线程
2.得到Editor 的实例对象,经过SharedPreferences 的实例对象的edit() 编辑器
3.存入数据用Editor的putXXX() [存放什么数据就put那个数据的类型,支持Long、Int、String、Boolean] ide
4.提交修改的数据用Editor的commit() 布局
读取: spa
1.得到SharedPreferences 的实例对象,经过getSharedPreferences()传递存储时的名称和模式 线程
2.读取数据,经过SharedPreferences 的实例对象的getXXX() [读取什么类型的数据就get那个类型]
getSharedPreferences方法扩展
getSharedPreferences("存储时的名称","模式")
模式(可组合使用):
私有: Context.MODE_PRIVATE 值0
公开可读:Context.MODE_WORLD_READABLE 值1
公开可写:Context.MODE_WORLD_WRITEABLE 值2
SharedPreferences sp = getSharedPreferences("mydata", Context.MODE_WORLD_WRITEABLE|Context.MODE_WORLD_WRITEABLE);
一、数据共享
2个activity 之间能够使用SharedPreferences来共享数据的方式
A类
Editor sharedata = getSharedPreferences("data", 0).edit(); sharedata.putString("item","hello getSharedPreferences"); sharedata.commit();B类
SharedPreferences sharedata = getSharedPreferences("data", 0); String data = sharedata.getString("item", null); Log.v("cola","data="+data);
二、保存修改
这里用记住用户名为例子解说
main.xml 布局文件
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="用户名:" /> <EditText android:id="@+id/login_user_et" android:layout_width="150dip" android:layout_height="wrap_content" android:digits="abcdefghigklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"/> </LinearLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="密 码:" /> <EditText android:id="@+id/login_pswd_et" android:layout_width="150dip" android:layout_height="wrap_content" android:password="true"/> </LinearLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="记住密码:" /> <CheckBox android:id="@+id/login_checkbox" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </LinearLayout> <Button android:id="@+id/login_btn" android:layout_width="200dip" android:layout_height="wrap_content" android:text="登录"/> </LinearLayout>java代码
public class DemoActivity extends Activity { public static final String PREFS_NAME = "prefsname"; //偏好设置名称 public static final String REMEMBER_USERID_KEY = "remember"; //记住用户名 public static final String USERID_KEY = "userid"; //用户名标记 private static final String DEFAULT_USERNAME = "Hades"; //默认用户名 //组件 private EditText userName = null; private EditText passWord = null; private CheckBox cb = null; private SharedPreferences mSettings = null; private Button submitBtn = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); userName = (EditText) findViewById(R.id.login_user_et); passWord = (EditText) findViewById(R.id.login_pswd_et); cb = (CheckBox) findViewById(R.id.login_checkbox); submitBtn = (Button) findViewById(R.id.login_btn); mSettings = getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE); //模式为私有 cb.setChecked(getRemember()); //勾选记住用户名 userName.setText(getUserName()); //设置用户名 //保存用户名 submitBtn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { //是否保存用户名 if (cb.isChecked()) { saveRemember(true); saveUserName(userName.getText().toString()); } else { saveRemember(false); saveUserName(""); } } }); } // 保存用户名 private void saveUserName(String userid) { Editor editor = mSettings.edit();// 获取编辑器 editor.putString(USERID_KEY, userid); editor.commit(); //保存数据 //editor.clear();//清除数据 } // 设置是否保存的用户名 private void saveRemember(boolean remember) { Editor editor = mSettings.edit();// 获取编辑器 editor.putBoolean(REMEMBER_USERID_KEY, remember); editor.commit(); } // 获取保存的用户名 private String getUserName() { return mSettings.getString(USERID_KEY, DEFAULT_USERNAME); } // 获取是否保存的用户名 private boolean getRemember() { return mSettings.getBoolean(REMEMBER_USERID_KEY, true); } }
页面
下面分享熊猫82 的SQLite、Content Provider、File的博文:
Android数据的四种存储方式SharedPreferences、SQLite、Content Provider和File (一) —— 总览
Android数据的四种存储方式SharedPreferences、SQLite、Content Provider和File (二) —— SQLite
Android数据的四种存储方式SharedPreferences、SQLite、Content Provider和File (三) —— SharedPreferences
Android数据的四种存储方式SharedPreferences、SQLite、Content Provider和File (四) —— ContentProvider