Android SP实现记住密码和自动登录案例
使用SP制作一个记住密码 自动登录的案例
初始化页面时,判断 记住密码 和 自动登录 打钩没,存储相应数据到SP 第2次第n次打开的时候,从SP取数据,看上次是否 勾选了记住密码和自动登录,如果勾选了,就回显数据
MainActivity
package com.example.sp;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity2 extends AppCompatActivity {
SharedPreferences sp;
private EditText et_name;
private EditText et_pwd;
private CheckBox cb_remeberpwd;
private CheckBox cb_autologin;
private Button bt_login;
private Button bt_register;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
// 获取首选项 SP
sp = getSharedPreferences("config", Context.MODE_PRIVATE);
initView();
// 第二次打开的时候从SP获取数据,从sp获取数据,进行画面同步
boolean remeberpwd = sp.getBoolean("remeberpwd",false);
boolean autologin = sp.getBoolean("autologin",false);
//记住密码
if(remeberpwd){
// 获取sp中的name 和 pwd 并保存到EditText
String name = sp.getString("name",null);
String pwd = sp.getString("pwd", null);
et_name.setText(name);
et_pwd.setText(pwd);
cb_remeberpwd.setChecked(true);
}
//自动登录
if (autologin){
cb_autologin.setChecked(true);
// 模拟 自动登录
Toast.makeText(this,"自动登录了",Toast.LENGTH_SHORT).show();
}
}
private void initView() {
// 找到控件
et_name = findViewById(R.id.et_name);
et_pwd = findViewById(R.id.et_pwd);
cb_remeberpwd = findViewById(R.id.cb_remeberpwd);
cb_autologin = findViewById(R.id.cb_autologin);
bt_login = findViewById(R.id.bt_login);
bt_register = findViewById(R.id.bt_register);
// 设置监听
MyOnClickListener l =new MyOnClickListener();
bt_login.setOnClickListener(l);
bt_register.setOnClickListener(l);
}
private class MyOnClickListener implements View.OnClickListener{
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.bt_register:
break;
case R.id.bt_login:
// 登录操作
String name = et_name.getText().toString().trim();
String pwd = et_pwd.getText().toString().trim();
if(TextUtils.isEmpty(name) || TextUtils.isEmpty(pwd)){
Toast.makeText(MainActivity2.this,"用户名或密码不能为空",Toast.LENGTH_SHORT).show();;
}else{
// 记住密码 打勾没有
if(cb_remeberpwd.isChecked()){
// 用户名和密码要保存 同时记住密码的状态要保存
SharedPreferences.Editor editor = sp.edit();
editor.putString("name",name);
editor.putString("pwd",pwd);
editor.putBoolean("remeberpwd",true);
editor.apply();
}
// 自动登录 打勾没有
if(cb_autologin.isChecked()){
SharedPreferences.Editor editor =sp.edit();
editor.putBoolean("autologin",true);
editor.apply();
}
}
break;
}
}
}
}
布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity2">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="用户名" />
<EditText
android:id="@ id/et_name"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="密码" />
<EditText
android:id="@ id/et_pwd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:password="true" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<CheckBox
android:id="@ id/cb_remeberpwd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="记住密码" />
<CheckBox
android:id="@ id/cb_autologin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="自动登录" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@ id/bt_register"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="注册" />
<Button
android:id="@ id/bt_login"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="登录" />
</LinearLayout>
</LinearLayout>
实现效果如下
勾选了记住密码和自动登录后关闭应用再次打开 自动登录了文字提示出现 说明数据回显成功
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /boutique/detail/tanhhjbeib
系列文章
更多
同类精品
更多
-
photoshop保存的图片太大微信发不了怎么办
PHP中文网 06-15 -
word里面弄一个表格后上面的标题会跑到下面怎么办
PHP中文网 06-20 -
photoshop扩展功能面板显示灰色怎么办
PHP中文网 06-14 -
《学习通》视频自动暂停处理方法
HelloWorld317 07-05 -
Android 11 保存文件到外部存储,并分享文件
Luke 10-12 -
TikTok加速器哪个好免费的TK加速器推荐
TK小达人 10-01 -
微信公众号没有声音提示怎么办
PHP中文网 03-31 -
excel下划线不显示怎么办
PHP中文网 06-23 -
excel打印预览压线压字怎么办
PHP中文网 06-22 -
微信运动停用后别人还能看到步数吗
PHP中文网 07-22