sharepreference(偏好参数保存)
来源:互联网 发布:好看的书法字体 知乎 编辑:程序博客网 时间:2024/04/28 17:46
sharepreference专门用于保存用户的偏好设置参数,它是一个轻量级的存储类,特别适合用于保存软件配置参数
SharedPreferences保存数据其背后是用xml文件存放数据,文件存放在/data/data/<package name>/shared_prefs/xxx.xml目录下
布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="姓名" /> <EditText android:id="@+id/name" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="年龄" /> <EditText android:id="@+id/age" android:layout_width="fill_parent" android:numeric="integer" android:layout_height="wrap_content" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="save" android:text="保存" /></LinearLayout>
MainActivity
@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);name_text = (EditText) this.findViewById(R.id.name);age_text = (EditText) this.findViewById(R.id.age);service = new preferencesService(this);//下面回显参数Map<String, String> params = service.getPreference();name_text.setText(params.get("name"));age_text.setText(params.get("age"));}public void save(View v){String name = name_text.getText().toString();String age = age_text.getText().toString();service.save(name,age);Toast.makeText(getApplicationContext(), "保存完成", 1).show();}}
preferencesService业务类
public class preferencesService {private Context context;public preferencesService(Context context) {this.context = context;}//保存参数public void save(String name, String age) {<span style="white-space:pre">//参数1:指定该文件的名称,不用带后缀,参数2:指定文件的操作模式<span style="white-space:pre"></span>Editor editor = preference.edit();<span style="white-space:pre"></span>//得到保存数据的编辑器</span><span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">editor存储对象采用key-value键值对进行存放</span>SharedPreferences preference = context.getSharedPreferences("params", context.MODE_PRIVATE);Editor editor = preference.edit();//得到保存数据的编辑器editor.putString("name", name);<span style="white-space:pre"></span>//editor存储对象采用key-value键值对进行存放editor.putInt("age", new Integer(age));editor.commit();//提交数据}//获取配置参数public Map<String,String> getPreference(){Map<String,String> params = new HashMap<String, String>();SharedPreferences preference = context.getSharedPreferences("itcase", context.MODE_PRIVATE);params.put("name", preference.getString("name", ""));//获取参数保存到集合里面,参数2:为缺省值,如果preference中不存在该key,将返回缺省值params.put("age", String.valueOf(preference.getInt("age", 0)));return params;} }
0 0
- sharepreference(偏好参数保存)
- SharedPreferences(保存偏好参数)
- Android开发总结笔记 SharePreference(偏好参数) 2-1
- 采用SharedPreferences偏好参数保存
- 使用SharedPreferences保存偏好参数
- SharedPreferences(保存用户偏好参数)保存数据
- SharedPreferences(保存用户偏好参数)
- 采用SharedPreferences保存用户偏好设置参数
- 采用SharedPreferences保存用户偏好设置参数
- Android采用SharedPreferences保存用户偏好设置参数
- Android数据存储方式(二)SharedPreferences(偏好参数保存)
- 实例教程五:采用SharedPreferences保存用户偏好设置参数
- Android采用SharedPreferences保存用户偏好设置参数
- Android数据存储方式(二)SharedPreferences(偏好参数保存)
- 16_采用SharedPreferences保存用户偏好设置参数
- 数据存储与访问之SharedPreferences(偏好参数保存)
- 数据类操作之SharedPreferences(保存用户偏好参数)
- 数据类操作之SharedPreferences(保存用户偏好参数)
- hibernate学习笔记
- Qt 之 The Property System
- SolrCloud同一shard的不同replica返回结果评分不一致
- 组合模式
- UVA -(11795 weapon 状态的附加信息不需再开一维)
- sharepreference(偏好参数保存)
- 详解PHP中的过滤器(Filter)
- 非常全面的讲解Hosts文件
- 求一元二次方程
- scrollview 显示位置问题
- copy命令介绍
- 一种使用Qt快速绘图的思路
- 查找IFileSourceFilter上的Pin
- LINUX启动分析