SharedPreferences存储
定义:SharedPreferences是Android平台上一个轻量级的存储类
使用SharedPreferences写入数据步骤
- 获得使用SharedPreferences对象;
- 获得Editor对象;
- 通过Editor对象的putXXX函数,设置写入数据;
- 通过Editor对象的commit提交写入。
使用ShardPreferences要注意的:
1.SharedPreferences对象本身只能读取,而不能编辑、删除和储存,要操作数据必须使用edit方法。
2.使用edit方法操作数据之后一定要使用Preferences中的edito.commit();方法提交,才能成功操作。
3.SharedPreferences使用键值对的方式储存数据,因此无法操作过于复杂的数据。
调用Context.getSharePreferences(String name, int mode)方法来得到SharePreferences接口,该方法的第一个参数是文件名称,第二个参数是操作模式。
操作模式有三种:MODE_PRIVATE(私有) ,MODE_WORLD_READABLE(可读),MODE_WORLD_WRITEABLE(可写)
SharePreference提供了获得数据的方法,如getString(String key,String defValue)等
调用SharePreferences的edit()方法返回SharePreferences.Editor内部接口,该接口提供了保存数据的方法如:putString(String key,String value)等,调用该接口的commit()方法可以将数据保存。
android 利用SharedPreferences做的简单记住密码+自动登陆代码展示
xml文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
tools:context="com.example.administrator.sharedapplication.MainActivity">
<EditText
android:id="@+id/username_et"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="输入账号" />
<EditText
android:id="@+id/password_et"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:text="输入密码" />
<Button
android:id="@+id/login_btn"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="登录" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:orientation="horizontal">
<CheckBox
android:id="@+id/rememberPswCk"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="70dp"
android:checked="false"
android:text="记住密码" />
<CheckBox
android:id="@+id/autoLoginCk"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:layout_weight="1"
android:checked="false"
android:text="自动登录" />
</LinearLayout>
</LinearLayout>
java文件
package com.example.administrator.sharedapplication;
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 MainActivity extends AppCompatActivity {
private EditText usernameEt;
private EditText passwordEt;
private Button loginBtn;
private CheckBox rememberPswCk;
private CheckBox autoLoginCk;
//创建flag用处:判断上次登录是否点击checkbox;
private int rememberFlag = 0;
private String password="";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bindID();
//从sp文件(mysp.xml)里取出“name”节点对应的值
SharedPreferences sharedPreferences = getSharedPreferences("mysp.xml",MODE_PRIVATE);
if (sharedPreferences!=null){
//获得存贮的数据
String name = sharedPreferences.getString("name","");
password = sharedPreferences.getString("password","");
//赋值给usernameET
usernameEt.setText(name);
rememberFlag = sharedPreferences.getInt("remember_flag",0);
}
//更具读取的数据,判断上次是否点击CheckBox
if (rememberFlag==1){
rememberPswCk.setChecked(true);
passwordEt.setText(password);
}
loginBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String username = usernameEt.getText().toString();
String password = passwordEt.getText().toString();
//SharedPreferences
//1、创建SharedPreferences对象
SharedPreferences spf = getSharedPreferences("mysp.xml",MODE_PRIVATE);
//2、创建Editor对象,写入值
SharedPreferences.Editor editor = spf.edit();
editor.putString("name",username);
if (rememberPswCk.isChecked()){
rememberFlag = 1;
editor.putInt("remember_flag",rememberFlag);
editor.putString("password",password);
}else {
rememberFlag = 0;
editor.putInt("remember_flag",rememberFlag);
}
//3、提交
editor.commit();
//提示登陆成功
Toast.makeText(MainActivity.this,"登录成功",Toast.LENGTH_SHORT).show();
}
});
}
private void bindID() {
usernameEt=findViewById(R.id.username_et);
passwordEt=findViewById(R.id.password_et);
loginBtn=findViewById(R.id.login_btn);
rememberPswCk=findViewById(R.id.remember_psw_ck);
autoLoginCk=findViewById(R.id.auto_login_ck);
}
}