Android大作業(yè)功能設計之自動登錄和記住密碼
前言
大家好,我是oy,今天介紹一下在登錄頁面中如何實現(xiàn)自動登錄及記住密碼。
一、效果

二、設計思路
- 使用sharedPreferenced存儲用戶賬號和密碼,以及是否記住密碼和自動登錄。
- 記住密碼和自動登錄按鈕都采用checkButton,使用checkButton的OnCheckedChangeListener監(jiān)聽。
三、知識點介紹
SharedPreferenced
sharedPreferenced是Android中存儲數(shù)據(jù)的一種方式。采用鍵值對的方式存儲數(shù)據(jù)。
使用過程:
- ① 獲取sharedPreferenced對象。
- ② 調用edit()獲取SharePreferenced.Editor對象。
- ③ 調用putBoolean()…等向SharePreferenced.Editor對象添加數(shù)據(jù)。
- ④ 調用apply()提交數(shù)據(jù)。
例子
// 存數(shù)據(jù)
SharedPreferences sp = getSharedPrefrences("data", MODE_PRIVATE);// 獲取sharedPreferenced對象
SharedPreferences.Editor ed = sp.edit();// 獲取SharePreferenced.Editor對象
ed.putString("name", "Sam");// 向SharePreferenced.Editor對象添加數(shù)據(jù)
ed.apply();// 調用apply()提交數(shù)據(jù),就是保存的意思
// 取數(shù)據(jù)
SharedPrefrences sp = getSharedPrefrences("data",MODE_PRIVATE);
String name = sp.getString("name","");// 取數(shù)據(jù)
checkButton就不介紹了
四、自動登錄及記住密碼實現(xiàn)
分為兩個activity,mainActivity是登錄頁面,homeActivity是登錄成功頁面。
HomeActivity.java代碼
public class HomeActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
}
}
activity_home.xml代碼
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".HomeActivity">
<TextView
android:id="@+id/tv_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/text"
android:textSize="26sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java代碼
private AppCompatEditText edit_account, edit_password;
private CheckBox cb_remember, cb_autologin;
private SharedPreferences sharedPreferences;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bindView();
initView();
}
/**
*用于綁定控件id的方法
*/
protected void bindView() {
edit_account = findViewById(R.id.edit_account);
edit_password = findViewById(R.id.edit_password);
cb_remember = findViewById(R.id.cb_remember);
cb_remember.setOnCheckedChangeListener(this);
cb_autologin = findViewById(R.id.cb_autologin);
cb_autologin.setOnCheckedChangeListener(this);
Button btn_login = findViewById(R.id.btn_login);
btn_login.setOnClickListener(this);
// 獲取SharedPreferences的實例
sharedPreferences = this.getSharedPreferences("loginInfo", MODE_PRIVATE);
}
/**
* 用于初始化界面
*/
protected void initView() {
// 獲取sharedPreferences中remember對于的boolean值,true表示記住密碼
if (sharedPreferences.getBoolean("remember", false)) {
cb_remember.setChecked(true);
edit_account.setText(sharedPreferences.getString("account", ""));
edit_password.setText(sharedPreferences.getString("password",""));
autologin();
}
}
// 登錄按鈕的邏輯
@Override
public void onClick(View view) {
// 定義賬號和密碼的字符串
String account, password;
// 判斷賬號是否為空
if (edit_account.getText() == null) {
showToast("賬號為空,請重新輸入");
return;
}
// 判斷密碼是否為空
if (edit_password.getText() == null) {
showToast("密碼為空,請重新輸入");
return;
}
// 賬號和密碼都不為空,進行密碼賬號校驗
account = edit_account.getText().toString().trim();
password = edit_password.getText().toString().trim();
// 此處固定了賬號和密碼
if (account.equals("admin") && password.equals("12345")) {
if (cb_remember.isChecked()) {
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("account", account);
editor.putString("password", password);
editor.apply();
}
showToast("登錄成功");
Intent intent = new Intent(MainActivity.this, HomeActivity.class);// 跳轉到主界面
startActivity(intent);
// finish();
}
}
/**
* 自動登錄邏輯
*/
private void autologin() {
// 獲取sharedPreferences中autologin對于的boolean值, true表示記住密碼
if (sharedPreferences.getBoolean("autologin", false)) {
// 勾選自動登錄
cb_autologin.setChecked(true);
// 跳轉頁面
Intent intent = new Intent(MainActivity.this, HomeActivity.class);// 跳轉到主界面
startActivity(intent);
}
}
/**
* 用于顯示toast彈出消息
* @param text 需要顯示的文本
*/
private void showToast(final String text) {
Toast.makeText(MainActivity.this, text, Toast.LENGTH_SHORT).show();
}
// checkButton按鈕的選中監(jiān)聽事件,compoundButton指的是checkButton控件, isChecked指的是是否勾選
@SuppressLint("NonConstantResourceId")
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
switch (compoundButton.getId()) {
case R.id.cb_remember:
if (isChecked) {
sharedPreferences.edit().putBoolean("remember", true).apply();
} else {
sharedPreferences.edit().putBoolean("remember", false).apply();
}
break;
case R.id.cb_autologin:
if (isChecked) {
sharedPreferences.edit().putBoolean("autologin", true).apply();
} else {
sharedPreferences.edit().putBoolean("autologin", false).apply();
}
break;
}
}
sharedPreferenced存儲是位于data/data/包名/shared_prefs下。是xml文件存儲鍵值對。
比如
<?xml version='1.0' encoding='utf-8' standalone='yes' ?> <map> <boolean name="remember" value="true" /> <boolean name="autologin" value="true" /> <string name="password">12345</string> <string name="account">admin</string> </map>
總結與補充
主要介紹了在登錄頁面中如何實現(xiàn)自動登錄和記住密碼的功能,簡單介紹了sharedPreferences的使用方法,這也是Android中存儲數(shù)據(jù)常用的方法之一。Android存儲還有sqlite數(shù)據(jù)庫存儲,在另一篇文章 " Android studio登錄注冊的實現(xiàn)及介紹 " 中有講到。
到此這篇關于Android大作業(yè)功能設計之自動登錄和記住密碼的文章就介紹到這了,更多相關Android自動登錄和記住密碼內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Android ListView滾動到底后自動加載數(shù)據(jù)
這篇文章主要為大家詳細介紹了Android之ListView滾動到底后自動加載數(shù)據(jù),文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-09-09
Android使用Circular Reveal動畫讓頁面跳轉更炫酷
本篇文章主要介紹了Android使用Circular Reveal動畫讓頁面跳轉更炫酷,具有一定的參考價值,有興趣的可以了解一下2017-08-08

