//存储数据 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(); }
时间: 2024-11-06 12:30:43