欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android實現(xiàn)登陸界面的記住密碼功能

 更新時間:2022年04月23日 14:22:06   作者:沙小光  
這篇文章主要為大家詳細介紹了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í)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論