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