SharedPreferences以键值对的形式将数据存储在XML文件中,通常是用来存储一些程序的配置文件,存储位置在"data/data/程序包名/shared_prefs"目录下。
本例为:输入账号和密码,如果点击"验证"按钮,则会自动判断当前输入的数据和以在程序中写入到SharedPreferences中的数据是否相同.
同时如果勾选上左下角的勾选框则会在这个Activity退出时将数据保存到SharedPreferences中去,下次再打开本例会自动将账号和密码显示到EditText上.
主程序:
package com.example.sharepreferences;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity {
private SharedPreferences sharedpreferences;
private EditText edit1;
private EditText edit2;
private Editor editor;
private CheckBox check;
private static boolean flag;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);
edit1 = (EditText) findViewById(R.id.editview1);
edit2 = (EditText) findViewById(R.id.editview2);
Button btn1 = (Button) findViewById(R.id.button1);
btn1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
//验证输入的账号和密码是否和SharedPreferences中保存的信息一致
if (edit1.getText().toString().equals(sharedpreferences.getString("账号", "666666"))&& edit2.getText().toString().equals(sharedpreferences.getString("密码","3838"))) {
Toast.makeText(MainActivity.this, "账号和密码正确",Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "验证错误 ",Toast.LENGTH_SHORT).show();
}
}
});
//获取SharedPreferences对象并且设置文件名为"test",操作模式为MODE_PRIVATE默认模式
sharedpreferences = getSharedPreferences("test", MODE_PRIVATE);
//获取Editor对象
editor = sharedpreferences.edit();
check = (CheckBox) findViewById(R.id.checkBox1);
check.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, "记住密码了", Toast.LENGTH_SHORT).show();
}
});
if(flag){
//再次启动Activity时判断CheckBox勾选框上次结束时是否被选中,如果选中则将SharedPreferences中保存的数据显示到EditText上
edit1.setText(sharedpreferences.getString("账号",""));
edit2.setText(sharedpreferences.getString("密码",""));
flag = false;
}
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
//在Activity结束时判断CheckBox勾选框是否被选中,如果选中则将以输入的数据保存到SharedPreferences中
if (check.isChecked()) {
flag = true;
//通过Editor的putBoolean() 或 putString()等方法存入值,最后使用commit()方法提交数据
editor.putString("账号", edit1.getText().toString());
editor.putString("密码", edit2.getText().toString());
editor.commit();
}
}
}
------------------------------------------------------------------------------------------------------ 布局文件:
<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" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="50dp"
android:orientation="horizontal" >
<TextView
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="账 号:"
android:gravity="center"
android:textSize="21sp" />
<EditText
android:id="@+id/editview1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FF00FFFF"
android:hint="请输入六位账号" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="50dp"
android:orientation="horizontal" >
<TextView
android:id="@+id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="密 码:"
android:gravity="center"
android:textSize="21sp" />
<EditText
android:id="@+id/editview2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FF00FFFF"
android:hint="请输入六位密码" />
</LinearLayout>
<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="记住密码" />
<Button
android:id="@+id/button1"
android:layout_width="96dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="验证" />
</LinearLayout>