I. Overview
Sharedpreferences can be understood as a simple database that stores data in the system as files or reads data, however, it only stores some simple numeric pairs, such as key-values. If some applications need to save some simple data when they exit to restore the required data when they are opened again, sharedpreferences can be used.
Ii. Requirements
Write a simple application that requires the application to save some required data when exiting, and restore the data when the application is opened again.
Iii. Implementation
Create a project myshared, modify the/RES/layout/Main. xml file, and add an edittext, two buttons, and a textview. The complete main. xml file is as follows:
1 <? XML version = "1.0" encoding = "UTF-8"?>
2 <linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android"
3 Android: layout_width = "fill_parent"
4 Android: layout_height = "fill_parent"
5 Android: Orientation = "vertical">
6
7 <edittext
8 Android: Id = "@ + ID/edittext"
9 Android: layout_width = "fill_parent"
10 Android: layout_height = "wrap_content"
11 Android: hint = "Enter content"
12/>
13
14 <button
15 Android: Id = "@ + ID/wbutton"
16 Android: layout_width = "fill_parent"
17 Android: layout_height = "wrap_content"
18 Android: text = "write"
19/>
20
21 <button
22 Android: Id = "@ + ID/rbutton"
23 Android: layout_width = "fill_parent"
24 Android: layout_height = "wrap_content"
25 Android: text = "read"
26/>
27
28 <textview
29 Android: Id = "@ + ID/textview"
30 Android: layout_width = "fill_parent"
31 Android: layout_height = "wrap_content"
32 Android: gravity = "center_horizontal"
33 Android: textsize = "20dip"
34/>
35
36 </linearlayout>
Next, modify the mysharedactivity. Java file to define a sharedpreferences object and a sharedpreferences. Editor object to monitor two buttons. The complete mysharedactivity. Java file is as follows:
1 package com. Nan. shared;
2
3 Import Android. App. activity;
4 Import Android. content. sharedpreferences;
5 import Android. OS. Bundle;
6 Import Android. View. view;
7 Import Android. widget. Button;
8 Import Android. widget. edittext;
9 Import Android. widget. textview;
10 Import Android. widget. Toast;
11
12 public class mysharedactivity extends Activity
13 {
14 private edittext medittext = NULL;
15 private button writebutton = NULL;
16 private button readbutton = NULL;
17 private textview mtextview = NULL;
18
19 private sharedpreferences msharedpreferences = NULL;
20 private sharedpreferences. Editor meditor = NULL;
21
22/** called when the activity is first created .*/
23 @ override
24 public void oncreate (bundle savedinstancestate)
25 {
26 super. oncreate (savedinstancestate );
27 setcontentview (R. layout. Main );
28
29 medittext = (edittext) This. findviewbyid (R. Id. edittext );
30 writebutton = (button) This. findviewbyid (R. Id. wbutton );
31 readbutton = (button) This. findviewbyid (R. Id. rbutton );
32 mtextview = (textview) This. findviewbyid (R. Id. textview );
33
34 msharedpreferences = getsharedpreferences ("myshare", mode_private );
35 meditor = msharedpreferences. Edit ();
36
37 writebutton. setonclicklistener (New View. onclicklistener ()
38 {
39
40 @ override
41 public void onclick (view V)
42 {
43 // todo auto-generated method stub
44 // obtain edittext content
45 string text = medittext. gettext (). tostring ();
46 // modify
47 meditor. putstring ("hehe", text );
48 // submit the modification
49 meditor. Commit ();
50 displaytoast ("Write successful! ");
51}
52 });
53
54 readbutton. setonclicklistener (New View. onclicklistener ()
55 {
56
57 @ override
58 public void onclick (view V)
59 {
60 // todo auto-generated method stub
61 // obtain the stored data
62 string text = msharedpreferences. getstring ("hehe", null );
63 // display the data
64 mtextview. settext (text );
65}
66 });
67
68}
69 // display the toast Function
70 Private void displaytoast (string S)
71 {
72 toast. maketext (mysharedactivity. This, S, Toast. length_short). Show ();
73}
74
75}
Run it as follows:
Enter a row of numbers and click "write", as shown below:
Exit the application, run it again, and click "read". The effect is as follows:
The previously saved data is displayed.
OK.