您好,登錄后才能下訂單哦!
這篇文章主要介紹“Android如何實(shí)現(xiàn)登陸界面的記住密碼功能”,在日常操作中,相信很多人在Android如何實(shí)現(xiàn)登陸界面的記住密碼功能問(wèn)題上存在疑惑,小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對(duì)大家解答”Android如何實(shí)現(xiàn)登陸界面的記住密碼功能”的疑惑有所幫助!接下來(lái),請(qǐng)跟著小編一起來(lái)學(xué)習(xí)吧!
一、復(fù)選框控件:CheckBox,
二、SharedPreferences用于存儲(chǔ)數(shù)據(jù),該工具的讀取和寫入較為簡(jiǎ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ù)。
一、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(); } }); } }
到此,關(guān)于“Android如何實(shí)現(xiàn)登陸界面的記住密碼功能”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注億速云網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)?lái)更多實(shí)用的文章!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。