public class Mainactivity extends Activity {
Private EditText et;
Private Button BT;
Private TextView TV;
Sharedpreferences preferences;
Sharedpreferences.editor Editor;
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
ET = (EditText) Findviewbyid (R.ID.EDITTEXT1);
BT = (Button) Findviewbyid (R.id.button1);
TV = (TextView) Findviewbyid (R.ID.TEXTVIEW1);
Preferences = Getsharedpreferences ("Crazyit", mode_world_readable);
Editor = Preferences.edit ();
Bt.setonclicklistener (New Onclicklistener () {
@Override
public void OnClick (View arg0) {
Reading a string
String time = preferences.getstring ("time", null);
int randnum = Preferences.getint ("random", 0);
String result = time = = null? "You have not yet written the data": "Write Time is" + "
+ "\ n the last generated random number is" + randnum;
Toast.maketext (mainactivity.this, result, Toast.length_short). Show ();
}
});
Tv.setonclicklistener (New Onclicklistener () {
@Override
public void OnClick (View v) {
SimpleDateFormat ADF = new SimpleDateFormat ("yyyy mm month DD Day Hh:mm:ss");
Editor.putstring ("Time", Adf.format (New Date ()));
Editor.putint ("random", (int) (Math.random () *100));
Editor.commit ();
}
});
}
}
Use of Android Sharedpreferences