android实现记住用户名和密码以及自动登录
毕业刚开始上班接触的第一个项目移动护士站,接到了第一任务就是登录,要用到自动登录功能,所以在这做个记录,以后用的时候直接来粘贴复制,废话少说,直奔主题
先上一下效果图,由于只是实现功能,界面没有美化,见谅


由于xml文件内容,就不展现在这了,自己写一写就好,爸妈再也不用担心我的学习了,so easy
package com.sdufe.login;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;
/**
* @author lili.guo
*
* 2014-6-6下午3:20:17
*/
public class MainActivity extends Activity {
private EditText username_et;
private EditText password_et;
private CheckBox rem;
private CheckBox auto;
private Button login;
private String username,password;
SharedPreferences sp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sp=getSharedPreferences("userInfo",Context.MODE_WORLD_READABLE);
username_et=(EditText) findViewById(R.id.username);
password_et=(EditText) findViewById(R.id.password);
rem=(CheckBox) findViewById(R.id.remember);
auto=(CheckBox) findViewById(R.id.autologin);
login=(Button) findViewById(R.id.login);
if (rem.isChecked()) {
username_et.setText(sp.getString("username", ""));
password_et.setText(sp.getString("password", ""));
if (auto.isChecked()) {
Intent intent1=new Intent();
intent1.setClass(getApplicationContext(), Welcome.class);
startActivity(intent1);
}
}
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
username=username_et.getText().toString();
password=password_et.getText().toString();
if (username.equals("Thea")&&password.equals("123")) {
Toast.makeText(getApplicationContext(), "登录成功", Toast.LENGTH_SHORT).show();
if (rem.isChecked()) {
Editor editor=sp.edit();
editor.putString("username", username);
editor.putString("password", password);
editor.commit();
}
Intent intent2=new Intent();
intent2.setClass(getApplicationContext(), Welcome.class);
startActivity(intent2);
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
用户名和密码是写死的,为了方便有需要的人学习,稍微解释一下
if (rem.isChecked()) {
username_et.setText(sp.getString("username", ""));
password_et.setText(sp.getString("password", ""));
if (auto.isChecked()) {
Intent intent1=new Intent();
intent1.setClass(getApplicationContext(), Welcome.class);
startActivity(intent1);
}
}
以上代码意思是如果记住密码就拿到本地存储的用户名和密码,如果是自动登录则直接跳转的下一个网页
if (rem.isChecked()) {
Editor editor=sp.edit();
editor.putString("username", username);
editor.putString("password", password);
editor.commit();
}
Intent intent2=new Intent();
intent2.setClass(getApplicationContext(), Welcome.class);
startActivity(intent2);
以上代码意思是说如果是记住密码的状态,则把用户名和密码写到本地
注意一点哈,跳转到下一个activity时,要修改一下AndroidManifest.xml文件,ok,结束。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
- android10 隐藏SystemUI锁屏下的多用户图标的示例代码
- Android 如何拦截用户频繁操作(点击事件)
- Android实现用户圆形头像和模糊背景
- Android实现简单用户注册案例
- Android启动页用户相关政策弹框的实现代码
- 详解Android Studio实现用户登陆界面demo(xml实现)
- Android权限如何禁止以及友好提示用户开通必要权限详解
- Android百度地图定位、显示用户当前位置
- Android模拟用户点击的实现方法
- Android EditText 监听用户输入完成的实例
- Android 用户Session管理的设计方案
- Android基于AlarmManager实现用户在线心跳功能示例
- Android 多用户详情
相关文章
Kotlin 作用域函数apply、let、run、with、also使用指南
在 Kotlin 开发中,作用域函数(Scope Functions)是一组能让代码更简洁、更函数式的高阶函数,本文将结合核心特性、代码示例和对比表格,助你精准掌握apply、let、run、with、also的使用精髓,感兴趣的朋友一起看看吧2025-04-04
Android图像切换器imageSwitcher的实例应用
这篇文章主要为大家详细介绍了Android图像切换器imageSwitcher的实例应用,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下2020-10-10
Anroid ListView分组和悬浮Header实现方法
这篇文章主要介绍了Anroid ListView分组和悬浮Header实现方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下。2016-11-11
Kotlin中Stack与LinkedList的实现方法示例
这篇文章主要给大家介绍了关于Kotlin中Stack与LinkedList实现的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考借鉴,下面随着小编来一起学习学习吧2018-06-06


最新评论