SharedPreferences的使用步骤:
1、得到SharedPreferences对象。
2、调用SharedPreferences对象的edit()方法来获取一个SharedPreferences.Editor对象。
3、向SharedPreferences.Editor对象中添加数据。
4、调用commit方法将添加的数据提交。
案例:用户登录记住密码
1、login_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/drawable_rounder"
android:padding="@dimen/activity_horizontal_margin">
<EditText
android:id="@+id/EtName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/etname"
android:ems="10"
android:drawablePadding="10dp"
android:background="@android:drawable/edit_text"
android:drawableLeft="@drawable/icon_user" >
<requestFocus/>
</EditText>
<EditText
android:id="@+id/EtPass"
android:layout_below="@id/EtName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/etpass"
android:drawablePadding="10dp"
android:inputType="textPassword"
android:ems="10"
android:background="@android:drawable/edit_text"
android:drawableLeft="@drawable/icon_pass">
<requestFocus/>
</EditText>
<CheckBox
android:id="@+id/cbpass"
android:layout_below="@id/EtPass"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="记住密码"
android:textSize="20dp"
android:layout_margin="5dp"
android:checked="false"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/cbpass">
<Button
android:id="@+id/btn"
android:onClick="login"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/de"
android:layout_marginRight="10dp"
android:background="@drawable/bt_press"
/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/zhuce"
android:background="@drawable/bt_press"
/>
</LinearLayout>
</RelativeLayout>
2、activity_login.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:background="@drawable/loginbg"
android:id="@+id/activity_login"
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="bzu.edu.cn.case_login.LoginActivity">
<include layout="@layout/loginlayout"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@drawable/deer"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginRight="23dp"
android:layout_marginEnd="23dp"
android:id="@+id/imageView" />
</RelativeLayout>
package bzu.edu.cn.case_login;
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 EditText Ename,Epass;
private CheckBox cb;
private Button btn;
SharedPreferences sharedPreferences;
SharedPreferences.Editor editor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//获取控件
Ename=(EditText)findViewById(R.id.EtName);
Epass=(EditText)findViewById(R.id.EtPass);
cb=(CheckBox)findViewById(R.id.cbpass);
//通过getSharedPreferences()方法获取SharePreferences的实例对象
sharedPreferences=getSharedPreferences("Login",MODE_PRIVATE);
//获取Editor实例对象,存储数据
editor=sharedPreferences.edit();
//获取数据
String name=sharedPreferences.getString("name","");
String pass=sharedPreferences.getString("pass","");
//判断文本框是否为空
if(name==null&&pass==null){
cb.setChecked(false);
}
else{
cb.setChecked(true);
Ename.setText(name);
Epass.setText(pass);
}
}
public void login(View view){
//获取文本框的内容
String name = Ename.getText().toString().trim();
String pass=Epass.getText().toString().trim();
//检验用户名和密码是否正确
if("admin".equals(name)&&"123456".equals(pass)){
if(cb.isChecked()){
//保存数据
editor.putString("name",name);
editor.putString("pass",pass);
editor.commit(); //提交数据
}else{
editor.clear(); //删除所有数据
editor.commit(); //提交修改
}
Toast.makeText(LoginActivity.this,"登录成功",Toast.LENGTH_LONG).show();
}else{
Toast.makeText(this,"用户名或密码错误",Toast.LENGTH_LONG).show();
}
}
}