main.xml

        

SharedPreferencesTest.java

package com.example.sharedpreferences;import java.text.SimpleDateFormat;import java.util.Date;import java.util.Locale;import android.os.Bundle;import android.app.Activity;import android.content.SharedPreferences;import android.view.Menu;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.Toast;public class SharedPreferencesTest extends Activity {SharedPreferences preferences;SharedPreferences.Editor editor;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);preferences = getSharedPreferences("crazyit", MODE_WORLD_READABLE);editor = preferences.edit();Button read = (Button)findViewById(R.id.read);Button write = (Button)findViewById(R.id.write);read.setOnClickListener(new OnClickListener(){@Overridepublic void onClick(View arg0) {// TODO Auto-generated method stubString time = preferences.getString("time", null);int randNum = preferences.getInt("random", 0);String result = time == null? "您暂时还未写入数据":"写入时间为:"+ time + "\n上次生成的随机数为:" + randNum;Toast.makeText(SharedPreferencesTest.this, result, 5000).show();}});write.setOnClickListener(new OnClickListener(){@Overridepublic void onClick(View arg0) {// TODO Auto-generated method stubSimpleDateFormat time = new SimpleDateFormat("yyyy年MM月dd日 "+ "hh:mm:ss a", Locale.ENGLISH);editor.putString("time", time.format(new Date()));editor.putInt("random", (int)(Math.random() * 100));editor.commit();}});}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.shared_preferences_test, menu);return true;}}


更多相关文章

  1. 数据存储之——Android文件存储系统及文件系统(Android(安卓)Q)
  2. 每日一道Android(安卓)面试题,面试途中不败题
  3. 数据存储之——Android内、外存储分区&常用存储目录详解(Android(
  4. Android基础笔记(二)-数据存储和界面展现
  5. 2011Android技术面试整理附有详细答案(包括百度、新浪、中科软等
  6. 对Android及移动互联网的大局观看法!
  7. 对Android及移动互联网的大局观看法!
  8. Android串口通信:抱歉,学会它真的可以为所欲为
  9. Android的数据处理:使用annotation实现JSON字符串解析为java对象

随机推荐

  1. Android(安卓)面试题(2)
  2. android中Parcelable接口的使用
  3. android中下载文件到sdcard和进度条小结
  4. Android(安卓)ImageView图片显示点击背景
  5. Android(安卓)支持网络协议以及简单用法
  6. android开发资料(DEVELOPER.ANDROID.COM/
  7. xmlns:android的作用
  8. Android(安卓)Studio 导入so
  9. 利用BLCR加快android的启动过程
  10. Android(安卓)修改开机动画(bootanimation