android自动登录实现原理,【教程】Android 记住密码和自动登录界面的实现

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼

登录界面源码:

LoginActivity.java

public class LoginActivity extends Activity {

private EditText userName, password;

private CheckBox rem_pw, auto_login;

private Button btn_login;

private ImageButton btnQuit;

private String userNameValue,passwordValue;

private SharedPreferences sp;

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

//去除标题

this.requestWindowFeature(Window.FEATURE_NO_TITLE);

setContentView(R.layout.login);

//获得实例对象

sp = this.getSharedPreferences("userInfo", Context.MODE_WORLD_READABLE);

userName = (EditText) findViewById(R.id.et_zh);

password = (EditText) findViewById(R.id.et_mima);

rem_pw = (CheckBox) findViewById(R.id.cb_mima);

auto_login = (CheckBox) findViewById(R.id.cb_auto);

btn_login = (Button) findViewById(R.id.btn_login);

btnQuit = (ImageButton)findViewById(R.id.img_btn);

//判断记住密码多选框的状态

if(sp.getBoolean("ISCHECK", false))

{

//设置默认是记录密码状态

rem_pw.setChecked(true);

userName.setText(sp.getString("USER_NAME", ""));

password.setText(sp.getString("PASSWORD", ""));

//判断自动登陆多选框状态

if(sp.getBoolean("AUTO_ISCHECK", false))

{

//设置默认是自动登录状态

auto_login.setChecked(true);

//跳转界面

Intent intent = new Intent(LoginActivity.this,LogoActivity.class);

LoginActivity.this.startActivity(intent);

}

}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是实现 Android 记住密码自动登录的简单代码示例: 1. 记住密码: 在登录页面的登录按钮点击事件中,添加如下代码: ```java // 获取账号和密码的EditText EditText etAccount = findViewById(R.id.et_account); EditText etPassword = findViewById(R.id.et_password); // 获取记住密码的CheckBox CheckBox cbRememberPwd = findViewById(R.id.cb_remember_pwd); if (cbRememberPwd.isChecked()) { // 如果勾选了记住密码,则将账号和密码保存到SharedPreferences中 SharedPreferences sp = getSharedPreferences("login_info", MODE_PRIVATE); SharedPreferences.Editor editor = sp.edit(); editor.putString("account", etAccount.getText().toString()); editor.putString("password", etPassword.getText().toString()); editor.putBoolean("remember_pwd", true); editor.apply(); } else { // 如果没有勾选记住密码,则清空SharedPreferences中的账号和密码 SharedPreferences sp = getSharedPreferences("login_info", MODE_PRIVATE); SharedPreferences.Editor editor = sp.edit(); editor.remove("account"); editor.remove("password"); editor.putBoolean("remember_pwd", false); editor.apply(); } ``` 2. 自动登录: 在应用启动的时候,在 onCreate() 方法中添加如下代码: ```java // 获取保存的账号和密码 SharedPreferences sp = getSharedPreferences("login_info", MODE_PRIVATE); String account = sp.getString("account", ""); String password = sp.getString("password", ""); boolean rememberPwd = sp.getBoolean("remember_pwd", false); if (rememberPwd) { // 如果勾选了记住密码,则填充账号和密码,并且自动登录 EditText etAccount = findViewById(R.id.et_account); EditText etPassword = findViewById(R.id.et_password); etAccount.setText(account); etPassword.setText(password); // 在这里添加登录操作,即可实现自动登录 } ``` 注意:在自动登录的时候,需要先检查账号和密码是否为空,如果为空,则不能自动登录。同时,为了保障账号和密码的安全,需要对密码进行加密存储。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值