日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無(wú)先后,達(dá)者為師

網(wǎng)站首頁(yè) 編程語(yǔ)言 正文

Android大作業(yè)功能設(shè)計(jì)之自動(dòng)登錄和記住密碼_Android

作者:勤奮的oyoung ? 更新時(shí)間: 2023-03-19 編程語(yǔ)言

前言

大家好,我是oy,今天介紹一下在登錄頁(yè)面中如何實(shí)現(xiàn)自動(dòng)登錄及記住密碼。

一、效果

二、設(shè)計(jì)思路

  • 使用sharedPreferenced存儲(chǔ)用戶賬號(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)及介紹 " 中有講到。

原文鏈接:https://blog.csdn.net/m0_49534667/article/details/128607535

欄目分類
最近更新