天天看點

Android中SharedPreferences的運作(登入記住使用者名案例)

一,SharedPreferences基礎知識:

     SharedPreferences是Android平台上一個輕量級的存儲類,用來儲存應用的一些常用配置,比如Activity狀态,Activity暫停時,将此activity的狀态儲存到SharedPereferences中;當Activity重載,系統回調方法onSaveInstanceState時,再從SharedPreferences中将值取出。 SharedPreferences提供了java正常的Long、Int、String等類型資料的儲存接口。  SharedPreferences類似過去Windows系統上的ini配置檔案,但是它分為多種權限,可以全局共享通路。 提示最終是以xml方式來儲存,整體效率來看不是特别的高,對于正常的輕量級而言比SQLite要好不少,如果真的存儲量不大可以考慮自己定義檔案格式。xml處理時Dalvik會通過自帶底層的本地XML Parser解析,比如XMLpull方式,這樣對于記憶體資源占用比較好。

二,用SharedPreferences實作簡單資料存儲:

       (1)看下效果:圖一(填寫使用者名并記住),圖二(再次打開時:)

Android中SharedPreferences的運作(登入記住使用者名案例)

  (2)具體實作:            對于案例中的xml檔案和id的綁定就不用說了,我們還是把重點放在SharedPreferences吧!             ①擷取SharedPreferences對象:                     SharedPreferences spf=getSharedPreference("檔案名",int)                    //第一個參數:想把資料儲存在那個檔案的檔案名                     //第二個參數:權限設定  :MODE_PRIVATE(私有型)

Android中SharedPreferences的運作(登入記住使用者名案例)
//定義一個SharedPreferences對象
        SharedPreferences spf;
         //第一個參數:将資料儲存的檔案名
        //第一個參數:權限設定
       // MODE_PRIVATE  //私有
        spf=getSharedPreferences("filename",MODE_PRIVATE);
           

         ②擷取SharedPreferences.Editor對象,進行資料的存入;

//擷取SharedPreferences.Editor對象
                    SharedPreferences.Editor editor=spf.edit();
           

                ③通過editor的putXxx(putString(),putInt()...)方法儲存資料

//存一個key為username的資料
                    editor.putString("username",username.getText().toString());
           

                  ④editor.commit()方法送出:(必須送出,不然資料就存不了);

//存入後必須送出
                    editor.commit();
           

                    ⑤擷取資料:

//去擷取資料,如果沒有,設定為空
       String name= spf.getString("username","");
        username.setText(name);
           
三:原碼:
package com.maeeage.administrator.androidlearn;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

/**
 * Created by Administrator on 2018/2/1/001.
 */

public class Login extends Activity {

    EditText username,password;
    CheckBox box;
    Button btn_login;

    //        //定義一個SharedPreferences對象     
    SharedPreferences spf;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);
        findById();
        init();
    }

    private void findById() {
        username=(EditText)findViewById(R.id.username);
        password=(EditText)findViewById(R.id.password);
        box=(CheckBox)findViewById(R.id.checkBox);
        btn_login=(Button)findViewById(R.id.btn_login);
    }

    private void init() {

         //第一個參數:将資料儲存的檔案名
        //第一個參數:權限設定
       // MODE_PRIVATE  //私有
        spf=getSharedPreferences("filename",MODE_PRIVATE);
        //去擷取資料,如果沒有,設定為空
       String name= spf.getString("username","");
        username.setText(name);
        //預設選擇記住使用者名
        box.setChecked(true);
       //btn_login點選事件
        btn_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(box.isChecked())
                {
                    //擷取SharedPreferences.Editor對象
                    SharedPreferences.Editor editor=spf.edit();
                    //存一個key為username的資料
                    editor.putString("username",username.getText().toString());
                    //存入後必須送出
                    editor.commit();
                }else{
                    SharedPreferences.Editor editor=spf.edit();
                    editor.putString("username","");
                    editor.commit();
                }

                if("admin".equals(username.getText().toString())&&"123456".equals(password.getText().toString()))
                Toast.makeText(Login.this,"登入成功",Toast.LENGTH_SHORT);
                else
                Toast.makeText(Login.this,"登入失敗",Toast.LENGTH_SHORT);
            }
        });
    }
}
           
四,總結

SharedPreferences在Android開發中四很重要,認真學習和多多練習。