Copy codeThe Code is as follows: public class MainActivity extends Activity {
@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. activity_main );
}
Public void onClick (View v ){
Switch (v. getId ()){
Case R. id. button1:
SharedPreferences sp = this. getSharedPreferences ("myxml", Context. MODE_PRIVATE );
Editor editor = sp. edit ();
Editor. putString ("name", "nanguabing ");
Editor. putInt ("age", 20 );
// Persistently operate on storage devices
Editor. commit ();
Toast. makeText (this, "Write sp completed", 1). show ();
Break;
Case R. id. button2:
SharedPreferences sp1 = this. getSharedPreferences ("myxml", Context. MODE_PRIVATE );
String name = sp1.getString ("name", "nobody ");
Int age = sp1.getInt ("age", 0 );
Toast. makeText (this, "name =" + name + ", age =" + age, 1). show ();
Break;
Default:
Break;
}
}
@ 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;
}
}
File Format:Copy codeThe Code is as follows: <? Xml version = '1. 0' encoding = 'utf-8' standalone = 'Yes'?>
<Map>
<String name = "name"> nanguabing </string>
<Int name = "age" value = "20"/>
</Map>