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>
如下是sharepreference文件的保存結果:
shell@android:/data/data/com.windvally.datasavefile/shared_prefs # pwd
pwd
/data/data/com.windvally.datasavefile/shared_prefs
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 #