1.什么是SharedPreferences 存储?
SharedPreferences是Android平台上一个轻量级的存储类,用来存储少量数据时简单,便捷(保存记住密码状态,设置开关状态等)。
以key-value(键值对)形式存储数据,可以存储的数据类型为:String、float、int、long、boolean。
存储位置在/data/data/<包名>/shared_prefs目录下。
保存的数据以XML形式存储。
2.使用SharedPreferences写入数据步骤:
1.获得使用SharedPreferences对象;
2.获得Editor对象;
3.通过Editor对象的putXXX函数,设置写入数据;
4.通过Editor对象的commit提交写入;
获取SharePreferences对象
在有Context环境中使用getSharePreferences方法获得
获取Editor对象
通过SharePreferences.edit()方法获得
使用SharedPreferences存储数据案例
package com.example.myapplication15;
import android.content.Context;
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.CompoundButton;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private EditText edit_username;
private EditText edit_password;
private CheckBox checkBox;
private Button button;
private CheckBox checkBox_zidong;
private int remamberFlag = 0;
private String password = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
binID();
SharedPreferences sharedPreferences = getSharedPreferences("test", MODE_PRIVATE);
//如果不为空
if (sharedPreferences != null) {
String name = sharedPreferences.getString("name", "");
password = sharedPreferences.getString("password", "");
remamberFlag = sharedPreferences.getInt("remeber_flag", 0);
edit_username.setText(name);
}
//确定为1获取 记住密码,打钩
if (remamberFlag == 1) {
checkBox.setChecked(true);
edit_password.setText(password);
}
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//1 创建 SharePreferences 对象
String username = edit_username.getText().toString();
String password = edit_password.getText().toString();
SharedPreferences sharedPreferences = getSharedPreferences("test", MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
//2 创建Editor对象,写入值
editor.putString("name", username);
if (checkBox.isChecked()) {
remamberFlag = 1;
editor.putInt("remeber_flag", remamberFlag);
editor.putString("password", password);
} else {
remamberFlag = 0;
editor.putInt("remeber_flag", remamberFlag);
}
//3 提交
editor.commit();
Toast.makeText(MainActivity.this, "登录成功", Toast.LENGTH_LONG).show();
}
});
}
private void binID() {
edit_username = findViewById(R.id.edit_username);
edit_password = findViewById(R.id.edit_password);
checkBox = findViewById(R.id.Cb);
checkBox_zidong = findViewById(R.id.zidong_check);
button = findViewById(R.id.main_button);
}
}
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.myapplication15.MainActivity">
<RelativeLayout
android:layout_marginTop="200dp"
android:gravity="center"
android:layout_gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/edit_username"
android:hint="输入账号"
android:layout_gravity="center"
android:layout_width="250dp"
android:layout_height="50dp" />
</RelativeLayout>
<RelativeLayout
android:gravity="center"
android:layout_gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/edit_password"
android:hint="输入密码"
android:password="true"
android:layout_gravity="center"
android:layout_width="250dp"
android:layout_height="50dp" />
</RelativeLayout>
<Button
android:id="@+id/main_button"
android:layout_width="match_parent"
android:layout_height="40dp"
android:text="登录"
/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="40dp">
<CheckBox
android:id="@+id/Cb"
android:layout_width="30dp"
android:layout_height="20dp"
android:layout_marginLeft="60dp"
/>
<TextView
android:textColor="#000000"
android:layout_width="80dp"
android:layout_height="30dp"
android:text="记住密码"
/>
<CheckBox
android:id="@+id/zidong_check"
android:layout_width="30dp"
android:layout_height="20dp"
android:layout_marginLeft="60dp"
/>
<TextView
android:textColor="#000000"
android:layout_width="80dp"
android:layout_height="30dp"
android:text="自动登录"
/>
</LinearLayout>
</LinearLayout>