Android uses SharedPreferences to save user preferences

Source: Internet
Author: User

In Android applications, we often need to record some user-defined preference parameters. In this case, we need to use SharedPreferences and Editor to save the information and read it at next login.

SharedPreferences stores data similar to the configuration information format, so it stores the data in the form of a key-value pair. Let's take a look at the instance code.

The first is the interface layout, which is relatively simple. It is a common login interface.

1 <RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android" 2 xmlns: tools = "http://schemas.android.com/tools" 3 android: layout_width = "match_parent" 4 android: layout_height = "match_parent" 5 android: paddingBottom = "@ dimen/activity_vertical_margin" 6 android: paddingLeft = "@ dimen/users" 7 android: paddingRight = "@ dimen/activity_horizontal_margin" 8 android: paddingTop = "@ dimen/activity_vertical_margin" 9 tools: context = ". mainActivity "> 10 <EditText 11 android: layout_width =" fill_parent "12 android: layout_height =" wrap_content "13 android: id = "@ + id/account" 14/> 15 <EditText 16 android: layout_width = "fill_parent" 17 android: layout_height = "wrap_content" 18 android: id = "@ + id/password" 19 android: layout_below = "@ id/account" 20/> 21 <Button 22 android: layout_width = "fill_parent" 23 android: layout_height = "wrap_content" 24 android: layout_below = "@ id/password" 25 android: text = "save parameter" 26 android: id = "@ + id/save" 27 android: onClick = "save" 28/> 29 </RelativeLayout>

This is a custom Preferences class used to save data and generate a file in the Android internal bucket.

1 import android. r. integer; 2 import android. content. context; 3 import android. content. sharedPreferences; 4 import android. content. sharedPreferences. editor; 5 import android. widget. editText; 6 7 public class Preferences {8 9 private Context context; 10 public Preferences (Context context) 11 {12 this. context = context; 13} 14 15 16 public void save (String name, Integer valueOf) 17 {18 // save the file name as "shared" and save it as Context. MODE_PRIVATE: this data can only be read by this application 19 SharedPreferences preferences = context. getSharedPreferences ("shared", Context. MODE_PRIVATE); 20 21 Editor editor = preferences. edit (); 22 editor. putString ("name", name); 23 editor. putInt ("age", valueOf); 24 25 editor. commit (); // submit data 26} 27 28 29}

 

The following is the Mainactivity code. In the oncreate stage of the activity, we load local data.

Import java. util. hashMap; import java. util. map; import android. r. integer; import android. OS. bundle; import android. app. activity; import android. content. sharedPreferences; import android. view. menu; import android. view. view; import android. widget. button; import android. widget. editText; import android. widget. toast; public class MainActivity extends Activity {private EditText account, passworad; Preferences prefer; // custom class SharedPreferences preference;
@ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); account = (EditText) findViewById (R. id. account); passworad = (EditText) findViewById (R. id. password); // obtain the local data preference = getSharedPreferences ("shared", MODE_PRIVATE); Map <String, String> map = new HashMap <String, String> (); map. put ("name", preference. getString ("name", ""); map. put ("age", String. valueOf (preference. getInt ("age", 0); account. setText (map. get ("name"); passworad. setText (map. get ("age");} // Method for saving the file public void save (View v) {String name = account. getText (). toString (); String age = passworad. getText (). toString (); prefer = new Preferences (this); prefer. save (name, Integer. valueOf (age); Toast. makeText (getApplicationContext (), "saved", Toast. LENGTH_SHORT ). show () ;}@ Override public boolean onCreateOptionsMenu (Menu menu) {// Inflate the menu; this adds items to the action bar if it is present. getMenuInflater (). inflate (R. menu. main, menu); return true ;}}

Let's take a look at the results.

Click "Save Parameters". The saved parameters are successfully saved. You can see that these parameters are still in the next logon. Because the record file is in the built-in space, we cannot find the file in the SD card,

If you have the root permission, you can download a RE file for management. We can find the built-in folders of many applications in the/data/path, we can see a shared_prefs folder in these folders,

There is an xml file that we just set.

 

Related Article

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.