package com.windvally.sharedpreferencestest;
import com.windvally.datasavefile.R;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Menu;
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.TextView;
public class SharedPreferencesTest extends Activity {
private EditText username_ET, password_ET;
private String username, password, lastusername_input, result = "";
private Button loginButton;
private CheckBox rem_username_CB, rem_password_CB, auto_login_CB;
private boolean if_rem_username, if_rem_password, if_auto_login;
private SharedPreferences lastusername, saveuserinfo;
private TextView resultTV;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_shared_preferences_test);
username_ET = (EditText) findViewById(R.id.username);
password_ET = (EditText) findViewById(R.id.password);
loginButton = (Button) findViewById(R.id.login);
rem_username_CB = (CheckBox) findViewById(R.id.rem_user);
rem_password_CB = (CheckBox) findViewById(R.id.rem_psw);
auto_login_CB = (CheckBox) findViewById(R.id.auto_login);
resultTV = (TextView) findViewById(R.id.result);
//首先,检查sharedpreference里保存的最后一次登录的用户名,根据用户名检查该用户保存的用户名密码和三个checkbox的状态来完成初始化。
init();
//初始化完成判断是否自动登录。如自动登录被选中,则开始自动登录。
check_autolog_selected();
//添加用户名输入框的输入字符的事件
username_ET.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
// TODO Auto-generated method stub
String temp_username = username_ET.getText().toString().trim();
saveuserinfo = getSharedPreferences(temp_username, 0);
password = saveuserinfo.getString("password", "");
if_rem_username = saveuserinfo
.getBoolean("rem_username", false);
if_rem_password = saveuserinfo
.getBoolean("rem_password", false);
if_auto_login = saveuserinfo.getBoolean("auto_login", false);
rem_username_CB.setChecked(if_rem_username);
rem_password_CB.setChecked(if_rem_password);
auto_login_CB.setChecked(if_auto_login);
password_ET.setText(password);
rem_username_CB.setChecked(if_rem_username);
rem_password_CB.setChecked(if_rem_password);
auto_login_CB.setChecked(if_auto_login);
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
});
//添加记住用户名选项的处理方法
rem_username_CB
.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
// TODO Auto-generated method stub
if (!isChecked) {
rem_password_CB.setChecked(false);
auto_login_CB.setChecked(false);
}
}
});
//添加记住密码选项的处理方法
rem_password_CB
.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
// TODO Auto-generated method stub
if (isChecked) {
rem_username_CB.setChecked(true);
}
if (!isChecked) {
auto_login_CB.setChecked(false);
}
}
});
//添加添加记住密码选项的处理方法
auto_login_CB.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
// TODO Auto-generated method stub
if (isChecked) {
rem_username_CB.setChecked(true);
rem_password_CB.setChecked(true);
}
}
});
loginButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
checkandsaveSP();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.shared_preferences_test, menu);
return true;
}
private void init(){
//初始化数据,读取sharedpreference里的数据填充各表现。
//lastusername是用来保存最近一次输入的用户名
lastusername = getSharedPreferences("lastusername", 0);
lastusername_input = lastusername.getString("lastusername", "");
//取出用户名后,读取这个用户名对应的密码和选项框状态
saveuserinfo = getSharedPreferences(lastusername_input, 0);
username = saveuserinfo.getString("username", "");
password = saveuserinfo.getString("password", "");
if_rem_username = saveuserinfo.getBoolean("rem_username", false);
if_rem_password = saveuserinfo.getBoolean("rem_password", false);
if_auto_login = saveuserinfo.getBoolean("auto_login", false);
rem_username_CB.setChecked(if_rem_username);
rem_password_CB.setChecked(if_rem_password);
auto_login_CB.setChecked(if_auto_login);
username_ET.setText(username);
password_ET.setText(password);
result += "username:" + username + "\n";
result += "password:" + password + "\n";
result += "if_rem_username:" + if_rem_username + "\n";
result += "if_rem_password:" + if_rem_password + "\n";
result += "if_auto_login:" + if_auto_login + "\n";
resultTV.setText(result);
}
private void check_autolog_selected(){
//如果选中了自动登录则开始自动登录
if(if_auto_login){
login(username,password);
}
}
private void login(String username,String password){
if (username.equals("aaaa")&&password.equals("aaaa")){
//这里可以加入代码进行页面跳转。示例只是弹出登录成功的对话框
AlertDialog.Builder passBuilder =new AlertDialog.Builder(this);
passBuilder.setTitle("登录成功");
passBuilder.setCancelable(false);
passBuilder.setNegativeButton("确定",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
});
passBuilder.create();
passBuilder.show();
}
else{
AlertDialog.Builder passBuilder =new AlertDialog.Builder(this);
passBuilder.setTitle("用户名密码错误");
passBuilder.setCancelable(false);
passBuilder.setNegativeButton("确定",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
//密码错误不做任何处理
}
});
passBuilder.create();
passBuilder.show();
}
}
private void checkandsaveSP(){
//点击登陆后检查所有选项,记录最后登录的用户名,按选项将用户名密码和三个选项框状态写入Sharepreference中
// TODO Auto-generated method stub
username = username_ET.getText().toString().trim();
password = password_ET.getText().toString().trim();
if_rem_username = rem_username_CB.isChecked();
if_rem_password = rem_password_CB.isChecked();
if_auto_login = auto_login_CB.isChecked();
lastusername.edit().putString("lastusername", username)
.commit();
lastusername.edit().commit();
saveuserinfo = getSharedPreferences(username, 0);
if (if_rem_username) {
saveuserinfo.edit().putString("username", username)
.commit();
}
if(!if_rem_username){
saveuserinfo.edit().putString("username", "").commit();
}
if (if_rem_password) {
saveuserinfo.edit().putString("password", password)
.commit();
}
if(!if_rem_password){
saveuserinfo.edit().putString("password", "").commit();
}
saveuserinfo.edit().putBoolean("rem_username", if_rem_username)
.commit();
saveuserinfo.edit().putBoolean("rem_password", if_rem_password)
.commit();
saveuserinfo.edit().putBoolean("auto_login", if_auto_login)
.commit();
result = "";
result += "lastusername is: " + username + "\n";
result += "username:" + username + "\n";
result += "password:" + password + "\n";
result += "if_rem_username:" + if_rem_username + "\n";
result += "if_rem_password:" + if_rem_password + "\n";
result += "if_auto_login:" + if_auto_login + "\n";
resultTV.setText(result);
//进行登录处理
login(username,password);
}
}
对应的布局文件
<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:gravity="center"
android:orientation="vertical" >
<TextView
android:id="@+id/welcom_welcome"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#B67044"
android:singleLine="true"
android:text="欢迎光临SharedPreference测试页面" />
<TableLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="用户名"
android:textSize="20sp" />
<EditText
android:id="@+id/username"
android:layout_width="140sp"
android:layout_height="wrap_content"
android:inputType="text"
android:maxLength="16"
android:singleLine="true" />
</TableRow>
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="密码"
android:textSize="20sp" />
<EditText
android:id="@+id/password"
android:layout_width="140sp"
android:layout_height="wrap_content"
android:maxLength="16"
android:singleLine="true" />
</TableRow>
</TableLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<CheckBox
android:id="@+id/rem_user"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="记住用户"
android:textSize="12px" >
</CheckBox>
<CheckBox
android:id="@+id/rem_psw"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="记住密码"
android:textSize="12px" >
</CheckBox>
<CheckBox
android:id="@+id/auto_login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="自动登录"
android:textSize="12px" >
</CheckBox>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<Button
android:id="@+id/login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="登录" />
<Button
android:id="@+id/regist"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="注册" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="忘记密码" />
</LinearLayout>
<TextView
android:id="@+id/result"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
sharedpreference文件的保存结果
shell@android:/data/data/com.windvally.datasavefile/shared_prefs # ls
ls
UserInfo.xml
aaa.xml
aaaa.xml
bbbb.xml
cccc.xml
dddd.xml
lastusername.xml
qqqq.xml
shell@android:/data/data/com.windvally.datasavefile/shared_prefs #
当然在实际应用中需要对密码进行加密处理,这里就不赘述了。