//存储数据 public void onClick(View v) { switch(v.getId()){ case R.id.btnSaveData: SharedPreferences.Editor editor = getSharedPreferences("share", Context.MODE_PRIVATE).edit(); editor.putString("name", "linda"); editor.putInt("age", 28); editor.putFloat("height", (float) 186.00); editor.putBoolean("married", false); editor.commit(); Toast.makeText(this, "save to share", Toast.LENGTH_LONG).show(); break; default: break; } } //读取数据 public void loaddata(){ SharedPreferences spf = getSharedPreferences("share", Context.MODE_PRIVATE); String name = spf.getString("name", "default"); int age = spf.getInt("age", 10); float height = spf.getFloat("height", (float)180.00); boolean married = spf.getBoolean("married", false); Log.d("name", name); Log.d("age", age + ""); Log.d("height", height + ""); Log.d("married", married + ""); Toast.makeText(this, "Load data successed", Toast.LENGTH_LONG).show(); }
本文出自 “leboit” 博客,谢绝转载!
原文地址:http://leboit.blog.51cto.com/1465210/1690428