Wirelessly sharedpreferences can be used to save some very small data, such as an application account information, such as our QQ login to remember the account and password this function is achieved through this. If you don't tell us about this, just record how to use it.
Implementation steps:
- Get the Sharedpreferences object through the context
- Get the Editor object through the editor () method
- Operation of adding and deleting data by using Editor object for key value
- Submitting data through the commit () method
The above four steps, if just get the data, as long as the first step to get the Sharedpreferences object can be called method to get the data. The following three steps are required to store and delete data
First, the simple implementation of the effect:
The layout is relatively simple and has been skipped.
Here is the code for the Java implementation
Package Com.contentprovide.liuliu.shard;import Android.content.sharedpreferences;import Android.support.v7.app.appcompatactivity;import Android.os.bundle;import Android.view.view;import Android.widget.button;import Android.widget.edittext;import Android.widget.TextView; Public classMainactivity extends Appcompatactivity {Button save_btn,get_btn; EditText et; TextView TV; @Overrideprotected voidonCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); SAVE_BTN=(Button) Findviewbyid (R.ID.SAVE_BTN); GET_BTN=(Button) Findviewbyid (R.ID.GET_BTN); ET=(EditText) Findviewbyid (r.id.et); TV=(TextView) Findviewbyid (r.id.tv); Save_btn.setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (view view) {//get the content in the input boxString s =Et.gettext (). toString ();//Get Sharedpreferences ObjectSharedpreferences sharedpreferences = mainactivity. This. Getsharedpreferences ("Myshard", Mainactivity. This. Mode_private);//manipulating data through the editor objectSharedpreferences.editor Editor =Sharedpreferences.edit (); Editor.putstring ("Usename", s); Editor.commit (); } }); Get_btn.setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (view view) {String s=""; Sharedpreferences sharedpreferences= Mainactivity. This. Getsharedpreferences ("Myshard", Mainactivity. This. Mode_private);//get data because you don't need to make changes to the data, so you don't need an editor object, you can get it with sharedpreferencess = sharedpreferences.getstring ("Usename","Default Data"); Tv.settext (s); } }); }}
Shardprefences itself is an excuse, so it is not possible to create objects directly, only the Getsharedprederences () method provided by the context to get the object.
The first parameter is a custom name, the second parameter is to indicate that the sharedprefences data can only be read and written by this application, and there are two other modes because it is not recommended, it does not log
Getsharedpreferences ("Myshard", MainActivity.this.MODE_PRIVATE);
The main way to store data: The key value of the main method to store, the first parameter is key, the second parameter is to store the content
Putstring ("," "");
Putint ("," "");
Pubbooean ("," "");
The main way to get the data: mainly through the method of key-value pairs, the first parameter is key, the second default data, the data is not obtained in the case of the default data display:
GetString ("," "");
Getint ("," "");
Getbooean ("," "");
Use of Android Sharedpreferences