網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
前言
大家好,我是oy,今天介紹一下在登錄頁(yè)面中如何實(shí)現(xiàn)自動(dòng)登錄及記住密碼。
一、效果
二、設(shè)計(jì)思路
- 使用sharedPreferenced存儲(chǔ)用戶賬號(hào)和密碼,以及是否記住密碼和自動(dòng)登錄。
- 記住密碼和自動(dòng)登錄按鈕都采用checkButton,使用checkButton的OnCheckedChangeListener監(jiān)聽(tīng)。
三、知識(shí)點(diǎn)介紹
SharedPreferenced
sharedPreferenced是Android中存儲(chǔ)數(shù)據(jù)的一種方式。采用鍵值對(duì)的方式存儲(chǔ)數(shù)據(jù)。
使用過(guò)程:
- ① 獲取sharedPreferenced對(duì)象。
- ② 調(diào)用edit()獲取SharePreferenced.Editor對(duì)象。
- ③ 調(diào)用putBoolean()…等向SharePreferenced.Editor對(duì)象添加數(shù)據(jù)。
- ④ 調(diào)用apply()提交數(shù)據(jù)。
例子
// 存數(shù)據(jù)
SharedPreferences sp = getSharedPrefrences("data", MODE_PRIVATE);// 獲取sharedPreferenced對(duì)象
SharedPreferences.Editor ed = sp.edit();// 獲取SharePreferenced.Editor對(duì)象
ed.putString("name", "Sam");// 向SharePreferenced.Editor對(duì)象添加數(shù)據(jù)
ed.apply();// 調(diào)用apply()提交數(shù)據(jù),就是保存的意思
// 取數(shù)據(jù)
SharedPrefrences sp = getSharedPrefrences("data",MODE_PRIVATE);
String name = sp.getString("name","");// 取數(shù)據(jù)
checkButton就不介紹了
四、自動(dòng)登錄及記住密碼實(shí)現(xiàn)
分為兩個(gè)activity,mainActivity是登錄頁(yè)面,homeActivity是登錄成功頁(yè)面。
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的實(shí)例
sharedPreferences = this.getSharedPreferences("loginInfo", MODE_PRIVATE);
}
/**
* 用于初始化界面
*/
protected void initView() {
// 獲取sharedPreferences中remember對(duì)于的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) {
// 定義賬號(hào)和密碼的字符串
String account, password;
// 判斷賬號(hào)是否為空
if (edit_account.getText() == null) {
showToast("賬號(hào)為空,請(qǐng)重新輸入");
return;
}
// 判斷密碼是否為空
if (edit_password.getText() == null) {
showToast("密碼為空,請(qǐng)重新輸入");
return;
}
// 賬號(hào)和密碼都不為空,進(jìn)行密碼賬號(hào)校驗(yàn)
account = edit_account.getText().toString().trim();
password = edit_password.getText().toString().trim();
// 此處固定了賬號(hào)和密碼
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);// 跳轉(zhuǎn)到主界面
startActivity(intent);
// finish();
}
}
/**
* 自動(dòng)登錄邏輯
*/
private void autologin() {
// 獲取sharedPreferences中autologin對(duì)于的boolean值, true表示記住密碼
if (sharedPreferences.getBoolean("autologin", false)) {
// 勾選自動(dòng)登錄
cb_autologin.setChecked(true);
// 跳轉(zhuǎn)頁(yè)面
Intent intent = new Intent(MainActivity.this, HomeActivity.class);// 跳轉(zhuǎn)到主界面
startActivity(intent);
}
}
/**
* 用于顯示toast彈出消息
* @param text 需要顯示的文本
*/
private void showToast(final String text) {
Toast.makeText(MainActivity.this, text, Toast.LENGTH_SHORT).show();
}
// checkButton按鈕的選中監(jiān)聽(tīng)事件,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存儲(chǔ)是位于data/data/包名/shared_prefs下。是xml文件存儲(chǔ)鍵值對(duì)。
比如
<?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>
總結(jié)與補(bǔ)充
主要介紹了在登錄頁(yè)面中如何實(shí)現(xiàn)自動(dòng)登錄和記住密碼的功能,簡(jiǎn)單介紹了sharedPreferences的使用方法,這也是Android中存儲(chǔ)數(shù)據(jù)常用的方法之一。Android存儲(chǔ)還有sqlite數(shù)據(jù)庫(kù)存儲(chǔ),在另一篇文章 " Android studio登錄注冊(cè)的實(shí)現(xiàn)及介紹 " 中有講到。
原文鏈接:https://blog.csdn.net/m0_49534667/article/details/128607535
相關(guān)推薦
- 2022-06-20 C語(yǔ)言手把手帶你掌握帶頭雙向循環(huán)鏈表_C 語(yǔ)言
- 2022-07-08 python基礎(chǔ)知識(shí)之索引與切片詳解_python
- 2022-09-23 Pandas多列值合并成一列的實(shí)現(xiàn)_python
- 2022-11-19 Kotlin協(xié)程之Flow異常示例處理_Android
- 2022-04-28 C++實(shí)現(xiàn)班級(jí)成績(jī)管理系統(tǒng)_C 語(yǔ)言
- 2022-08-20 React的生命周期詳解_React
- 2022-12-13 使用Idea調(diào)試RocketMQ源碼教程_服務(wù)器其它
- 2022-09-03 詳解.NET主流的幾款重量級(jí)?ORM框架_實(shí)用技巧
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過(guò)濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支