一、實作記住密碼功能
- 利用上一節的內容,我們來實作一個記住密碼的功能,我們直接修改
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()方法,將剛才輸入的賬戶和密碼的文本洗掉一下,
- 注意:這樣存盤的密碼是明文的,這是非常不安全的,正式專案會使用加密演算法對密碼進行加密,
二、SQLite資料存盤
- 安卓內置了SQLite這種輕量級資料庫,支持SQL語言以及ACID事務,可以存盤結構復雜的資料,之前的兩種方式都是很簡單的方式,遠不能滿足我們的要求,
- 具體使用方式我們下次再說
三、原始碼:
- BroadcastBestPractice
- 地址:https://github.com/ruigege66/Android/tree/master/BroadcastBestPractice
- CSDN:https://blog.csdn.net/weixin_44630050
- 博客園:https://www.cnblogs.com/ruigege0000/
- 歡迎關注微信公眾號:傅里葉變換,個人賬號,僅用于技術交流
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/106808.html
標籤:其他
下一篇:移動端開發初識
