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,所以信息会被保存在相对应的文件夹下
在这里插入图片描述

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