一.SharedPreferences是什么?
1.SharedPreferences是Android平台上一个轻量级的存储类,用来存储少量数据时简单、便捷(如:保存记住密码状态,设置开关状态等)。
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.Editor editor=sp.edit()
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;
@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.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);
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);
}
}