android-SharedPreferences存儲

1.SharedPreferences是什麼

  • SharedPreferences是安卓的一種輕量級的存儲類,用來保存activity的狀態以及一些數據。
  • 工作原理:通過Android系統生成一個xml文件保到:/data/data/包名/shared_prefs目錄下,類似鍵值對的方式來存儲數據。
  • Sharedpreferences提供了常規的數據類型保存接口比如:int、long、boolean、String、Float、Set和Map這些數據類型

2.如何存儲數據

1. 得到SharedPreferences對象
方法1:Context.getSharedPreferences(文件名稱,操作模式)

sharedPreferences=this.getPreferences(MODE_PRIVATE);

MODE_PRIVATE:默認操作模式,直接在把第二個參數寫0就是默認使用這種操作模式,這種模式表示只有當前的應用程序纔可以對當前這個SharedPreferences文件進行讀寫。
MODE_MULTI_PRIVATE:用於多個進程共同操作一個SharedPreferences文件。
方法二.PreferenceManager.getDefaultSharedPreferences(Context)

sharedPreferences= PreferenceManager.getDefaultSharedPreferences(this);

使用這個方法會自動使用當前程序的包名作爲前綴來命名SharedPreferences文件
2.調用SharedPreferences對象的edit()方法來獲取一SharedPreferences.Editor對象

SharedPreferences.Editor editor=sharedPreferences.edit();

3.把數據保存到SharedPreferences.Editor對象中。

//以鍵值對的方式存入
editor.putString("usename",usename);
editor.putString("pwds",pwds);

4.提交數據

 editor.commit();

3.如何讀取數據

SharedPreferences pref = getSharedPreferences(“data”,MODE_PRIVATE); 
String name = pref.getString(“name”,”“);//第二個參數爲默認值 
int age = pref.getInt(“age”,0);//第二個參數爲默認值 
boolean married = pref.getBoolean(“married”,false);//第二個參數爲默認值

4.記住密碼案例

xml佈局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="#f4efef"
    tools:context="com.example.dfcn.paopao.LoginActivity">
   <LinearLayout
       android:background="#f9f74922"
       android:layout_width="match_parent"
       android:layout_height="45dp">
       <LinearLayout
           android:layout_gravity="center_vertical"
           android:layout_weight="1"
           android:layout_width="10dp"
           android:layout_height="wrap_content">
           <ImageView
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:src="@mipmap/back_btn"/>
       </LinearLayout>

<LinearLayout
    android:layout_weight="1"
    android:layout_gravity="center_vertical"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <TextView
        android:textColor="#ffff"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:text="登 陸"
        android:textSize="25sp" />
</LinearLayout>

   </LinearLayout>
<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="15dp">

</LinearLayout>
<LinearLayout
    android:background="#ffffff"
    android:layout_width="match_parent"
    android:layout_height="150dp"
    android:orientation="vertical">
    <RelativeLayout
        android:gravity="center_vertical"
        android:layout_width="match_parent"
        android:layout_height="75dp">
        <TextView
            android:id="@+id/tv_number"
            android:textSize="20sp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="手機:"/>
        <EditText
            android:id="@+id/login_iphone_et"
            android:layout_toRightOf="@id/tv_number"
            android:hint="請輸入手機號"
            android:background="#00000000"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignBaseline="@id/tv_number"/>
    </RelativeLayout>
    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <TextView
            android:background="#dedddd"
            android:layout_width="match_parent"
            android:layout_height="1dp" />
    </RelativeLayout>

    <RelativeLayout
        android:gravity="center_vertical"
        android:layout_width="match_parent"
        android:layout_height="75dp">
        <TextView
            android:id="@+id/tv_pwd"
            android:textSize="20sp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="密碼:"/>
        <EditText
            android:id="@+id/login_pwd_et"
            android:layout_toRightOf="@id/tv_pwd"
            android:hint="請輸入密碼"
            android:background="#00000000"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignBaseline="@id/tv_pwd"/>
    </RelativeLayout>

</LinearLayout>
    <CheckBox
        android:id="@+id/cb_pwd"
        android:text="記住密碼"
        android:layout_width="match_parent"
        android:layout_height="30dp"/>
<LinearLayout
    android:layout_marginTop="30sp"
    android:gravity="center"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
   <RelativeLayout
       android:layout_width="wrap_content"
       android:layout_height="wrap_content">
     <Button
         android:textColor="#ffff"
         android:background="#f9f74922"
         android:id="@+id/btn_login"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="登陸"
         />
       <Button
           android:textColor="#ffff"
           android:background="#f9f74922"
           android:id="@+id/btn_register"
           android:layout_marginLeft="80dp"
           android:layout_toRightOf="@id/btn_login"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:text="註冊"/>
   </RelativeLayout>
</LinearLayout>


</LinearLayout>

activity:

package com.example.dfcn.paopao;

import android.content.Intent;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;

public class LoginActivity extends AppCompatActivity {
private EditText iphonenum,pwd;
private Button loginbtn;
private CheckBox checkBox;
private SharedPreferences sharedPreferences;
private SharedPreferences.Editor editor;
    @Override
    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        iphonenum=findViewById(R.id.login_iphone_et);
        pwd=findViewById(R.id.login_pwd_et);
        loginbtn=findViewById(R.id.btn_login);
        checkBox=findViewById(R.id.cb_pwd);
        sharedPreferences= PreferenceManager.getDefaultSharedPreferences(this);
        //CheakBox的狀態
        boolean isRemenber=sharedPreferences.getBoolean("checkBox",false);
        //如果勾選了下次在進入就把保存的數據給讀取出來
        if (isRemenber==true){
            String usename=sharedPreferences.getString("usename","");
            String pwds=sharedPreferences.getString("pwds","");
            iphonenum.setText(usename);
            pwd.setText(pwds);
            checkBox.setChecked(true);
        }
        //登陸的單擊事件
        loginbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String usename=iphonenum.getText().toString();
                String pwds=pwd.getText().toString();
                editor=sharedPreferences.edit();
                //判斷是否勾上了記住密碼
               if (checkBox.isChecked()){
                   editor.putString("usename",usename);
                   editor.putString("pwds",pwds);
                   editor.putBoolean("checkBox",true);
               }
               else {
                   editor.clear();
               }
                    editor.commit();
                Intent intent=new Intent(LoginActivity.this,MainActivity.class);
                startActivity(intent);
                finish();

            }
        });
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章