Android Learning-data storage and io-sharedpreferences
After running, you can see this XML file in Ddms.
Mainactivity.java
Public classMainactivityextendsActivityImplementsonclicklistener{EditText EditText1; TextView textView3; Sharedpreferences Pre; Editor editor; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); EditText1=(EditText) Findviewbyid (R.ID.EDITTEXT1); TEXTVIEW3=(TextView) Findviewbyid (R.ID.TEXTVIEW3); Pre=getsharedpreferences ("myPre222", context.mode_world_readable+context.mode_world_writeable); Editor=Pre.edit (); Button BTN1=(Button) Findviewbyid (R.id.button1); Button btn2=(Button) Findviewbyid (R.id.button2); Btn1.setonclicklistener ( This); Btn2.setonclicklistener ( This); } Public voidOnClick (View v) {if(v==Findviewbyid (R.id.button1)) {editor.putstring ("Name", Edittext1.gettext () + ""); Editor.commit (); }Else if(v==Findviewbyid (R.id.button2)) {String name=pre.getstring ("name", "" "); Textview3.settext (name); } }}View Code
Activity_main.xml
<Relativelayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:tools= "Http://schemas.android.com/tools"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"Tools:context= "${relativepackage}.${activityclass}" > <TextViewAndroid:id= "@+id/textview1"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:layout_alignbaseline= "@+id/edittext1"Android:layout_alignbottom= "@+id/edittext1"Android:layout_alignparentleft= "true"Android:text= "Name"android:textappearance= "? Android:attr/textappearancelarge" /> <EditTextAndroid:id= "@+id/edittext1"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:layout_alignparenttop= "true"Android:layout_marginleft= "15DP"Android:layout_margintop= "15DP"Android:layout_torightof= "@+id/textview1"Android:ems= "Ten" > <Requestfocus/> </EditText> <ButtonAndroid:id= "@+id/button1"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:layout_alignparentleft= "true"Android:layout_below= "@+id/edittext1"Android:layout_margintop= "22DP"Android:text= "Write" /> <ButtonAndroid:id= "@+id/button2"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:layout_alignparentleft= "true"Android:layout_below= "@+id/button1"Android:layout_margintop= "36DP"Android:text= "read" /> <TextViewAndroid:id= "@+id/textview2"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:layout_alignbaseline= "@+id/button2"Android:layout_alignbottom= "@+id/button2"Android:layout_marginleft= "11DP"Android:layout_torightof= "@+id/button2"Android:text= "Value:"android:textappearance= "? Android:attr/textappearancelarge" /> <TextViewAndroid:id= "@+id/textview3"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:layout_alignbaseline= "@+id/textview2"Android:layout_alignbottom= "@+id/textview2"Android:layout_centerhorizontal= "true"android:textappearance= "? Android:attr/textappearancelarge" /></Relativelayout>View Code
Other applications to read this XML
Try{Context Context=createpackagecontext ("Com.example.fffff", context.context_ignore_security); Sharedpreferences Pre=context.getsharedpreferences ("myPre222", context.mode_world_readable+context.mode_world_writeable); String name=pre.getstring ("name", "" "); Toast.maketext (mainactivity. This, Name, Toast.length_short). Show (); } Catch(namenotfoundexception e) {//TODO Auto-generated catch blockE.printstacktrace (); }View Code
Mode_world_readable and mode_world_writeable have been deprecated in Android 4.2 (API level 17) because of the danger and low security.
Android Learning-data storage and io-sharedpreferences