第八天 SharedPreferences存储和SD卡存储

一、SharedPreferences存储

1.SharedPreferences简介

SharedPreferences简称Sp(后面都会称Sp),是一种轻量级的数据存储方式,采用Key/value的方式 进行映射,最终会在手机的/data/data/package_name/shared_prefs/目录下以xml的格式存在。
Sp通常用于记录一些参数配置、行为标记等!因为其使用简单,所以大多数开发者用起来很爽!
但是 请注意:千万不要使用Sp去存储量大的数据,也千万不要去让你的Sp文件超级大,否则会大大影响应用性能, 甚至出现ANR(程序无响应)

2.特点

1.保存少量的数据,且这些数据的格式非常简单。 存储5种原始数据类型: boolean, float, int, long, String
2.比如应用程序的各种配置信息(如是否打开音效、是否使用震动效果、小游戏的玩家积分等),记住密码功能,音乐播放器播放模式。

3.写数据

 //得到SharedPreferences对象
                SharedPreferences preferences = getSharedPreferences("name", MODE_PRIVATE);
                //获得编辑对象
                SharedPreferences.Editor edit = preferences.edit();
                //存入各种类型的数据
                edit.putString("username","123");
                edit.putInt("age",18);
                edit.putBoolean("man",true);
                edit.putFloat("price",12.5f);
                edit.putLong("id",100000000);
                //提交数据
                edit.commit();

4.读数据

//获取SharedPreferences对象
                SharedPreferences name = getSharedPreferences("name", MODE_PRIVATE);
                //直接读取
                String usernmae = name.getString("username", "");
                int age = name.getInt("age", 0);
                Toast.makeText(MainActivity.this, usernmae+"+"+age, Toast.LENGTH_SHORT).show();

5.记住密码案例

package com.example.day008;

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

import java.util.Map;

public class MainActivity extends AppCompatActivity {
   

    private SharedPreferences sharedPreferences;
    private EditText username;
    private EditText password;
    private CheckBox cb;
    private Button login;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
   
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        username = (EditText) findViewById(R.id.username);
        password = (EditText) findViewById(R.id.password);
        cb=(CheckBox)findViewById(R.id.cb_remember);
        login=(Button)findViewById(R.id.login);
        //TODO 读取
        sharedPreferences=getSharedPreferences("1705A",MODE_PRIVATE);
        boolean ischeck= sharedPreferences.getBoolean("ischeck",false);
        if(ischeck){
   
            //读到用户名和密码展现在页面中,复选框被勾选
            String username1=sharedPreferences.getString("username","");
            String password1=sharedPreferences.getString("password","");
            username.setText(username1);
            password.setText(password1);
            cb.setChecked(true);
        }
        //TODO 写数据
        login.setOnClickListener(new View.OnClickListener() {
   
            @Override
            public void onClick(View v) {
   
                String username2=username.getText().toString().trim();
                String password2=password.getText().toString().trim();
                //用户名和密码是否正确
                if("sgf".equals(username2)&&"123456".equals(password2)){
   
                    //判断记住密码是否被选中
                    if(cb.isChecked()){
   //存
                        SharedPreferences.Editor edit = sharedPreferences.edit();
                        edit.putBoolean("ischeck",true);
                        edit.putString("username",username2);
                        edit.putString("password",password2);
                        edit.commit();

                    }else{
   //清空
                        SharedPreferences.Editor edit = sharedPreferences.edit();
                        edit.putBoolean("ischeck",false);
                        edit.commit();
                    }
                }
            }
        });
    }
}

6.轮播图

package com.example.day6;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;
imp
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值