SharedPreferences

一.SharedPreferences是什么?

1.SharedPreferencesAndroid平台上一个轻量级的存储类,用来存储少量数据时简单、便捷(如:保存记住密码状态,设置开关状态等)。

2.以key-value(键值对)形式存储数据,可以存储的数据类型为:String、float、int、long、boolean。

3.存储位置/data/data/<包名>shared_prefsm目录下。

4.保存的数据以xml形式存在

二.如何存储数据?

1.获得使用的SharedPreferences对象。

2.获得Editor对象。

3.通过Editor对象的putXXX函数,设置写入数据。

4.通过Editor对象的commit提交写入。

代码如下:

SharedPreferences sp=getSharedPreferences("sp",MODE_PRIVATE);//声明一个sharedPreferences用于保存数据
                        SharedPreferences.Editor editor=sp.edit();//得到editor对象
                        editor.putString("name",spName.getText().toString());//记录用户
                        editor.putString("pwd",spPwd.getText().toString());//记住密码
                        editor.commit();

三.如何读取数据?

1.获得使用的SharedPreferences对象。

2.把已经存储的数据放到相应的控件中显示。

代码如下:

SharedPreferences sp=getSharedPreferences("sp",MODE_PRIVATE);
            spName.setText(sp.getString("name",""));
            spPwd.setText(sp.getString("pwd",""));

四.记住密码案例

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"
    tools:context="com.example.androidthree.SpActivity">



    <EditText
        android:id="@+id/sp__name_et"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:hint="请输入用户名"/>

    <EditText
        android:id="@+id/sp__pwd_et"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:hint="请输入密码"/>

    <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:orientation="horizontal"
    >
    <CheckBox
        android:id="@+id/sp_jizhupwd"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:text="记住密码"/>
</LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="50dp">

        <Button
            android:id="@+id/sp_btn"
            android:layout_width="120dp"
            android:layout_height="match_parent"
            android:text="保存"
            android:layout_marginHorizontal="120dp"/>
    </LinearLayout>
</LinearLayout>

Java代码如下:

package com.example.androidthree;

import android.content.Intent;
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;

public class SpActivity extends AppCompatActivity {
    private EditText spName;
    private EditText spPwd;
    private CheckBox spJizhu;
    private Button spBtn;
    private SharedPreferences sp=null;//声明一个sharedPreferences用于保存数据
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sp);

        tindID();

        setListener();//获取控件
        getData();//绑定事件
    }

    private void getData() {
        SharedPreferences sp=getSharedPreferences("sp",MODE_PRIVATE);
        if (sp.getBoolean("isKeep",false)){
            //如果之前存储过,则显示在相应文本框中
            spName.setText(sp.getString("name",""));
            spPwd.setText(sp.getString("pwd",""));

        }else {
            //否则显示为空
            spName.setText("");
            spPwd.setText("");
        }
    }

    private void setListener() {
        spBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (spName.getText().toString().equals("1")&&spPwd.getText().toString().equals("2")){
                    //判断是否记住密码,复选框是否选中
                    if (spJizhu.isChecked()){
                        sp=getSharedPreferences("sp",MODE_PRIVATE);
                        SharedPreferences.Editor editor=sp.edit();//得到editor对象
                        editor.putBoolean("isKeep",true);//记录保存的标记
                        editor.putString("name",spName.getText().toString());//记录用户
                        editor.putString("pwd",spPwd.getText().toString());//记住密码
                        editor.commit();
                    }else {
                        sp=getSharedPreferences("sp", MODE_PRIVATE);
                        SharedPreferences.Editor edit=sp.edit();
                        edit.putBoolean("isKeep", true);//保存的文件名isKeep
                        edit.putString("name", "");
                        edit.putString("pwd", "");
                        edit.commit();
                    }
                    //跳转页面
                    Intent intent=new Intent(SpActivity.this,WebActivity.class);
                    startActivity(intent);
                }

            }
        });
    }

    private void tindID() {
        spName=findViewById(R.id.sp__name_et);
        spPwd=findViewById(R.id.sp__pwd_et);
        spJizhu=findViewById(R.id.sp_jizhupwd);
        spBtn=findViewById(R.id.sp_btn);


    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值