目录前言一、效果二、设计思路三、知识点介绍SharedPreferenced四、自动登录及记住密码实现总结与补充前言 大家好,我是oy,今天介绍一下在登录页面中如何实现自动登录及记住
大家好,我是oy,今天介绍一下在登录页面中如何实现自动登录及记住密码。
sharedPreferenced是Android中存储数据的一种方式。采用键值对的方式存储数据。
使用过程:
例子
// 存数据
SharedPreferences sp = getSharedPrefrences("data", MODE_PRIVATE);// 获取sharedPreferenced对象
SharedPreferences.Editor ed = sp.edit();// 获取SharePreferenced.Editor对象
ed.putString("name", "Sam");// 向SharePreferenced.Editor对象添加数据
ed.apply();// 调用apply()提交数据,就是保存的意思
// 取数据
SharedPrefrences sp = getSharedPrefrences("data",MODE_PRIVATE);
String name = sp.getString("name","");// 取数据
checkButton就不介绍了
分为两个activity,mainActivity是登录页面,homeActivity是登录成功页面。
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();
}
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的实例
sharedPreferences = this.getSharedPreferences("loginInfo", MODE_PRIVATE);
}
protected void initView() {
// 获取sharedPreferences中remember对于的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) {
// 定义账号和密码的字符串
String account, password;
// 判断账号是否为空
if (edit_account.getText() == null) {
showToast("账号为空,请重新输入");
return;
}
// 判断密码是否为空
if (edit_password.getText() == null) {
showToast("密码为空,请重新输入");
return;
}
// 账号和密码都不为空,进行密码账号校验
account = edit_account.getText().toString().trim();
password = edit_password.getText().toString().trim();
// 此处固定了账号和密码
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);// 跳转到主界面
startActivity(intent);
// finish();
}
}
private void autologin() {
// 获取sharedPreferences中autologin对于的boolean值, true表示记住密码
if (sharedPreferences.getBoolean("autologin", false)) {
// 勾选自动登录
cb_autologin.setChecked(true);
// 跳转页面
Intent intent = new Intent(MainActivity.this, HomeActivity.class);// 跳转到主界面
startActivity(intent);
}
}
private void showToast(final String text) {
Toast.makeText(MainActivity.this, text, Toast.LENGTH_SHORT).show();
}
// checkButton按钮的选中监听事件,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存储是位于data/data/包名/shared_prefs下。是xml文件存储键值对。
比如
<?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>
主要介绍了在登录页面中如何实现自动登录和记住密码的功能,简单介绍了sharedPreferences的使用方法,这也是Android中存储数据常用的方法之一。Android存储还有sqlite数据库存储,在另一篇文章 " Android studio登录注册的实现及介绍 " 中有讲到。
到此这篇关于Android大作业功能设计之自动登录和记住密码的文章就介绍到这了,更多相关Android自动登录和记住密码内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!
--结束END--
本文标题: Android大作业功能设计之自动登录和记住密码
本文链接: https://www.lsjlt.com/news/178257.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-01-21
2023-10-28
2023-10-28
2023-10-27
2023-10-27
2023-10-27
2023-10-27
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0