这篇文章主要介绍“Android如何实现登陆界面的记住密码功能”,在日常操作中,相信很多人在Android如何实现登陆界面的记住密码功能问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”Android如何实现登陆界面的记住密码功能”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!
一、复选框控件:CheckBox,
二、SharedPreferences用于存储数据,该工具的读取和写入较为简单,放在代码里的注释中解释
如果没弄懂逻辑,代码看起来还是有点小难度的
一、判断SharedPreferences中已存入的CheckBox的Boolean信息(没有读取到则默认条件为“否”),如果条件为“是”(同时满足能读取到和读取的信息为“是”两个条件),通过SharedPreferences将存储的数据(account和password)读取出来并写入对应的文本框。
二、点击登录按键时,判断CheckBox是否勾选,如果条件为“是”,则将accout和password框里的数据(String)以及CheckBox的数据(Boolean)写入SharedPreferences,若没有勾选,则清除SharedPreferences中的数据。
一、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>
二、实现功能部分
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的数据
private SharedPreferences.Editor editor;//editor将数据写入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();
}
});
}
}
到此,关于“Android如何实现登陆界面的记住密码功能”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。