SharedPreferences是Android平台上一个轻量级的存储类,主要是保存一些常用的配置比如窗口状态,它提供了Android平台常规的Long长整形、Int整形、String字符串型的保存。

10年的武清网站建设经验,针对设计、前端、开发、售后、文案、推广等六对一服务,响应快,48小时及时工作处理。网络营销推广的优势是能够根据用户设备显示端的尺寸不同,自动调整武清建站的显示方式,使网站能够适用不同显示终端,在浏览器中调整网站的宽度,无论在任何一种浏览器上浏览网站,都能展现优雅布局与设计,从而大程度地提升浏览体验。创新互联从事“武清网站设计”,“武清网站推广”以来,每个客户项目都认真落实执行。
SharedPreferences不支持多线程。例如,可以通过它保存上一次用户所做的修改或者自定义参数设定,当再次启动程序后依然保持原有的设置。
另外的数据存储方式还有SQLite、Content Provider、File...
用法:
SharedPreferences对象本身只能获取数据而不支持存储和修改,存储修改是通过Editor对象实现的。
存放:
1.获得SharedPreferences 的实例对象,通过getSharedPreferences()传递存储时的名称和模式
2.获得Editor 的实例对象,通过SharedPreferences 的实例对象的edit()
3.存入数据用Editor的putXXX() [存放什么数据就put那个数据的类型,支持Long、Int、String、Boolean]
4.提交修改的数据用Editor的commit()
读取:
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);
 
1、数据共享
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);
 
2、保存修改
这里用记住用户名为例子解说
main.xml 布局文件
- android:layout_width="fill_parent"
 - android:layout_height="fill_parent"
 - android:orientation="vertical" >
 - android:layout_width="fill_parent"
 - android:layout_height="wrap_content"
 - android:orientation="horizontal">
 - android:layout_width="wrap_content"
 - android:layout_height="wrap_content"
 - android:text="用户名:" />
 - android:id="@+id/login_user_et"
 - android:layout_width="150dip"
 - android:layout_height="wrap_content"
 - android:digits="abcdefghigklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"/>
 - android:layout_width="fill_parent"
 - android:layout_height="wrap_content"
 - android:orientation="horizontal">
 - android:layout_width="wrap_content"
 - android:layout_height="wrap_content"
 - android:text="密 码:" />
 - android:id="@+id/login_pswd_et"
 - android:layout_width="150dip"
 - android:layout_height="wrap_content"
 - android:password="true"/>
 - android:layout_width="fill_parent"
 - android:layout_height="wrap_content"
 - android:orientation="horizontal">
 - android:layout_width="wrap_content"
 - android:layout_height="wrap_content"
 - android:text="记住密码:" />
 - android:id="@+id/login_checkbox"
 - android:layout_width="wrap_content"
 - android:layout_height="wrap_content"/>
 
- 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);
 - }
 - }
 
页面:
Copyright © 2009-2022 www.wtcwzsj.com 青羊区广皓图文设计工作室(个体工商户) 版权所有 蜀ICP备19037934号