资 源 简 介
Android SharePreferences读出和写入数据测试:
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
preferences = getSharedPreferences("myPreference", MODE_PRIVATE);
editor = preferences.edit();
writeBtn = (Button)findViewById(R.id.write);
readBtn = (Button)findViewById(R.id.read);
edit = (EditText)findViewById(R.id.edit);
writeBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
editor.putString("time", sdf.format(new Date()));
editor.putInt("random", (int)(Math.random()*100));
editor.commit();
}
});
readBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String date = preferences.getString("time", null);
int radom = preferences.getInt("random", 0);
edit.setText(date + " " + radom );
Toast.makeText(SharePreferencesSampleActivity.this, "时间为:" + date + " 随机数:" + radom , Toast.LENGT