写入文件
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()){
//存储数据
case R.id.save_data:
//通过getSharedPreferences()方法指定SharedPreferences的文件名为data,并且得到
//SharedPreferences.Editor对象;添加不同类型的数据;调用commit()方法提交,完成存储
SharedPreferences.Editor editor = getSharedPreferences("data",MODE_PRIVATE).edit();
editor.putString("myName","cat_pp");
editor.putInt("myAge",23);
editor.putBoolean("haveGirlfriend", true);
editor.commit();
break;
**//读取数据**
case R.id.restore_data:
//通过getSharedPreferences()方法得到SharedPreferences对象,调用各自的方法读取存储的数据,打印
SharedPreferences pref = getSharedPreferences("data",MODE_PRIVATE);
String myName = pref.getString("myName","");
int myAge = pref.getInt("myAge",0);
boolean haveGirlfriend = pref.getBoolean("haveGirlfriend",false);
Log.d("MainActivity","name is " + myName);
Log.d("MainActivity","age is " + myAge);
Log.d("MainActivity","girlfriend" + haveGirlfriend);
default:
break;
}
}