SharedPreferences
It is used to provide software parameter settings when developing software. The xml file is used to store data, and the file is stored in the/data/<package name>/shared_prefs directory;
Copy codeThe Code is as follows:
Public void savePreferences (String name, Integer age ){
--> Get SharedPreferences
SharedPreferences preferences = context. getSharedPreferences ("itcase", Context. MODE_PRIVATE );
// You do not need to specify the xml Suffix of the file name. Because the system knows that it is an xml file, android will automatically add it;
Editor editor = preferences. edit (); // the data is stored in the memory;
Editor. putString ("name", name );
Editor. putInt ("age", age );
Editor. commit (); // This method must be used to submit internal data back to the file
}Public Map <String, String> getPreferences (){
Map <String, String> map = new HashMap <String, String> ();
SharedPreferences preferences = context. getSharedPreferences ("itcase ",
Context. MODE_PRIVATE); // you do not need to specify the xml extension name because the system knows it is an xml file;
Map. put ("name", preferences. getString ("name", "default"); // if no parameter exists, the default value is returned.
Map. put ("age", String. valueOf (preferences. getInt ("age", 0 )));
Return map;
}
--> Get SharedPreferences
// This. getPreferences (mode); // by default, the activity name is used as the xml name;
// PreferenceManager. getdefasharsharedpreferences (Context );
**************************************** **************
Android: onClick = "save" <! -- This parameter is used to specify a method name. It must be defined in acvitity on the displayed interface. The save method must be the same as the Onclick signature, as shown below! -->
View. OnClickListener (){
Public void onClick (View v ){}
}
Public void save (View v) {}// definition in Activity;
Android: numeric = "integer" <! -- Set the value type -->