The Android system provides several ways to store data by default:
- Shared Preferences
- Internal storage
- External storage
- SQLite database
- Save to network server
Using the shared Preferences
The Shared preferences class is primarily used to hold the data type of a key-value pair. We can use it to save some simple data types.
There are two ways to get sharedpreferences objects:
- Through the Getsharedpreferences () method
- Through the Getpreferences () method
The main differences between the above two methods are:
The first method can create multiple files to hold the data
The second method can only create one file to save data
Let's start by creating a layout like this:
Layout code:
<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools " android:layout_width=" match_parent " android:layout_height=" Match_parent " android:orientation= "vertical" ><edittext android:id= "@+id/username" android:layout_width= " Fill_parent " android:layout_height=" wrap_content "/><edittext android:id=" @+id/password " Android:layout_width= "Fill_parent" android:layout_height= "wrap_content"/><button android:id= "@+ Id/write " android:layout_width=" wrap_content " android:layout_height=" Wrap_content " android: Layout_gravity= "right" android:text= "write to File"/> </LinearLayout>
Once the layout is ready, you can get the Sharedpreferences object to write to the data:
Package Com.whathecode.storageoptinos;import Android.app.activity;import Android.content.sharedpreferences;import Android.os.bundle;import Android.view.view;import Android.widget.button;import Android.widget.EditText;import Android.widget.toast;public class Mainactivity extends activity{@Overrideprotected void OnCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); final EditText Namefield = (EditText) Findviewbyid (r.id.username); final EditText Agefield = (EditText) Findviewbyid (r.id.age); Button Btnwrite = (button) Findviewbyid (r.id.write);//Get Sharedpreferences object, file permission is private sharedpreferences SPF = Getsharedpreferences ("Record", mode_private);//Get editor final Sharedpreferences.editor editor = Spf.edit (); Btnwrite.setonclicklistener (New View.onclicklistener () {@Overridepublic void OnClick (View v) {String username = Namefield.gettext (). toString (); String age = Agefield.gettext (). toString (); Editor.putstring ("username", username); Editor.putstring ("Age", age), if (Editor.commit ())//When the data is successfully written back True{toast.maketext (Getbasecontext (), "file written", Toast.length_short). Show ();}}});}}
In the above code, we mainly use Sharedpreferences's internal class editor class
We mainly use several put* methods of this class
When these methods are called, we can start writing the data using the commit () or the Apply () method.
It is important to note that when multiple editor writes data to the same file, the result of an editor after completion overwrites the previous one.
The difference between the commit and the Apply method is that the commit write successfully returns TRUE, and apply does not return a value.
You can call the Apply () method to write the data when we don't care about the return value.
Operation Result:
After the data is written, we can see if there is a write success in Eclipse's DDMS attempt
The data is saved in the/data/data/package name/shared_prefs Directory
How to store data under Android