資料持久化就是指将那些記憶體中的瞬時資料儲存到持久化裝置中(如手機檔案、資料庫等),當關機,停電後,資料不丢失。 Android 系統中主要提供了三種方式用于實作資料持久化功能,分别是: 1、檔案存儲 2、SharedPreference 存儲 3、資料庫存儲。
今日使用SharedPreference存儲實作記住登陸賬号密碼的功能:
效果圖:
![](https://img.laitimes.com/img/_0nNw4CM6IyYiwiM6ICdiwiIyVGduV2QvwVe0lmdhJ3ZvwFM38CXlZHbvN3cpR2Lc1TPB10QGtWUCpEMJ9CXsxWam9CXwADNvwVZ6l2c052bm9CXUJDT1wkNhVzLcRnbvZ2LcZXUYpVd1kmYr50MZV3YyI2cKJDT29GRjBjUIF2LcRHelR3LcJzLctmch1mclRXY39TOwkDN0MDMxEDNwITM1EDMy8CX0Vmbu4GZzNmLn9Gbi1yZtl2Lc9CX6MHc0RHaiojIsJye.jpg)
activity_main.xml中代碼:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="登陸成功,進入首頁面"
android:textSize="30sp" />
<Button
android:id="@+id/forceExit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="強制退出" />
</LinearLayout>
MainActivity中代碼:
package com.demo.rememberaccountdemo;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends BaseActivity {
private Button forceExit;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
forceExit = (Button) findViewById(R.id.forceExit);
forceExit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
// 銷毀所有的活動
ActivityCollector.finishAll();
// 從首頁面跳轉到登入頁面
Intent intent = new Intent(MainActivity.this,
LoginActivity.class);
startActivity(intent);
finish();
}
});
}
}
login.xml中代碼:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="使用者名:" />
<EditText
android:id="@+id/userName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:hint="輸入使用者名" >
<requestFocus />
</EditText>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="密 碼:" />
<EditText
android:id="@+id/password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:hint="6~16位數字、密碼" >
<requestFocus />
</EditText>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<CheckBox
android:id="@+id/remember"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="記住密碼" />
</LinearLayout>
<Button
android:id="@+id/login"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="登陸" />
</LinearLayout>
LoginActivity中的代碼:
package com.demo.rememberaccountdemo;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;
public class LoginActivity extends BaseActivity {
private SharedPreferences preferences;
private SharedPreferences.Editor editor;
private EditText userNameText;
private EditText passwordText;
private CheckBox remember;
private Button login;
private String TAG = "LoginActivity" ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
userNameText = (EditText) findViewById(R.id.userName);
passwordText = (EditText) findViewById(R.id.password);
remember = (CheckBox) findViewById(R.id.remember);
login = (Button) findViewById(R.id.login);
preferences = PreferenceManager.getDefaultSharedPreferences(this);
boolean isRemember = preferences.getBoolean("remember", false);
if (isRemember) {
// 将賬号和密碼加載到文本框中
String userName = preferences.getString("userName", "");
String password = preferences.getString("password", "");
userNameText.setText(userName);
passwordText.setText(password);
remember.setChecked(true);
}
login.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// 獲得使用者名和密碼
String userName = userNameText.getText().toString();
String password = passwordText.getText().toString();
// 判斷使用者名和密碼是否正确
if (userName.equals("admin") && password.equals("123456")) {
editor = preferences.edit();
if (remember.isChecked()) {
Log.d(TAG, "come in remember") ;
// 檢查複選框是否被選中,選中則将資料放到editor中
editor.putBoolean("remember", true);
editor.putString("userName", userName);
editor.putString("password", password);
} else {
Log.d(TAG, " not come in remember") ;
editor.clear();
}
editor.commit();// 送出資料
// 登陸成功,跳轉到首頁面
Log.d(TAG, " go to MainActivity") ;
Intent intent = new Intent(LoginActivity.this,
MainActivity.class);
startActivity(intent);
finish();
} else {
Toast.makeText(LoginActivity.this, "使用者名密碼錯誤", 1).show();
}
}
});
}
}
由于頁面的原因,ActivityCollector中的代碼,BaseActivity中的代碼,AndroidManifest.xml中的代碼,就不貼出來了,想看的,下載下傳demo裡面有,
demo下載下傳點這裡:SharedPreference存儲實戰之記住登陸賬号密碼
儲存前截圖:
儲存後截圖:
儲存值截圖: