package com.example.sharedpreferences; import java.text.SimpleDateFormat; import java.util.Date; 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.TextView; public class MainActivity extends Activity { TextView tv; Button read,write; SharedPreferences preferences; SharedPreferences.Editor editor; @SuppressWarnings("deprecation") @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tv = (TextView)findViewById(R.id.tv); read = (Button)findViewById(R.id.read); write = (Button)findViewById(R.id.write); //声明preferences,通过getSharedPreferences方法得到,参数两个 preferences = getSharedPreferences("test", MODE_WORLD_READABLE); //声明editor,通过preferences.edit()方法得到 editor = preferences.edit(); read.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub //得到日期 String time = preferences.getString("time", null); //得到随机数 int randNum = preferences.getInt("random", 0); String result = time == null ? "未写数据" : "the write time" + time +"\n the randNum:" +randNum; tv.setText(result); } }); write.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub SimpleDateFormat sdf = new SimpleDateFormat("yyyy年mm月dd日" +"hh:mm:ss"); //放入日期 editor.putString("time", sdf.format(new Date())); //放入一个随机数 editor.putInt("random", (int)(Math.random()*100)); //提交 editor.commit(); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.activity_main, menu); return true; } }
package com.example.sharedpreferences; import java.text.SimpleDateFormat; import java.util.Date; 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.TextView; public class MainActivity extends Activity { TextView tv; Button read,write; SharedPreferences preferences; SharedPreferences.Editor editor; @SuppressWarnings("deprecation") @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tv = (TextView)findViewById(R.id.tv); read = (Button)findViewById(R.id.read); write = (Button)findViewById(R.id.write); //声明preferences,通过getSharedPreferences方法得到,参数两个 preferences = getSharedPreferences("test", MODE_WORLD_READABLE); //声明editor,通过preferences.edit()方法得到 editor = preferences.edit(); read.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub //得到日期 String time = preferences.getString("time", null); //得到随机数 int randNum = preferences.getInt("random", 0); String result = time == null ? "未写数据" : "the write time" + time +"\n the randNum:" +randNum; tv.setText(result); } }); write.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub SimpleDateFormat sdf = new SimpleDateFormat("yyyy年mm月dd日" +"hh:mm:ss"); //放入日期 editor.putString("time", sdf.format(new Date())); //放入一个随机数 editor.putInt("random", (int)(Math.random()*100)); //提交 editor.commit(); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.activity_main, menu); return true; } }
package com.example.sharedpreferences; import java.text.SimpleDateFormat; import java.util.Date; 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.TextView; public class MainActivity extends Activity { TextView tv; Button read,write; SharedPreferences preferences; SharedPreferences.Editor editor; @SuppressWarnings("deprecation") @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tv = (TextView)findViewById(R.id.tv); read = (Button)findViewById(R.id.read); write = (Button)findViewById(R.id.write); //声明preferences,通过getSharedPreferences方法得到,参数两个 preferences = getSharedPreferences("test", MODE_WORLD_READABLE); //声明editor,通过preferences.edit()方法得到 editor = preferences.edit(); read.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub //得到日期 String time = preferences.getString("time", null); //得到随机数 int randNum = preferences.getInt("random", 0); String result = time == null ? "未写数据" : "the write time" + time +"\n the randNum:" +randNum; tv.setText(result); } }); write.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub SimpleDateFormat sdf = new SimpleDateFormat("yyyy年mm月dd日" +"hh:mm:ss"); //放入日期 editor.putString("time", sdf.format(new Date())); //放入一个随机数 editor.putInt("random", (int)(Math.random()*100)); //提交 editor.commit(); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.activity_main, menu); return true; } }