網站首頁 編程語言 正文
本文實例為大家分享了Android實現登陸界面記住密碼功能的具體代碼,供大家參考,具體內容如下
所需工具
一、復選框控件:CheckBox,
二、SharedPreferences用于存儲數據,該工具的讀取和寫入較為簡單,放在代碼里的注釋中解釋
實現邏輯:
如果沒弄懂邏輯,代碼看起來還是有點小難度的
一、判斷SharedPreferences中已存入的CheckBox的Boolean信息(沒有讀取到則默認條件為“否”),如果條件為“是”(同時滿足能讀取到和讀取的信息為“是”兩個條件),通過SharedPreferences將存儲的數據(account和password)讀取出來并寫入對應的文本框。
二、點擊登錄按鍵時,判斷CheckBox是否勾選,如果條件為“是”,則將accout和password框里的數據(String)以及CheckBox的數據(Boolean)寫入SharedPreferences,若沒有勾選,則清除SharedPreferences中的數據。
實現代碼
一、ui界面
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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:orientation="vertical" ? ? android:layout_width="match_parent" ? ? android:layout_height="match_parent" ? ? tools:context=".LoginActivity"> <LinearLayout ? ? android:orientation="horizontal" ? ? android:layout_width="match_parent" ? ? android:layout_height="60dp"> ? ? <TextView ? ? ? ? android:layout_width="90dp" ? ? ? ? android:text="Account" ? ? ? ? android:layout_height="wrap_content" ? ? ? ? android:textSize="18sp" ? ? ? ? android:layout_gravity="center" ? ? ? ? /> ? ? <EditText ? ? ? ? android:layout_width="0dp" ? ? ? ? android:layout_height="wrap_content" ? ? ? ? android:layout_weight="1" ? ? ? ? android:layout_gravity="center" ? ? ? ? android:id="@+id/account"/> </LinearLayout> <LinearLayout ? ? android:layout_width="match_parent" ? ? android:layout_height="60dp"> ? ? <TextView ? ? ? ? android:layout_width="90dp" ? ? ? ? android:text="Password" ? ? ? ? android:layout_height="wrap_content" ? ? ? ? android:textSize="18sp" ? ? ? ? android:layout_gravity="center"/> ? ? <EditText ? ? ? ? android:layout_width="0dp" ? ? ? ? android:layout_height="wrap_content" ? ? ? ? android:layout_weight="1" ? ? ? ? android:layout_gravity="center" ? ? ? ? android:id="@+id/password"/> </LinearLayout> ? ? <LinearLayout ? ? ? ? android:layout_width="match_parent" ? ? ? ? android:layout_height="wrap_content" ? ? ? ? android:orientation="horizontal"> ? ? ? ? <CheckBox ? ? ? ? ? ? android:layout_width="wrap_content" ? ? ? ? ? ? android:layout_height="wrap_content" ? ? ? ? ? ? android:id="@+id/remember_pass"/> ? ? ? ? <TextView ? ? ? ? ? ? android:layout_width="wrap_content" ? ? ? ? ? ? android:layout_height="wrap_content" ? ? ? ? ? ? android:textSize="18sp" ? ? ? ? ? ? android:text="Rember password"/> ? ? </LinearLayout> ? ? <Button ? ? ? ? android:layout_width="match_parent" ? ? ? ? android:layout_height="60dp" ? ? ? ? android:text="LogIn" ? ? ? ? android:id="@+id/login"/> </LinearLayout>
二、實現功能部分
package com.example.broadcastbestpractice;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;
public class LoginActivity extends BaseActivity {
? ? private EditText accountEdit;
? ? private EditText passwordEdit;
? ? private Button login;
? ? private SharedPreferences pref;//通過pref讀取SharedPreferences的數據
? ? private SharedPreferences.Editor editor;//editor將數據寫入SharedPreferences
? ? private CheckBox rememberPass;
? ? @Override
? ? protected void onCreate(Bundle savedInstanceState) {
? ? ? ? super.onCreate(savedInstanceState);
? ? ? ? setContentView(R.layout.activity_login);
? ? ? ? pref= PreferenceManager.getDefaultSharedPreferences(this);
? ? ? ? accountEdit = (EditText) findViewById(R.id.account);
? ? ? ? passwordEdit = (EditText) findViewById(R.id.password);
? ? ? ? rememberPass=(CheckBox)findViewById(R.id.remember_pass);
? ? ? ? login = (Button) findViewById(R.id.login);
? ? ? ? boolean isRemenber=pref.getBoolean("remember_password",false);//讀取上次登陸時存入"remember_password"的信息,沒有讀取到則默認為false
? ? ? ? if(isRemenber)//如果讀取為true,則將account和password,checkbox的信息寫入文本框
? ? ? ? {
? ? ? ? ? ? String account=pref.getString("account","");
? ? ? ? ? ? String password=pref.getString("password","");
? ? ? ? ? ? accountEdit.setText(account);
? ? ? ? ? ? passwordEdit.setText(password);
? ? ? ? ? ? rememberPass.setChecked(true);
? ? ? ? }
? ? ? ? login.setOnClickListener(new View.OnClickListener() {
? ? ? ? ? ? @Override
? ? ? ? ? ? public void onClick(View v) {
? ? ? ? ? ? ? ? String accout = accountEdit.getText().toString();
? ? ? ? ? ? ? ? String password = passwordEdit.getText().toString();
? ? ? ? ? ? ? ? if (accout.equals("1") && password.equals("1")) {
? ? ? ? ? ? ? ? ? ? editor=pref.edit();
? ? ? ? ? ? ? ? ? ? if(rememberPass.isChecked()){//如果勾選了checkbox框,則將account,password,checkbox信息寫入
? ? ? ? ? ? ? ? ? ? ? ? editor.putBoolean("remember_password",true);
? ? ? ? ? ? ? ? ? ? ? ? editor.putString("account",accout);
? ? ? ? ? ? ? ? ? ? ? ? editor.putString("password",password);
? ? ? ? ? ? ? ? ? ? }else {
? ? ? ? ? ? ? ? ? ? ? ? editor.clear();//若沒有,清除SharedPreferences存儲的信息
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? editor.apply();
? ? ? ? ? ? ? ? ? ? Intent intent = new Intent(LoginActivity.this, MainActivity.class);
? ? ? ? ? ? ? ? ? ? startActivity(intent);
? ? ? ? ? ? ? ? ? ? finish();
? ? ? ? ? ? ? ? } else
? ? ? ? ? ? ? ? ? ? Toast.makeText(LoginActivity.this, "account or password is wrong", Toast.LENGTH_SHORT).show();
? ? ? ? ? ? }
? ? ? ? });
? ? }
}
功能完善
我在app內部添加了一個強制下線功能,這樣,進入app后點擊下線按鈕就能直接退出到登陸界面,會使效果更加直觀。
原文鏈接:https://blog.csdn.net/It_is_IT_/article/details/104440902
相關推薦
- 2022-12-19 教你react中如何理解usestate、useEffect副作用、useRef標識和useCont
- 2023-04-29 Python腳本修改Maya?ASCII文件路徑方法實現_python
- 2022-08-26 Python?Matplotlib?marker?標記詳解_python
- 2022-04-22 mac安裝oh-my-zsh出現command not found: npm問題解決
- 2022-07-18 Linux文件系統和日志分析
- 2021-12-26 WebStorm?發布2021.3重大更新新功能介紹_其它綜合
- 2022-08-18 Go?Web編程添加服務器錯誤和訪問日志_Golang
- 2022-12-25 Redis過期鍵與內存淘汰策略深入分析講解_Redis
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支