I. Storage type: sharedpreferences storage
Two. Sharedpreferences Storage
1. Features
① stores single data, such as numeric, String, Boolean
② file:/date/date/package name/shared_prefs/xxx.xml: <map><string name= "Key" >value</string></map>
③ stored as key-value pairs
④ can be set to not be manipulated by other apps
2.API
(1) sharedpreferences
① Get Instance context.getsharedpreferences ():
1) name store file name; 2) mode operation: Mode_private cannot be accessed by other application, overwrite mode, mode_append cannot be accessed by other application, append mode;
② Startup editor: Edit (): Back to Editor
③ read value:1) getString (key,defvalue default) 2) GetAll () returns a map collection of all key-value pairs
(2) Editor
① Storage data: 1) putstring (key,value) 2) Putlong (key,value) 3) Putint 4) putfloat 5) Putboolean 6) Putstringset
② Commit Storage
③clear () Clear
④remove (String key) removes the key value pair of the instruction key
1<?xml version= "1.0" encoding= "Utf-8"?>2<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"3Xmlns:tools= "Http://schemas.android.com/tools"4Android:layout_width= "Match_parent"5android:layout_height= "Match_parent"6android:paddingbottom= "@dimen/activity_vertical_margin"7android:paddingleft= "@dimen/activity_horizontal_margin"8android:paddingright= "@dimen/activity_horizontal_margin"9android:paddingtop= "@dimen/activity_vertical_margin"Tentools:context= "Com.hanqi.testapp3.MainActivity" Oneandroid:orientation= "Vertical" > A -<TextView -Android:layout_width= "Wrap_content" theandroid:layout_height= "Wrap_content" -android:text= "Hello world!"/> - -<Button +Android:layout_width= "Match_parent" -android:layout_height= "Wrap_content" +android:text= "SP Storage" Aandroid:onclick= "Bt_onclick"/> at -<Button -Android:layout_width= "Match_parent" -android:layout_height= "Wrap_content" -android:text= "SP read" -android:onclick= "Bt1_onclick"/> in -</LinearLayout>
. XML
1 Packagecom.hanqi.testapp3;2 3 Importandroid.content.SharedPreferences;4 ImportAndroid.os.Bundle;5 Importandroid.support.v7.app.AppCompatActivity;6 ImportAndroid.view.View;7 ImportAndroid.widget.Toast;8 9 Public classMainactivityextendsappcompatactivity {Ten One @Override A protected voidonCreate (Bundle savedinstancestate) { - Super. OnCreate (savedinstancestate); - Setcontentview (r.layout.activity_main); the } - - Public voidBt_onclick (View v) - { + //1. Get Sharedpreferences Object -Sharedpreferences sharedpreferences=getsharedpreferences ("abc"), mode_append); + A //2. Get the editor atSharedpreferences.editor editor=Sharedpreferences.edit (); - - //3. Adding Data Using editor - //editor.putstring ("B", "xxxxxx"); - //Editor.putlong ("Long", 123456); - inEditor.remove ("a"); - to + //4. Submit a Save - editor.commit (); the *Toast.maketext (mainactivity. This, "Save data successfully", Toast.length_short). Show (); $ }Panax Notoginseng - //Read the Public voidBt1_onclick (View v) + { ASharedpreferences sp=getsharedpreferences ("abc"), mode_private); the +String str=sp.getstring ("AB", "Default value")); - $Toast.maketext (mainactivity. This, "key=b" + "value=" +str, toast.length_short). Show (); $ } -}
. Java
Data storage--SP Storage