Analysis of the use of Android sharedpreferences _android

Source: Internet
Author: User

Sharedpreferences
It is used to provide the software parameter setting when the software is developed, the XML file is used to store the data, and the file is stored in the/data/data/<package name>/shared_prefs directory;

Copy Code code 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 name of the filename because the system knows it is an XML file and Android is automatically added;
Editor Editor = Preferences.edit ();//data stored in memory;
Editor.putstring ("name", name);
Editor.putint (' age ', age);
Editor.commit ()//must use this method to submit the intrinsic 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 an XML suffix name because the system knows it is an XML file;
Map.put ("Name", Preferences.getstring ("name", "Default value"));//If no argument 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 name of the activity is used as the name of the XML;

Preferencemanager.getdefaultsharedpreferences (context);

******************************************************

android:onclick= "Save" <!--is used to specify a method name, which needs to be defined in the acvitity that displays the interface, and requires the Save method to be the same as its own OnClick signature, as follows! -->

View.onclicklistener () {

public void OnClick (View v) {}

}

The definition in public void Save (View v) {}//activity;

android:numeric= "integer" <!--set Numeric type-->

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.