Android大作業(yè)功能設(shè)計(jì)之自動(dòng)登錄和記住密碼
前言
大家好,我是oy,今天介紹一下在登錄頁(yè)面中如何實(shí)現(xiàn)自動(dòng)登錄及記住密碼。
一、效果

二、設(shè)計(jì)思路
- 使用sharedPreferenced存儲(chǔ)用戶(hù)賬號(hào)和密碼,以及是否記住密碼和自動(dòng)登錄。
- 記住密碼和自動(dòng)登錄按鈕都采用checkButton,使用checkButton的OnCheckedChangeListener監(jiān)聽(tīng)。
三、知識(shí)點(diǎn)介紹
SharedPreferenced
sharedPreferenced是Android中存儲(chǔ)數(shù)據(jù)的一種方式。采用鍵值對(duì)的方式存儲(chǔ)數(shù)據(jù)。
使用過(guò)程:
- ① 獲取sharedPreferenced對(duì)象。
- ② 調(diào)用edit()獲取SharePreferenced.Editor對(duì)象。
- ③ 調(diào)用putBoolean()…等向SharePreferenced.Editor對(duì)象添加數(shù)據(jù)。
- ④ 調(diào)用apply()提交數(shù)據(jù)。
例子
// 存數(shù)據(jù)
SharedPreferences sp = getSharedPrefrences("data", MODE_PRIVATE);// 獲取sharedPreferenced對(duì)象
SharedPreferences.Editor ed = sp.edit();// 獲取SharePreferenced.Editor對(duì)象
ed.putString("name", "Sam");// 向SharePreferenced.Editor對(duì)象添加數(shù)據(jù)
ed.apply();// 調(diào)用apply()提交數(shù)據(jù),就是保存的意思
// 取數(shù)據(jù)
SharedPrefrences sp = getSharedPrefrences("data",MODE_PRIVATE);
String name = sp.getString("name","");// 取數(shù)據(jù)
checkButton就不介紹了
四、自動(dòng)登錄及記住密碼實(shí)現(xiàn)
分為兩個(gè)activity,mainActivity是登錄頁(yè)面,homeActivity是登錄成功頁(yè)面。
HomeActivity.java代碼
public class HomeActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
}
}
activity_home.xml代碼
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".HomeActivity">
<TextView
android:id="@+id/tv_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/text"
android:textSize="26sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java代碼
private AppCompatEditText edit_account, edit_password;
private CheckBox cb_remember, cb_autologin;
private SharedPreferences sharedPreferences;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bindView();
initView();
}
/**
*用于綁定控件id的方法
*/
protected void bindView() {
edit_account = findViewById(R.id.edit_account);
edit_password = findViewById(R.id.edit_password);
cb_remember = findViewById(R.id.cb_remember);
cb_remember.setOnCheckedChangeListener(this);
cb_autologin = findViewById(R.id.cb_autologin);
cb_autologin.setOnCheckedChangeListener(this);
Button btn_login = findViewById(R.id.btn_login);
btn_login.setOnClickListener(this);
// 獲取SharedPreferences的實(shí)例
sharedPreferences = this.getSharedPreferences("loginInfo", MODE_PRIVATE);
}
/**
* 用于初始化界面
*/
protected void initView() {
// 獲取sharedPreferences中remember對(duì)于的boolean值,true表示記住密碼
if (sharedPreferences.getBoolean("remember", false)) {
cb_remember.setChecked(true);
edit_account.setText(sharedPreferences.getString("account", ""));
edit_password.setText(sharedPreferences.getString("password",""));
autologin();
}
}
// 登錄按鈕的邏輯
@Override
public void onClick(View view) {
// 定義賬號(hào)和密碼的字符串
String account, password;
// 判斷賬號(hào)是否為空
if (edit_account.getText() == null) {
showToast("賬號(hào)為空,請(qǐng)重新輸入");
return;
}
// 判斷密碼是否為空
if (edit_password.getText() == null) {
showToast("密碼為空,請(qǐng)重新輸入");
return;
}
// 賬號(hào)和密碼都不為空,進(jìn)行密碼賬號(hào)校驗(yàn)
account = edit_account.getText().toString().trim();
password = edit_password.getText().toString().trim();
// 此處固定了賬號(hào)和密碼
if (account.equals("admin") && password.equals("12345")) {
if (cb_remember.isChecked()) {
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("account", account);
editor.putString("password", password);
editor.apply();
}
showToast("登錄成功");
Intent intent = new Intent(MainActivity.this, HomeActivity.class);// 跳轉(zhuǎn)到主界面
startActivity(intent);
// finish();
}
}
/**
* 自動(dòng)登錄邏輯
*/
private void autologin() {
// 獲取sharedPreferences中autologin對(duì)于的boolean值, true表示記住密碼
if (sharedPreferences.getBoolean("autologin", false)) {
// 勾選自動(dòng)登錄
cb_autologin.setChecked(true);
// 跳轉(zhuǎn)頁(yè)面
Intent intent = new Intent(MainActivity.this, HomeActivity.class);// 跳轉(zhuǎn)到主界面
startActivity(intent);
}
}
/**
* 用于顯示toast彈出消息
* @param text 需要顯示的文本
*/
private void showToast(final String text) {
Toast.makeText(MainActivity.this, text, Toast.LENGTH_SHORT).show();
}
// checkButton按鈕的選中監(jiān)聽(tīng)事件,compoundButton指的是checkButton控件, isChecked指的是是否勾選
@SuppressLint("NonConstantResourceId")
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
switch (compoundButton.getId()) {
case R.id.cb_remember:
if (isChecked) {
sharedPreferences.edit().putBoolean("remember", true).apply();
} else {
sharedPreferences.edit().putBoolean("remember", false).apply();
}
break;
case R.id.cb_autologin:
if (isChecked) {
sharedPreferences.edit().putBoolean("autologin", true).apply();
} else {
sharedPreferences.edit().putBoolean("autologin", false).apply();
}
break;
}
}
sharedPreferenced存儲(chǔ)是位于data/data/包名/shared_prefs下。是xml文件存儲(chǔ)鍵值對(duì)。
比如
<?xml version='1.0' encoding='utf-8' standalone='yes' ?> <map> <boolean name="remember" value="true" /> <boolean name="autologin" value="true" /> <string name="password">12345</string> <string name="account">admin</string> </map>
總結(jié)與補(bǔ)充
主要介紹了在登錄頁(yè)面中如何實(shí)現(xiàn)自動(dòng)登錄和記住密碼的功能,簡(jiǎn)單介紹了sharedPreferences的使用方法,這也是Android中存儲(chǔ)數(shù)據(jù)常用的方法之一。Android存儲(chǔ)還有sqlite數(shù)據(jù)庫(kù)存儲(chǔ),在另一篇文章 " Android studio登錄注冊(cè)的實(shí)現(xiàn)及介紹 " 中有講到。
到此這篇關(guān)于Android大作業(yè)功能設(shè)計(jì)之自動(dòng)登錄和記住密碼的文章就介紹到這了,更多相關(guān)Android自動(dòng)登錄和記住密碼內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
android檢測(cè)SD卡讀寫(xiě)權(quán)限方法
今天小編就為大家分享一篇android檢測(cè)SD卡讀寫(xiě)權(quán)限方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-08-08
Android自定義帶圓點(diǎn)的半圓形進(jìn)度條
這篇文章主要為大家詳細(xì)介紹了Android自定義帶圓點(diǎn)的半圓形進(jìn)度條,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-12-12
Android嵌套滾動(dòng)的傳統(tǒng)方法與思路
Android嵌套滾動(dòng)是在開(kāi)發(fā)中經(jīng)常遇到的一個(gè)需求,這篇文章主要介紹了Android嵌套滾動(dòng)的傳統(tǒng)方法與思路的相關(guān)資料,對(duì)各位Android開(kāi)發(fā)者們具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-05-05
Android ListView滾動(dòng)到底后自動(dòng)加載數(shù)據(jù)
這篇文章主要為大家詳細(xì)介紹了Android之ListView滾動(dòng)到底后自動(dòng)加載數(shù)據(jù),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-09-09
Android 完全退出當(dāng)前應(yīng)用程序的四種方法
Android程序有很多Activity,比如說(shuō)主窗口A,調(diào)用了子窗口B,如果在B中直接finish(), 接下里顯示的是A。在B中如何關(guān)閉整個(gè)Android應(yīng)用程序呢?本人總結(jié)了幾種比較簡(jiǎn)單的實(shí)現(xiàn)方法2016-02-02
實(shí)例分析Android中HandlerThread線程用法
本篇文章主要給大家介紹了Android HandlerThread使用介紹以及源碼解析,有需要的朋友參考學(xué)習(xí)下吧。2017-12-12
Android使用Circular Reveal動(dòng)畫(huà)讓頁(yè)面跳轉(zhuǎn)更炫酷
本篇文章主要介紹了Android使用Circular Reveal動(dòng)畫(huà)讓頁(yè)面跳轉(zhuǎn)更炫酷,具有一定的參考價(jià)值,有興趣的可以了解一下2017-08-08

