SP(安卓储存类)使用小结

SharedPreferences是Android用于存储轻量级数据的XML文件,常用于保存用户名、密码等简单信息。存储数据涉及新建SharedPreferences对象、创建Editor、使用put方法存键值对,最后commit提交。读取数据则通过get方法配合默认值完成,适用于实现记住密码等功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.SharedPreFerences是什么

是安卓的一种最轻量的储存类,储存为xml文件储存到/data/data/包名/shared_prefs下,一般用来存储一些比较简单的数据,比如用户名姓名,密码等等

2.如何储存数据

SharedPreFerences 储存数据分为四步
1.新建sharedPreFerences对象
2.通过sharedPreFerences.editor()创建editor对象
3.储存数据,通过editor.putString()、editor.putInt()方法储存,括号内是以键值对的形式存在例如editor.putString(“name”,”小明”)
4.提交数据 editor.commit();
附上一段简单的代码

//sp为新建xml文件的文件名,MODE模式,PRIVATE私有
 SharedPreferences sharedPreferences=getSharedPreferences("sp",MODE_PRIVATE);
        SharedPreferences.Editor editor=sharedPreferences.edit();
        editor.putString("name","张三");
        editor.commit();

3.如何读取数据

editor提交了数据后,如果我们要访问数据,就可以通过sharedPreferences对象读取,editor
是put,sharedPreferences就是get,后面还要加上一个默认值

 SharedPreferences sharedPreferences=getSharedPreferences("sp",MODE_PRIVATE);
        SharedPreferences.Editor editor=sharedPreferences.edit();
        editor.putString("name","张三");
        editor.commit();
        sp_tv.setText(sharedPreferences.getString("name",""));

4.记住密码案例

<?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"
    android:orientation="vertical">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="#e62828"
        android:orientation="horizontal">

        <ImageButton
            android:layout_width="50dp"
            android:layout_height="match_parent"
            android:background="#e62828"
            android:src="@mipmap/back_btn" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="110dp"
            android:layout_marginTop="10dp"
            android:text="登录"
            android:textColor="#ffffff"
            android:textSize="30dp" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingTop="10dp"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:gravity="center"
            android:orientation="horizontal">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="手机:"
                android:textSize="20dp"
                android:textStyle="bold" />
            <EditText
                android:id="@+id/login_name"
                android:layout_width="250dp"
                android:layout_height="match_parent"
                android:hint="请输入你的手机号!" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:gravity="center"
            android:orientation="horizontal">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="密码:"
                android:textSize="20dp"
                android:textStyle="bold" />
            <EditText
                android:id="@+id/login_psd"
                android:layout_width="250dp"
                android:layout_height="match_parent"
                android:hint="请输入你的密码!" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="50dp">
            <CheckBox
                android:id="@+id/login_cb"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:text="记住密码"
                />
        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:gravity="center"
            android:orientation="horizontal">
            <Button
                android:id="@+id/login_btn"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="#e62828"
                android:text="登录"
                android:textColor="#ffffff" />
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="50dp"
                android:background="#e62828"
                android:text="注册"
                android:textColor="#ffffff" />
        </LinearLayout>
    </LinearLayout>
</LinearLayout>
package com.example.zuoye1;

import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;

public class LoginActivity extends AppCompatActivity implements View.OnClickListener {
    private EditText login_name;
    private EditText login_psd;
    private Button login_btn;
    private CheckBox login_cb;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        bindID();

        SharedPreferences sharedPreferences = getSharedPreferences("name", MODE_PRIVATE);
        int checked=sharedPreferences.getInt("isCheck",0);
        //判断name文件中是否有数据,复选按钮是否被点击
        if(checked==1){
            login_name.setText(sharedPreferences.getString("name",""));
            login_psd.setText(sharedPreferences.getString("psd",""));
            //如果有数据,让复选按钮继续保持点击状态
            login_cb.setChecked(true);
        }else {
        //如果没有数据,让复选按钮继续保持未点击状态
            login_cb.setChecked(false);
        }
    }

    private void bindID() {
        login_name = findViewById(R.id.login_name);
        login_psd = findViewById(R.id.login_psd);
        login_btn = findViewById(R.id.login_btn);
        login_btn.setOnClickListener(this);
        login_cb=findViewById(R.id.login_cb);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.login_btn:
                SharedPreferences sharedPreferences=getSharedPreferences("name", MODE_PRIVATE);
                SharedPreferences.Editor editor = sharedPreferences.edit();
                //判断是否点击了复选按钮
                if (login_cb.isChecked()) {
                //复选按钮被点击时将密码和用户名存放进name.xml文件中,
                并且设置一个标志位通知复选按钮按下了
                    String name = login_name.getText().toString();
                    String psd = login_psd.getText().toString();
                    editor.putString("name", name);
                    editor.putString("psd", psd);
                    editor.putInt("isCheck",1);
                }else {
                //复选按钮未被点击时将name.xml文件中密码和用户名清空,
                并且设置一个标志位通知复选按钮未按
                    editor.putString("name", "");
                    editor.putString("psd", "");
                    editor.putInt("isCheck",0);
                }
                editor.commit();
                Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                startActivityForResult(intent, 1);
                break;
            default:
                break;
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值