Android實(shí)現(xiàn)登陸界面的記住密碼功能
本文實(shí)例為大家分享了Android實(shí)現(xiàn)登陸界面記住密碼功能的具體代碼,供大家參考,具體內(nèi)容如下
所需工具
一、復(fù)選框控件:CheckBox,
二、SharedPreferences用于存儲(chǔ)數(shù)據(jù),該工具的讀取和寫入較為簡(jiǎn)單,放在代碼里的注釋中解釋
實(shí)現(xiàn)邏輯:
如果沒(méi)弄懂邏輯,代碼看起來(lái)還是有點(diǎn)小難度的
一、判斷SharedPreferences中已存入的CheckBox的Boolean信息(沒(méi)有讀取到則默認(rèn)條件為“否”),如果條件為“是”(同時(shí)滿足能讀取到和讀取的信息為“是”兩個(gè)條件),通過(guò)SharedPreferences將存儲(chǔ)的數(shù)據(jù)(account和password)讀取出來(lái)并寫入對(duì)應(yīng)的文本框。
二、點(diǎn)擊登錄按鍵時(shí),判斷CheckBox是否勾選,如果條件為“是”,則將accout和password框里的數(shù)據(jù)(String)以及CheckBox的數(shù)據(jù)(Boolean)寫入SharedPreferences,若沒(méi)有勾選,則清除SharedPreferences中的數(shù)據(jù)。
實(shí)現(xiàn)代碼
一、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>
二、實(shí)現(xiàn)功能部分
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;//通過(guò)pref讀取SharedPreferences的數(shù)據(jù) ? ? private SharedPreferences.Editor editor;//editor將數(shù)據(jù)寫入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);//讀取上次登陸時(shí)存入"remember_password"的信息,沒(méi)有讀取到則默認(rèn)為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();//若沒(méi)有,清除SharedPreferences存儲(chǔ)的信息 ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? 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內(nèi)部添加了一個(gè)強(qiáng)制下線功能,這樣,進(jìn)入app后點(diǎn)擊下線按鈕就能直接退出到登陸界面,會(huì)使效果更加直觀。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android實(shí)現(xiàn)記住密碼功能
- Android SharedPreferences實(shí)現(xiàn)記住密碼和自動(dòng)登錄
- Android開發(fā)筆記SQLite優(yōu)化記住密碼功能
- Android實(shí)現(xiàn)用戶登錄記住密碼功能
- Android sharedPreferences實(shí)現(xiàn)記住密碼功能
- Android 使用SharedPreferrences儲(chǔ)存密碼登錄界面記住密碼功能
- Android實(shí)現(xiàn)登錄界面記住密碼的存儲(chǔ)
- Android SharedPreferences實(shí)現(xiàn)記住密碼和自動(dòng)登錄界面
- Android實(shí)現(xiàn)帶有記住密碼功能的登陸界面
- Android通過(guò)記住密碼功能學(xué)習(xí)數(shù)據(jù)存儲(chǔ)類SharedPreferences詳解及實(shí)例
相關(guān)文章
Android基于google Zxing實(shí)現(xiàn)各類二維碼掃描效果
這篇文章主要介紹了Android基于google Zxing實(shí)現(xiàn)各類二維碼掃描效果的相關(guān)資料,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-02-02Android view滑動(dòng)懸浮固定效果實(shí)現(xiàn)代碼示例
本篇文章主要介紹了Android view滑動(dòng)懸浮固定效果實(shí)現(xiàn)代碼示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-10-10Android解決getExternalStorageDirectory在29后廢棄問(wèn)題(推薦)
這篇文章主要介紹了Android解決getExternalStorageDirectory在29后廢棄問(wèn)題(推薦),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-02-02Android實(shí)現(xiàn)點(diǎn)擊切換視圖并跳轉(zhuǎn)傳值
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)點(diǎn)擊切換視圖并跳轉(zhuǎn)傳值,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-01-01android開發(fā)教程之自定義控件checkbox的樣式示例
這篇文章主要介紹了android自定義checkbox的樣式示例,需要的朋友可以參考下2014-03-03Android編程之TabWidget選項(xiàng)卡用法實(shí)例分析
這篇文章主要介紹了Android編程之TabWidget選項(xiàng)卡用法,結(jié)合實(shí)例形式較為詳細(xì)的分析了TabWidget選項(xiàng)卡的具體實(shí)現(xiàn)技巧與使用注意事項(xiàng),需要的朋友可以參考下2015-12-12