当前位置: 移动技术网 > IT编程>开发语言>Java > 安卓SharePreferences数据存储

安卓SharePreferences数据存储

2020年07月09日  | 移动技术网IT编程  | 我要评论

1.什么是SharePreferences
SharePreferences是安卓平台上的一个轻量级存储类,用来存储少量数据时简单、便捷(如记住密码)。
SharePreferences以键值对形式存储数据。
SharePreferences的存储位置在/data/data/<包名>/sharedprefs目录下
SharePreferences保存数据以XML存储。
2.如何使用SharePreferences

获得使用SharePreferences对象;

获得Editor对象;

通过Editor对象的putXXX函数,设置写入数据;

通过Editor对象的commit()提交写入
3.使用SharePreferences记住密码实例
在主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:orientation="vertical"
    android:layout_height="match_parent"
    tools:context="com.example.ll.storeapplication.MainActivity">

<EditText
        android:hint="输入账号"
        android:id="@+id/zhet"
        android:layout_width="match_parent"
        android:layout_height="50dp" />
    <EditText
        android:password="true"
        android:hint="输入密码"
        android:id="@+id/mimaet"
        android:layout_width="match_parent"
        android:layout_height="50dp" />
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="50dp">
<CheckBox
            android:id="@+id/cb"
            android:layout_gravity="center"
            android:layout_width="30dp"
            android:layout_height="30dp" />
        <TextView
           android:gravity="center"
            android:id="@+id/jizhumim"
            android:text="记住密码"
            android:textSize="20sp"
            android:layout_width="150dp"
            android:layout_height="50dp" />
        <Button
            android:id="@+id/denlu"
            android:text="登陆"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </LinearLayout>
</LinearLayout>

然后MainActivity编写主要代码,如下:

package com.example.ll.storeapplication;
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.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
    private EditText mimaet;
    private EditText zhet;
    private CheckBox ck;
    private TextView tv;
    private Button btn;
    private int rember = 0;
    private String password;
    @Override
 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //绑定Id
        BindId();
        //从sharedPreferences文件(mysp.xml)中取出“name”节点对应的值
        SharedPreferences preferences = getSharedPreferences("mysp.xml", MODE_PRIVATE);
        //如果文件存不存在,进行节点赋值
        if (preferences != null) {
            String name = preferences.getString("name", "");
            password = preferences.getString("password", "");
            rember=preferences.getInt("rember",0);
            //赋值给zhet
             zhet.setText(name);
        }
        //如果记住密码,那么勾选记住密码的勾一直存在
        if (rember == 1) {
            ck.setChecked(true);
            mimaet.setText(password);//密码也被一直记住
        }
        //对btn设置监听事件
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String username = zhet.getText().toString();
                String password = mimaet.getText().toString();
  //1.创建SharedPreferences对象
                SharedPreferences sharedPreferences = getSharedPreferences("mysp.xml", MODE_PRIVATE);
                //2.创建Editor 对象,写入值
                SharedPreferences.Editor editor = sharedPreferences.edit();
                editor.putString("name", username);
                //判断是否记住密码
                if (ck.isChecked()) {  rember = 1;//再次点进时密码存在
                    editor.putInt("rember", rember);
                    editor.putString("password", password);
                }else {
                    rember=0;//再次点进时密码不存在
                    editor.putInt("rember", rember);
                }
                //3.提交
                editor.commit();
                //4.打印日志
                Toast.makeText(MainActivity.this, "登陆成功", Toast.LENGTH_SHORT).show();
            }
        });
    }
     private void BindId() {
        mimaet = findViewById(R.id.mimaet);
        zhet = findViewById(R.id.zhet);
        ck = findViewById(R.id.cb);
        tv = findViewById(R.id.jizhumim);
        btn = findViewById(R.id.denlu);
    }
}

本文地址:https://blog.csdn.net/Miraclefate/article/details/107164102

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网