Android Login Username Remember password--preferences (sharedpreferences)

Source: Internet
Author: User
Tags gettext

sharedpreferences: Preference Settings

Preferences refer to "personalization", that is, the same software, the different users, the results are not the same, such as "whether to download the lyrics on WiFi" in a music player, "whether to turn on background music" in a game, "open between Effects", "Default font Size" In a news app ... 

First. Data with preferences is characterized by a small amount of data that can be described as K-V relational data is private in the application and may need to be saved or read two at any time without sharing to other applications . 

Second. Store data using preferences:

Invokes the Getsharedpreferences (String name, int mode) method of the context to get the Sharedpreferences object, where the 1th parameter represents the file name of the file that holds the preference, and does not need to specify an extension. When saved, the. XML is automatically added as an extension, and the 2nd parameter is fixed with a value context.mode_private

Call the Sharedpreferences object's edit () method to get the Sharedpreferences.editor object

Call the put??? of the Sharedpreferences.editor object (String key,??? value) method to write data

Invoke the Commit () method of the Sharedpreferences.editor object to commit the Write

Data retention with preferences is stored in the package/shared_prefs/folder of the/data/data/application and automatically created if the folder does not exist.

When the system settings-> application (Apps) finds an application that stores preferences, selecting clear data clears all preferences data, private files, databases, and logged in accounts. 

Third. Read the data in preferences:

Invokes the Getsharedpreferences (String name, int mode) method of the context to get the Sharedpreferences object, which can refer to saving the data

Call the Get??? of the Sharedpreferences object (String key,??? defvalue) method to read data

Note: When reading data in preferences, you can treat preferences as "unreliable" because the first run may not have saved the data, the data will not be read, or the user may be able to erase the data and not read the data while using the software. 

Fourth. Login remember username and password

public class MainActivity extends Activity {
    // Declare the control
    private Button btLog;
    private EditText etUserName;
    private EditText etPassWord;
    // declare variables
    private SharedPreferences preferences;

    protected void onCreate(Bundle savedInstanceState) {
        // Control initialization

        //Get the preference setting object: the first parameter is the table name, the second parameter is the permission

        //Get the user name and password data stored in the preferences
        String name=preferences.getString("name", "");
        String pass=preferences.getString("pass", "");

        //Put the data we stored in the two input boxes

        //Set the click event of the login button: save the entered data into the preferences
        btLog.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                //Save the data to the preferences
                //1. Get an edited object of preferences
                Editor ed=preferences.edit();
                //2. Editor.putString; put string
                    //2.1. Get the content of the input box
                    String username=etUserName.getText().toString().trim();
                    String password=etPassWord.getText().toString().trim();
                    //2.2. Put our data
                //3. Submit
                Toast.makeText(getApplicationContext(),"Save successfully",Toast.LENGTH_SHORT).show();
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: 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.