1、實現記住密碼功能
- 利用上一節的內容,咱們來實現一個記住密碼的功能,咱們直接修改
BroadcastBestPractice
項目中的代碼。
- 首先修改
login.xm
中的代碼
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:stretchColumns="1" >
.........省略代碼.............
<TableRow>
<CheckBox
android:id="@+id/remember_pass"
android:layout_height="wrap_content" />
<TextView
android:layout_height="wrap_content"
android:text="Remember password" />
</TableRow>
<TableRow>
<Button
android:id="@+id/login"
android:layout_height="wrap_content"
android:layout_span="2"
android:text="Login" />
</TableRow>
</TableLayout>
- 這裏使用到一個新的控件
CheckBox
,這是一個複選框控件,用戶能夠經過點擊來實現選中和取消,咱們就是用這個控件來表示用戶是否須要記住密碼。
- 修改LoginActivity中的代碼
package com.example.broadcastbestpractice;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.preference.PreferenceManager;
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 pref;
private Editor editor;
private CheckBox rememberPass;
private EditText accountEdit;
private EditText passwordEdit;
private Button login;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
pref = PreferenceManager.getDefaultSharedPreferences(this);
accountEdit = (EditText)findViewById(R.id.account);
rememberPass = (CheckBox)findViewById(R.id.remember_pass);
boolean isRemember = pref.getBoolean("remember_password", false);
passwordEdit = (EditText)findViewById(R.id.password);
login = (Button) findViewById(R.id.login);
if(isRemember) {
//將帳號和密碼都設置到文本框中
String account = pref.getString("account", "");
String password = pref.getString("password", "");
accountEdit.setText(account);
passwordEdit.setText(password);
rememberPass.setText(true);
}
login.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String account =accountEdit.getText().toString();
String password = passwordEdit.getText().toString();
//若是帳號是admin,密碼是12345,就認爲登陸成功
if(account.equals("admin") && password.equals("12345")) {
editor = pref.edit();
if(rememberPass.isChecked()) {
//檢查複選框是否被選中
editor.putBoolean("remember_password", true);
editor.putString("account",account);
editor.putString("password", password);
}else {
editor.clear();
}
editor.commit();
Intent intent = new Intent(LoginActivity.this,MainActivity.class);
startActivity(intent);
finish();
}else {
Toast.makeText(LoginActivity.this,"account or password is invalid",Toast.LENGTH_SHORT).show();
}
}
});
}
}

- 首先獲取到SharedPrefereces對象,而後調用了getBoolean()方法獲取remember_password這個鍵對應的值,一開始固然不存在,那麼就會使用默認值false,這樣就什麼都不會發生,接着在登陸成功以後會調用CheckBox的isCreated()方法來檢查複選框是否被選中。若是選中了說明了用戶想要記住密碼,這時候將remember_password設置爲true,而後把account和password對應的值都存入到SharedPerences文件中而且提交,若是沒有選中的話,那麼就會調用clear()方法,將剛纔輸入的帳戶和密碼的文本刪除一下。
- 注意:這樣存儲的密碼是明文的,這是很是不安全的,正式項目會使用加密算法對密碼進行加密。
2、SQLite數據存儲
- 安卓內置了SQLite這種輕量級數據庫,支持SQL語言以及ACID事務,能夠存儲結構複雜的數據,以前的兩種方式都是很簡單的方式,遠不能知足咱們的要求。
- 具體使用方式咱們下次再說
3、源碼: