1.SharedPreferences是什么
- SharedPreferences是安卓的一种轻量级的存储类,用来保存activity的状态以及一些数据。
- 工作原理:通过Android系统生成一个xml文件保到:/data/data/包名/shared_prefs目录下,类似键值对的方式来存储数据。
- Sharedpreferences提供了常规的数据类型保存接口比如:int、long、boolean、String、Float、Set和Map这些数据类型
2.如何存储数据
1. 得到SharedPreferences对象
方法1:Context.getSharedPreferences(文件名称,操作模式)
sharedPreferences=this.getPreferences(MODE_PRIVATE);
MODE_PRIVATE:默认操作模式,直接在把第二个参数写0就是默认使用这种操作模式,这种模式表示只有当前的应用程序才可以对当前这个SharedPreferences文件进行读写。
MODE_MULTI_PRIVATE:用于多个进程共同操作一个SharedPreferences文件。
方法二.PreferenceManager.getDefaultSharedPreferences(Context)
sharedPreferences= PreferenceManager.getDefaultSharedPreferences(this);
使用这个方法会自动使用当前程序的包名作为前缀来命名SharedPreferences文件
2.调用SharedPreferences对象的edit()方法来获取一SharedPreferences.Editor对象。
SharedPreferences.Editor editor=sharedPreferences.edit();
3.把数据保存到SharedPreferences.Editor对象中。
//以键值对的方式存入
editor.putString("usename",usename);
editor.putString("pwds",pwds);
4.提交数据
editor.commit();
3.如何读取数据
SharedPreferences pref = getSharedPreferences(“data”,MODE_PRIVATE);
String name = pref.getString(“name”,”“);//第二个参数为默认值
int age = pref.getInt(“age”,0);//第二个参数为默认值
boolean married = pref.getBoolean(“married”,false);//第二个参数为默认值
4.记住密码案例
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"
android:background="#f4efef"
tools:context="com.example.dfcn.paopao.LoginActivity">
<LinearLayout
android:background="#f9f74922"
android:layout_width="match_parent"
android:layout_height="45dp">
<LinearLayout
android:layout_gravity="center_vertical"
android:layout_weight="1"
android:layout_width="10dp"
android:layout_height="wrap_content">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/back_btn"/>
</LinearLayout>
<LinearLayout
android:layout_weight="1"
android:layout_gravity="center_vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:textColor="#ffff"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="登 陆"
android:textSize="25sp" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="15dp">
</LinearLayout>
<LinearLayout
android:background="#ffffff"
android:layout_width="match_parent"
android:layout_height="150dp"
android:orientation="vertical">
<RelativeLayout
android:gravity="center_vertical"
android:layout_width="match_parent"
android:layout_height="75dp">
<TextView
android:id="@+id/tv_number"
android:textSize="20sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="手机:"/>
<EditText
android:id="@+id/login_iphone_et"
android:layout_toRightOf="@id/tv_number"
android:hint="请输入手机号"
android:background="#00000000"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignBaseline="@id/tv_number"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:background="#dedddd"
android:layout_width="match_parent"
android:layout_height="1dp" />
</RelativeLayout>
<RelativeLayout
android:gravity="center_vertical"
android:layout_width="match_parent"
android:layout_height="75dp">
<TextView
android:id="@+id/tv_pwd"
android:textSize="20sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="密码:"/>
<EditText
android:id="@+id/login_pwd_et"
android:layout_toRightOf="@id/tv_pwd"
android:hint="请输入密码"
android:background="#00000000"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignBaseline="@id/tv_pwd"/>
</RelativeLayout>
</LinearLayout>
<CheckBox
android:id="@+id/cb_pwd"
android:text="记住密码"
android:layout_width="match_parent"
android:layout_height="30dp"/>
<LinearLayout
android:layout_marginTop="30sp"
android:gravity="center"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:textColor="#ffff"
android:background="#f9f74922"
android:id="@+id/btn_login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="登陆"
/>
<Button
android:textColor="#ffff"
android:background="#f9f74922"
android:id="@+id/btn_register"
android:layout_marginLeft="80dp"
android:layout_toRightOf="@id/btn_login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="注册"/>
</RelativeLayout>
</LinearLayout>
</LinearLayout>
activity:
package com.example.dfcn.paopao;
import android.content.Intent;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
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;
public class LoginActivity extends AppCompatActivity {
private EditText iphonenum,pwd;
private Button loginbtn;
private CheckBox checkBox;
private SharedPreferences sharedPreferences;
private SharedPreferences.Editor editor;
@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
iphonenum=findViewById(R.id.login_iphone_et);
pwd=findViewById(R.id.login_pwd_et);
loginbtn=findViewById(R.id.btn_login);
checkBox=findViewById(R.id.cb_pwd);
sharedPreferences= PreferenceManager.getDefaultSharedPreferences(this);
//CheakBox的状态
boolean isRemenber=sharedPreferences.getBoolean("checkBox",false);
//如果勾选了下次在进入就把保存的数据给读取出来
if (isRemenber==true){
String usename=sharedPreferences.getString("usename","");
String pwds=sharedPreferences.getString("pwds","");
iphonenum.setText(usename);
pwd.setText(pwds);
checkBox.setChecked(true);
}
//登陆的单击事件
loginbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String usename=iphonenum.getText().toString();
String pwds=pwd.getText().toString();
editor=sharedPreferences.edit();
//判断是否勾上了记住密码
if (checkBox.isChecked()){
editor.putString("usename",usename);
editor.putString("pwds",pwds);
editor.putBoolean("checkBox",true);
}
else {
editor.clear();
}
editor.commit();
Intent intent=new Intent(LoginActivity.this,MainActivity.class);
startActivity(intent);
finish();
}
});
}
}