1. Introduction
SharedPreferences is a lightweight data storage method that stores data in an XML file through key-value key-value pairs, and is often used to store simple configuration information.
2. How to use
2.1 Get SharedPreferences object
SharedPreferences objects can be obtained in Android in the following three ways:
2.2.1 getSharedPreferences() in the Context class
Receive two parameters. The first parameter specifies the file to store data. If the specified file does not exist, create a new file and store the directory "/data/data/package_name/shared_prefs/", where package_name is the package name.
The second parameter is the operating mode, and there are two main types:
MODE_PRIVATE
: Private mode, the mode by default, is the same as the effect of passing 0 as a parameter, indicating that only the current program can operate on this file.
MODE_MULTI_PROCESS
: Multi-process mode, allowing multiple processes to operate on the file.
2.2.2 getPreferences() in the Activity class
This method is similar to the previous method, except that it only receives one parameter to specify the operation mode without specifying the file name. This method defaults to the class name of the current Activity as the file name for storing data.
2.2.3 getDefaultSharedPreferences() in PreferenceManager class
This is a static method that takes a Context parameter, using the current application's package name as the file name for storing the data.
2.2 Get the object
The SharedPreferences object itself can only read but not save data. If you need to save data, you must call the edit() method of the SharedPreferences object to get an Editor object.
2.3 Store data through putXxx method
After obtaining the Editor object, you can call its putXxx method to add data. Here, Xxx refers to the added data type. For example, if you store string data, you call the putString() method. This method receives two parameters, the first parameter is the key value and the second parameter is the value of the data, that is, a key-value pair.
2.4 Submit changes
After adding or removing (remove method) data, you need to call the commit() method of the Editor object to submit the changes.
2.5 Get stored data
It is relatively simple to obtain the stored data. You can directly call the getXxx method of the SharedPreferences object. The usage method is similar to putXxx of the Editor object. This method also receives two parameters. The first parameter specifies the key value of the data to be retrieved, and the second parameter specifies the default value returned when the retrieved data does not exist.
3. Example - Implement the function of saving username
layout:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:andro xmlns:tools="/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center_horizontal" android:orientation="vertical" tools:context=""> <!--username--> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center" android:text="username" /> <EditText android: android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="4" /> </LinearLayout> <!--password--> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center" android:text="password" /> <EditText android: android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="4" android:inputType="textPassword" /> </LinearLayout> <!--是否记住username--> <CheckBox android: android:layout_width="wrap_content" android:layout_height="wrap_content" android:checked="false" android:text="Remember username" /> <!--Log in--> <Button android: android:layout_width="200dp" android:layout_height="35dp" android:text="Log in" android:textSize="12sp" /> </LinearLayout>
Activity category:
public class MainActivity extends Activity implements { private SharedPreferences mPref; private mEditor; private EditText mUserName; private EditText mPassword; private CheckBox mIsRemember; private Button mLogin; @Override protected void onCreate(Bundle savedInstanceState) { (savedInstanceState); setContentView(.activity_main); init(); } private void init() { mUserName = (EditText) findViewById(); mPassword = (EditText) findViewById(); mIsRemember = (CheckBox) findViewById(); mLogin = (Button) findViewById(); (this); mPref = getSharedPreferences("user_data", MODE_PRIVATE); mEditor = (); //If you have set a remembered username before, read and set the username if (("is_remember", false)) { (("user_name", "")); } } @Override public void onClick(View v) { switch (()) { case : String userName = ().toString().trim(); String password = ().toString().trim(); //Test account if ("admin".equals(userName) && "123456".equals(password)) { (this, "Login successfully!", Toast.LENGTH_SHORT).show(); //If you check Remember username, save the data if (()) { ("user_name", userName); ("is_remember", true); (); } } else { (this, "Incorrect username or password!", Toast.LENGTH_SHORT).show(); } break; } } }
This article is for learning and communication purposes. If there are any mistakes, please feel free to correct them! I hope it will be helpful to everyone's learning and I hope everyone will support me more.