Android使用Sharepreferences實現簡單的數據存儲

Android實現數據存儲的方式有五種,包括SQLite數據存儲、網絡存儲、Sharepreferences存儲、ContentProvider存儲和文件存儲這五種;
接下來我們就拿Sharepreferences來實現一個簡單的數據存儲吧
首先寫一個簡單的登陸界面

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:orientation="vertical">
    <EditText
        android:id="@+id/user_et"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/user"/>
    <EditText
        android:id="@+id/psw_et"
        android:inputType="textPassword"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/psw"/>
    <Button
        android:id="@+id/btn_login"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/Login"/>

</LinearLayout>

然後寫主代碼,代碼上都有註釋,這裏我就不多說啦

package com.example.sharepreference;

import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    private EditText editText_user,editText_psw;
    private Button button;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate (savedInstanceState);
        setContentView (R.layout.activity_main);
        //初始化控件
        button = findViewById (R.id.btn_login);
        editText_psw = findViewById (R.id.psw_et);
        editText_user = findViewById (R.id.user_et);
        button.setOnClickListener (new View.OnClickListener () {
            @Override
            public void onClick(View v) {
                //獲取兩個輸入框的內容
                String user = editText_user.getText ().toString ();
                String psw = editText_psw.getText ().toString ();
                //將信息存儲到SharePreferences
                if(user.equals ("admin") && psw.equals ("123654")){
                    //獲取SharePreferences(參數1:文件名,參數2:模式)
                    SharedPreferences sharedPreferences = getSharedPreferences ("SaveData",MODE_PRIVATE);
                    //獲取Editor對象
                    SharedPreferences.Editor editor = sharedPreferences.edit ();
                    //存儲信息
                    editor.putString ("admin",user);
                    editor.putString ("123654",psw);
                    //提交
                    editor.commit ();
                    Toast.makeText (MainActivity.this,"登錄成功",Toast.LENGTH_SHORT).show ();
                }else {
                    Toast.makeText (MainActivity.this,"用戶名或者密碼錯誤",Toast.LENGTH_LONG).show ();
                }
            }
        });
    }
}


然後運行程序
在這裏插入圖片描述
當你輸入用戶名和密碼之後,點擊登陸,數據會被保存在本地,不過要注意的是隻有賬號密碼都對了數據纔會保存哦,如果去掉if判斷語句,那麼布官輸入什麼內容,點擊登陸數據都會被保存
數據保存在
在這裏插入圖片描述
點擊Device File Explorer,然後點擊data->data->然後找到與自己項目相對應的包名,打開它,這裏我把保存的文件名寫成了SaveData,所以信息會被保存在相對應的文件夾下
在這裏插入圖片描述

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