Android: Data Storage Method-SharedPreference

Source: Internet
Author: User

Android: Data Storage Method-SharedPreference

SharedPreference:

1. It is a lightweight data storage method.

2. The essence is to store key_value pairs based on xml files.

3. It is usually used to store some simple configuration information (password, window Status, basic configuration of software, wallpaper, etc)

The SharedPreferences object can only obtain data, but does not support storage and modification. The storage and modification are implemented through the Editor object.

To save data using SharedPreferences, follow these steps:

1. Get the SharedPreferences object

There are two methods

(1) The SharedPreferences object obtained through the context. getsharedpreferences (string name, int mode) function can be shared by other components in the same application. Name is the name of the specified file, and mode is the operation mode. There are four types (Context. MODE_PRIVATE: the default operation mode. This mode indicates that the file is private data and can only be accessed by the application itself. In this mode, the written content will overwrite the content of the original file. Context. MODE_APPEND: the mode checks whether the file exists and appends the content to the file if it exists. Otherwise, a new file is created. MODE_WORLD_READABLE: indicates that the current file can be read by other applications. MODE_WORLD_WRITEABLE: indicates that the current file can be written by other applications .)

 

SharedPreferences pre = getSharedPreferences("myinfo", MODE_PRIVATE);

 

(2) Through the activity. getpreferences (int mode) function, the configuration file can only be used by the called activity. Mode is the operation mode.

 

2. Get the SharedPreferences. Editor object

 

SharedPreferences pre = getSharedPreferences("myinfo", MODE_PRIVATE);Editor edit=pre.edit();


 

3. Use the putXxx () method of the Editor interface to save the key-value. Xxx indicates different data types.

 

edit.putString("name", "zhangsan");edit.putString("age", "30");

After the storage is complete, do not forget to submit through the Editor's commit () method.

 

 

edit.commit();

4. Get Data

 

 

SharedPreferences pre = getSharedPreferences("myinfo", MODE_PRIVATE);pre.getString("name", "");pre.getString("age", "");
5. Remove Data

 

 

edit.remove("name");edit.remove("age");edit.commit();
I learned so much. An instance is attached below. Save user name and password

 

Running result

MainActivity. class

 

Package com. example. sharedpreferences; import android. OS. bundle; import android. app. activity; import android. content. sharedPreferences; import android. content. sharedPreferences. editor; import android. view. view; import android. widget. checkBox; import android. widget. editText; import android. widget. toast; public class MainActivity extends Activity {private EditText et_name; private EditText et_passward; private CheckBox ckb_save; private SharedPreferences pre; private Editor edit; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); et_name = (EditText) findViewById (R. id. et_name); et_passward = (EditText) findViewById (R. id. et_passward); ckb_save = (CheckBox) findViewById (R. id. checkBox1); pre = getSharedPreferences ("myinfo", MODE _ PRIVATE); edit = pre. edit (); String name = pre. getString ("name", ""); String passward = pre. getString ("passward", ""); if (name! = Null & passward! = Null) {et_name.setText (name); et_passward.setText (passward) ;}} public void btnLogin (View v) {switch (v. getId () {case R. id. btn_login: {String name = et_name.getText (). toString (); String passward = et_passward.getText (). toString (); if (name! = Null & passward! = Null) {if (ckb_save.isChecked () {edit. putString ("name", name); edit. putString ("passward", passward); edit. commit (); Toast. makeText (MainActivity. this, "saved user succeeded", Toast. LENGTH_SHORT ). show ();} else {Toast. makeText (MainActivity. this, "Save not checked, save failed", Toast. LENGTH_SHORT ). show () ;}} else {Toast. makeText (MainActivity. this, "the user name or password is empty. Please enter it again", Toast. LENGTH_SHORT ). show (); et_name.setText (null); et_passward.setText (null);} break;} case R. id. btn_cancel: {edit. remove ("name"); edit. remove ("passward"); edit. commit (); Toast. makeText (MainActivity. this, "forgot", Toast. LENGTH_SHORT ). show (); break ;}}}}

Layout File

 

 

     
      
      
      
      
      
  
  
  
  
 
 


 

 

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.