在Android中,数据存储是开发人员不可以避免的。Android为开发者提供了很多的存储方法,在前面的博客中,已经讲述了sqlite存储数据。今天将介绍用SharedPreferences来存储数据,它可以将数据保存在应用软件的私有存储区,存储区的数据只能被写入这些数据的软件读取。SharedPreference通过键值对的方法存储数据。

1.SharedPreference存储简单数据

SharedPreference可以存放简单的String、Boolean、Int等对象。

 <RelativeLayout xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" > <TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="姓名" /> <EditText
android:id="@+id/editename"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/textView1" /> <TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editename"
android:layout_below="@+id/editename"
android:layout_marginTop="15dp"
android:text="兴趣爱好" /> <EditText
android:id="@+id/editehoby"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView2"
android:layout_below="@+id/textView2"
android:layout_marginTop="21dp"
android:ems="10" > <requestFocus />
</EditText> <CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editehoby"
android:layout_below="@+id/editehoby"
android:layout_marginTop="18dp"
android:text="是否工作" /> <TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/checkBox1"
android:layout_below="@+id/checkBox1"
android:layout_marginTop="22dp"
android:text="单位性质" /> <RadioGroup
android:id="@+id/radioGroup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView3"
android:layout_below="@+id/textView3"
android:layout_marginTop="24dp" > <RadioButton
android:id="@+id/radio0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="国营" /> <RadioButton
android:id="@+id/radio1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="私营" /> <RadioButton
android:id="@+id/radio2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="股份制" />
</RadioGroup> <Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/radioGroup1"
android:layout_alignRight="@+id/editehoby"
android:layout_marginBottom="26dp"
android:layout_marginRight="30dp"
android:text="tiao" /> </RelativeLayout>

main.xml

这里定义了几个edittext。

 protected void onStop()
{
//获得SharedPreference对象
SharedPreferences myShared=getSharedPreferences(PREFERENCE_NAME, Activity.MODE_PRIVATE);
//获得editor对象
SharedPreferences.Editor editor=myShared.edit();
//添加需要保存的数据
editor.putString("name", edname.getText().toString());
editor.putString("hobby", edhoby.getText().toString());
editor.putBoolean("employee", cbcareer.isChecked());
editor.putInt("companytype", rdCompanyType.getCheckedRadioButtonId());
//提交数据
editor.commit();
super.onStop(); }

保存数据

这对数据的存储,并没有放在单独的事件中,而是放在onstop方法中。当activity停止的时候,会自动提交数据。

     protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edname=(EditText)findViewById(R.id.editename);
edhoby=(EditText)findViewById(R.id.editehoby);
cbcareer=(CheckBox)findViewById(R.id.checkBox1);
Button button=(Button)findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() { @Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent=new Intent();
intent.setClass(MainActivity.this, ProductSharedActivity.class);
startActivity(intent);
}
});
rdCompanyType=(RadioGroup)findViewById(R.id.radioGroup1);
SharedPreferences sharedpre=getSharedPreferences(PREFERENCE_NAME, Activity.MODE_PRIVATE);
edname.setText(sharedpre.getString("name", ""));
edhoby.setText(sharedpre.getString("hobby", ""));
cbcareer.setChecked(sharedpre.getBoolean("employee",false));
rdCompanyType.check(sharedpre.getInt("companytype", -1)); }

数据读取

数据读取与保存的方法类似。

2.SharedPreference保存复杂数据

SharedPreference不仅可以保存简单的数据,而且可以保存复杂的数据对象,比如对象、图像等。保存复杂的数据类型,需要对数据进行编码。对数据的保存方法和上面的基本一致

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" > <TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="16dp"
android:layout_marginTop="15dp"
android:text="产品ID" /> <EditText
android:id="@+id/txtID"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_marginTop="25dp"
android:ems="10" > <requestFocus />
</EditText> <TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/txtID"
android:layout_below="@+id/txtID"
android:layout_marginTop="18dp"
android:text="产品名称" /> <EditText
android:id="@+id/txtName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView2"
android:layout_below="@+id/textView2"
android:layout_marginTop="32dp"
android:ems="10" /> <TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/txtName"
android:layout_centerVertical="true"
android:text="产品价格" /> <EditText
android:id="@+id/txtprice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView3"
android:layout_below="@+id/textView3"
android:layout_marginTop="28dp"
android:ems="10" /> <ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/txtprice"
android:layout_toRightOf="@+id/textView3" /> <Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/textView3"
android:layout_below="@+id/imageView1"
android:layout_marginTop="34dp"
android:text="选择图像" /> </RelativeLayout>

base.xml

后台代码:

     protected void onStop()
{
Product product=new Product();
/*product.setID(edid.getText().toString());
product.setName(edname.getText().toString());
product.setPrice(edprice.getText().toString());*/
product.productname=edname.getText().toString();
product.productid=edid.getText().toString();
product.productprice=edprice.getText().toString(); ByteArrayOutputStream baos=new ByteArrayOutputStream();
ObjectOutputStream oos;
((BitmapDrawable)imageview.getDrawable()).getBitmap().compress(CompressFormat.JPEG, 50, baos);
String imagebase=new String(Base64.encode(baos.toByteArray(), Base64.DEFAULT));
try {
oos = new ObjectOutputStream(baos);
oos.writeObject(product);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
};
//获得SharedPreference对象
SharedPreferences myshared=getSharedPreferences("base64", Activity.MODE_PRIVATE);
String productbase=new String(Base64.encode(baos.toByteArray(),Base64.DEFAULT));
//获得editor对象
SharedPreferences.Editor editor=myshared.edit();
//添加需要存储的数据
editor.putString("product", productbase);
editor.putString("productimage", imagebase);
//提交保存数据
editor.commit(); super.onStop();
}

数据保存

这里需要保存的数据都经过了base64的编码处理,在编码之前需要将其转为流的形式。

最新文章

  1. MySQL实现嵌套集合模型
  2. 史上最强大的40多个纯CSS绘制的图形
  3. uC/OS-II类型定义
  4. Hardwood Species 分类: POJ 树 2015-08-05 16:24 2人阅读 评论(0) 收藏
  5. OpenShare新功能@2014年第三季度
  6. PC上面的蓝牙的通信(C#)
  7. subline的安装
  8. 开始学习&lt;p&gt;标签,添加段落
  9. openwrt time sycronize
  10. 微信LazyMan笔试题的深入解析和实现
  11. virtualBox,webstorm,开虚拟机传代码
  12. 设置TCP_USER_TIMEOUT参数来判断tcp连接是否断开
  13. Cocos2D中相关问题提问的几个论坛
  14. SAP 没有激活HUM功能照常可以使用Handling Unit
  15. Spring Cloud微服务Ribbon负载均衡/Zuul网关使用
  16. ZooKeeper基础
  17. exshop第6天
  18. F5-VM
  19. jQuery添加自定义函数的三种方法
  20. 利用capability特征加强Linux系统安全【转】

热门文章

  1. wamp安装
  2. 简单的javascript实例二(随页面滚动广告效果)
  3. hdu2952Counting Sheep
  4. C++ 多态性浅谈
  5. [Effective C++系列]-为多态基类声明Virtual析构函数
  6. SecureCRT使用的技巧 键盘修改
  7. Struts2注解学习1
  8. H5 progress标记
  9. 【应用】Markdown 在线阅读器
  10. php isset — 检测变量是否设置 foreach循环运用