Android development-Introduction to SharedPreferences

Source: Internet
Author: User

Data storage is an important feature in Android development. The following describes how to store key-value pairs using XML files. SharedPreferences can be used in four steps: 1) Get SharedPreferences object 2) Get SharedPrefercences. editor object 3) Use putXXX to save the data 4) Save the data in the file the following example uses SharedPreferences to store simple data and the stored content is test. xml. The content of the file is as follows: [html] www.2cto.com <? Xml version = '1. 0' encoding = 'utf-8' standalone = 'Yes'?> <Map> <string name = "name"> zhanghu </string> <string name = "habit"> android, surfing, playing basketball </string> </map> is implemented below: The specific implementation code is as follows: [java] public class SharedPreferences_Activity extends Activity {private EditText editText1, editText2, editText3, editText4; private Button button1, button2; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_shared_preferences _); editText1 = (EditText) findViewById (R. id. editname); editText2 = (EditText) findViewById (R. id. edithabbit); editText3 = (EditText) findViewById (R. id. editname2); editText4 = (EditText) findViewById (R. id. edithabbit2); button1 = (Button) findViewById (R. id. commit); button2 = (Button) findViewById (R. id. display); button1.setOnClickListener (new OnClickListener () {@ Override public void onClick (View v) {// TODO Auto-generated method stub // get the SharedPreferences object (step 1) sharedPreferences mySharedPreferences = getSharedPreferences ("test", Activity. MODE_PRIVATE); // obtain SharedPrefercences. editor object (step 2) SharedPreferences. editor editor = mySharedPreferences. edit (); // use putXXX to save the data (Step 3) editor. putString ("name", editText1.getText (). toString (); editor. putString ("habit", editText2.getText (). toString (); // Save the data in the file (step 4) editor. commit (); editText1.setText (""); editText2.setText ("") ;}}); button2.setOnClickListener (new OnClickListener () {@ Override public void onClick (View v) {// TODO Auto-generated method stub SharedPreferences sharedPreferences = getSharedPreferences ("test", Activity. MODE_PRIVATE); String nameString = sharedPreferences. getString ("name", ""); String habitString = sharedPreferences. getString ("habit", ""); editText3.setText (nameString); editText4.setText (habitString) ;}}@ Override public boolean onCreateOptionsMenu (Menu menu) {// Inflate the menu; this adds items to the action bar if it is present. getMenuInflater (). inflate (R. menu. activity_shared_preferences _, menu); return true ;}}

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.