SharedPreference类是Android提供的几种数据存储方法之一,基于XML文件存储才存储键值对(key-value)数据。SharedPreference类支持boolean,Float,int,long,string类型数据的保存和检索(但是不支持修改数据,新的数据会将原来数据覆盖)。其存储路径为“data/data/程序包名/shared_prefs”。下面介绍一下SharedPreference类的使用:
1、 获取SahredPreference对象的方法。
(1) getSharedPreference(String name,int mode) 方法
第一个参数用于指定文件名,第二个用于指定操作模式,目前只有MODE_PRIVATE模式可以使用,代表只有当前应用程序才可以对其进行操作。
(2) getPreference(int mode)方法
该方法只接受一个操作模式参数,以当前活动类名作为文件名称。
(3) getDefaultSharedPreference()方法
它只接受一个Context参数,并以当前应用程序包名作为文件名称。
2、 向SharedPreference中写入值
(1) 首先通过SharedPreferences.Editor获取Editor对象
(2) 通过Editor对象的putString(key,value),putInt(key,value)方法存入值。
(3) 调用Editor对象的apply()方法将数据提交。
(4) 同时Editor还有两个方法,remove(key),移除key对应的键值对;clear(),清除所有键值对。
3、 从SharedPreferences读取数据
利用SharedPreference对象的getString(key,String xxx),getInt(key,Int xxx)等方法获取key对应的值,若该键值对不存在或内容为空,则返回xxx代表的值。
4、 SharedPreferences用法实例,保存密码。
(1) 核心代码
//存储从SharedPreferences中读取的值
private String name=null;
private String password=null;
private Boolean check=false;
……
private CheckBox checkBox;
checkBox=(CheckBox)findViewById(R.id.checkbox_remeber);
……
SharedPreferences preferences=getSharedPreferences(“login”,MODE_PRIVATE);
name=preferences.getString(“name”,"");
password=preferences.getString(“password”,"");
check=preferences.getBoolean(“checkBox”,false);
edt_name.setText(name);
edt_password.setText(password);
checkBox.setChecked(check);
……
if(checkBox.isChecked()){
SharedPreferences.Editor editor=getSharedPreferences(“login”,MODE_PRIVATE).edit();
editor.putString(“name”,edt_name.getText().toString());
editor.putString(“password”,edt_password.getText().toString());
editor.putBoolean(“checkBox”,true);
editor.apply();
}
(2)完整代码
布局文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.asus.login.LoginActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/linearLayout"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_marginTop="99dp"
android:gravity="center_vertical|center_horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="账号"
android:textSize="20dp"/>
<EditText
android:id="@+id/edt_name"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:hint="请输入用户名"
android:textSize="15dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/linearLayout"
android:layout_alignParentStart="true"
android:gravity="center_vertical|center_horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="密码"
android:textSize="20dp"/>
<EditText
android:id="@+id/edt_password"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:hint="请输入密码"
android:textSize="15dp"
android:inputType="textPassword"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/btn_login"
android:layout_alignParentEnd="true"
android:gravity="center_vertical|center_horizontal">
<CheckBox
android:id="@+id/checkbox_remeber"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/text_remeber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="记住密码"/>
</LinearLayout>
<Button
android:id="@+id/btn_login"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="登陆"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
活动代码
package com.example.asus.login;
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.Toast;
public class LoginActivity extends AppCompatActivity {
private String Name="android";
private String Password="android";
//存储从SharedPreferences中读取的值
private String name=null;
private String password=null;
private Boolean check=false;
private Button btn_login;
private EditText edt_name;
private EditText edt_password;
private CheckBox checkBox;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
btn_login=(Button)findViewById(R.id.btn_login);
edt_name=(EditText)findViewById(R.id.edt_name);
edt_password=(EditText)findViewById(R.id.edt_password);
checkBox=(CheckBox)findViewById(R.id.checkbox_remeber);
SharedPreferences preferences=getSharedPreferences("login",MODE_PRIVATE);
name=preferences.getString("name","");
password=preferences.getString("password","");
check=preferences.getBoolean("checkBox",false);
edt_name.setText(name);
edt_password.setText(password);
checkBox.setChecked(check);
btn_login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(edt_name.getText().toString().equals(Name)){
if(edt_password.getText().toString().equals(Password)){
Intent intent=new Intent(LoginActivity.this,MainActivity.class);
startActivity(intent);
if(checkBox.isChecked()){
SharedPreferences.Editor editor=getSharedPreferences("login",MODE_PRIVATE).edit();
editor.putString("name",edt_name.getText().toString());
editor.putString("password",edt_password.getText().toString());
editor.putBoolean("checkBox",true);
editor.apply();
}
}else{
Toast.makeText(LoginActivity.this, "密码错误!", Toast.LENGTH_SHORT).show();
}
}else{
Toast.makeText(LoginActivity.this, "用户名错误!", Toast.LENGTH_SHORT).show();
}
}
});
}
}