SharedPreferences介紹:
SharedPreferences是Android平臺上一個輕量級的存儲類,主要是保存一些常用的配置參數,它是採用xml文件存放數據的,文件存放在"/data/data<package name>/shared_prefs"目錄下。
SharedPreferences的用法:
由於SharedPreferences是一個接口,而且在這個接口裏沒有提供寫入數據和讀取數據的能力。但它是通過其Editor接口中的一些方法來操作SharedPreference的,用法見下面代碼:
Context.getSharedPreferences(String name,int mode)來得到一個SharedPreferences實例
name:是指文件名稱,不需要加後綴.xml,系統會自動爲我們添加上。
mode:是指定讀寫方式,其值有三種,分別爲:
Context.MODE_PRIVATE:指定該SharedPreferences數據只能被本應用程序讀、寫
Context.MODE_WORLD_READABLE:指定該SharedPreferences數據能被其他應用程序讀,但不能寫
Context.MODE_WORLD_WRITEABLE:指定該SharedPreferences數據能被其他應用程序讀寫。
結果截圖:
工程目錄:
Java代碼
LoginActivity.java
[java] view plaincopy
<span style="font-family:'Courier New';">package com.liu.activity;
import android.app.Activity;
import android.app.backup.SharedPreferencesBackupHelper;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.text.Spannable;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;
public class LoginActivity extends Activity {
private EditText userName, password;
private CheckBox rem_pw, auto_login;
private Button btn_login;
private ImageButton btnQuit;
private String userNameValue,passwordValue;
private SharedPreferences sp;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//去除標題
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.login);
//獲得實例對象
sp = this.getSharedPreferences("userInfo", Context.MODE_WORLD_READABLE);
userName = (EditText) findViewById(R.id.et_zh);
password = (EditText) findViewById(R.id.et_mima);
rem_pw = (CheckBox) findViewById(R.id.cb_mima);
auto_login = (CheckBox) findViewById(R.id.cb_auto);
btn_login = (Button) findViewById(R.id.btn_login);
btnQuit = (ImageButton)findViewById(R.id.img_btn);
//判斷記住密碼多選框的狀態
if(sp.getBoolean("ISCHECK", false))
{
//設置默認是記錄密碼狀態
rem_pw.setChecked(true);
userName.setText(sp.getString("USER_NAME", ""));
password.setText(sp.getString("PASSWORD", ""));
//判斷自動登陸多選框狀態
if(sp.getBoolean("AUTO_ISCHECK", false))
{
//設置默認是自動登錄狀態
auto_login.setChecked(true);
//跳轉界面
Intent intent = new Intent(LoginActivity.this,LogoActivity.class);
LoginActivity.this.startActivity(intent);
}
}
// 登錄監聽事件 現在默認爲用戶名爲:liu 密碼:123
btn_login.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
userNameValue = userName.getText().toString();
passwordValue = password.getText().toString();
if(userNameValue.equals("liu")&&passwordValue.equals("123"))
{
Toast.makeText(LoginActivity.this,"登錄成功", Toast.LENGTH_SHORT).show();
//登錄成功和記住密碼框爲選中狀態才保存用戶信息
if(rem_pw.isChecked())
{
//記住用戶名、密碼、
Editor editor = sp.edit();
editor.putString("USER_NAME", userNameValue);
editor.putString("PASSWORD",passwordValue);
editor.commit();
}
//跳轉界面
Intent intent = new Intent(LoginActivity.this,LogoActivity.class);
LoginActivity.this.startActivity(intent);
//finish();
}else{
Toast.makeText(LoginActivity.this,"用戶名或密碼錯誤,請重新登錄", Toast.LENGTH_LONG).show();
}
}
});
//監聽記住密碼多選框按鈕事件
rem_pw.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
if (rem_pw.isChecked()) {
System.out.println("記住密碼已選中");
sp.edit().putBoolean("ISCHECK", true).commit();
}else {
System.out.println("記住密碼沒有選中");
sp.edit().putBoolean("ISCHECK", false).commit();
}
}
});
//監聽自動登錄多選框事件
auto_login.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
if (auto_login.isChecked()) {
System.out.println("自動登錄已選中");
sp.edit().putBoolean("AUTO_ISCHECK", true).commit();
} else {
System.out.println("自動登錄沒有選中");
sp.edit().putBoolean("AUTO_ISCHECK", false).commit();
}
}
});
btnQuit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
}
}</span>
LogoActivity.java
[java] view plaincopy
<span style="font-family:'Courier New';">package com.liu.activity;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.opengl.ETC1;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ProgressBar;
public class LogoActivity extends Activity {
private ProgressBar progressBar;
private Button backButton;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 去除標題
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.logo);
progressBar = (ProgressBar) findViewById(R.id.pgBar);
backButton = (Button) findViewById(R.id.btn_back);
Intent intent = new Intent(this, WelcomeAvtivity.class);
LogoActivity.this.startActivity(intent);
backButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
}
}
</span>
WelcomeActivity.java
[java] view plaincopy
<span style="font-family:'Courier New';"><span style="BACKGROUND-COLOR: rgb(240,240,240); WHITE-SPACE: pre">package com.liu.activity;</span>
import android.app.Activity;
import android.os.Bundle;
public class WelcomeAvtivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.welcome);
}
}</span>
【轉】http://blog.csdn.net/liuyiming_/article/details/7704923