1.Sharedpreferences是什么
SharedPreferences 是Android 中的内置API,它允许我们存取键值对形式的基础类型数据,像:boolean,float ,int , long , string 。这些数据将会持久化的存在,即使你的应用程序结束之后(注意:如果应用从手机上写在掉后,该程序对应的SharedPreferences将会消失,保存在路径:/data/data//shared_prefs 目录下)
注意:User Preferences 用户配置
严格来说,SharedPreferences最好不要用来保存“用户配置”,比如,用户选择了什么铃声,是否自动更新等等。如果要为应用创建用户配置,可以使用PreferenceActivity,它可以用来创建“用户配置”。(PreferenceActivity用来创建程序中的设置界面)
2.如何存储数据
SharedPreferences 是Android 中的内置API,它允许我们存取键值对形式的基础类型数据,像:boolean,float ,int , long , string 。这些数据将会持久化的存在,即使你的应用程序结束之后(注意:如果应用从手机上写在掉后,该程序对应的SharedPreferences将会消失,保存在路径:/data/data//shared_prefs 目录下)
注意:User Preferences 用户配置
严格来说,SharedPreferences最好不要用来保存“用户配置”,比如,用户选择了什么铃声,是否自动更新等等。如果要为应用创建用户配置,可以使用PreferenceActivity,它可以用来创建“用户配置”。(PreferenceActivity用来创建程序中的设置界面)
3.如何读写数据
使用方法getXXX()等方法读取。
比如:
boolean silent = settings.getBoolean("silentMode", false);
4.记住密码案例
1、设置相应的权限:如果只供本程序或者有相同user id的程序使用 使用Context.MODE_PRIVATE权限;如果允许其它程序可读设置权限;如果允许其它程序可写设置相应权限;
2、SharedPreferences保存的数据最好是基础数据类型(当然也可以用来保存用户配置,官方推荐PreferenceActivtiy )。
3、如果想让程序有多个preferences文件使用getSharedPreferences(xxx , xxx);如果想让程序只有一个preferences使用getPreferences()。
4、SharedPreferences最好不要用来保存多个程序交互使用的共享数据,建议使用ContentProvider, BroadcastReceiver, and Service 来完成多个程序之间的数据共享。
代码如下
布局文件
<?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.administrator.myapp.LoginActivity">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/username_et"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:id="@+id/psw_et"/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<CheckBox
android:id="@+id/rememver_psw"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:text="记住密码"/>
</LinearLayout>
<Button
android:id="@+id/login_btn"
android:layout_width="match_parent"
android:layout_height="60dp"
android:text="Login"/>
</LinearLayout>
java代码
package com.example.administrator.myapp;
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;
public class LoginActivity extends AppCompatActivity implements View.OnClickListener{
private EditText usernameET, pswET;
private Button loginBtn;
private CheckBox checkBox;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
bindID();
SharedPreferences sharedPreferences=getSharedPreferences("user",MODE_PRIVATE);
int checked=sharedPreferences.getInt("checked",0);
if (checked==1){
String name=sharedPreferences.getString("username","");
String psw=sharedPreferences.getString("psw","");
usernameET.setText(name);
pswET.setText(psw);
checkBox.setChecked(true);
}else {
checkBox.setChecked(false);
}
}
private void bindID() {
usernameET=findViewById(R.id.username_et);
pswET=findViewById(R.id.psw_et);
loginBtn=findViewById(R.id.login_btn);
checkBox=findViewById(R.id.rememver_psw);
loginBtn.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.login_btn:
SharedPreferences sharedPreferences=getSharedPreferences("user",MODE_PRIVATE);
SharedPreferences.Editor editor=sharedPreferences.edit();
if (checkBox.isChecked()){
String username =usernameET.getText().toString();
String psw=pswET.getText().toString();
editor.putString("username",username);
editor.putString("psw",psw);
editor.putInt("checked",1);
}else {
editor.putString("username","");
editor.putString("psw","");
editor.putInt("checked",0);
}
editor.commit();
break;
}
}
}