How to Use SharedPreference to save files for Android storage Learning

Source: Internet
Author: User

How to Use SharedPreference to save files for Android storage Learning

In the last two sections, we used text files to store user information, which is obviously vulnerable. At the same time, it is difficult to manage the content in the file. Today, we learn to use SharedPreference to save. SharedPreference stores scattered data.

We still use the example in the section to analyze and save the user information using SharedPreference.

Note: If you do not know the example, read and write files in the internal storage of Android storage.

Click OK to save the user information:

 

Public void login (View v) {String name = ed_nam.getText (). toString (); String passwd = ed_passwd.getText (). toString (); // determines whether the user name or password is input if (name. equals () | (passwd. equals () {Toast. makeText (this, the user name or password cannot be blank, Toast. LENGTH_SHORT ). show ();} else {if (cb. isChecked () {// use sharedPreference to save user information. // The default path of SharedPreference is/cmd_prefs/SharedPreferences sp = getSharedPreferences (info, MODE_PRIVATE); // get the Editor editor Editor = sp. edit (); editor. putString (name, name); editor. putString (passwd, passwd); // submit the editor. commit ();} Toast. makeText (this, logon successful, Toast. LENGTH_SHORT ). show ();}}

 

You can see that there is an info file in the sharedPreference path.

Export. Info. xml is saved as key and value

 

 
1233333 Ert

 

When you come in again, you can also display the user information:

 

Public void readInfo () {// get the content in the info file SharedPreferences sP = getSharedPreferences (info, MODE_PRIVATE); ed_nam.setText (sP. getString (name,); ed_passwd.setText (sP. getString (passwd ,));}
Display Effect:

 



 

 

 

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.