Android:利用SharedPreferences实现自动登录

主要代码:
public class LoginActivity extends Activity {
private EditText username;
private EditText userpassword;
private CheckBox remember;
private CheckBox autologin;
private Button login;
private SharedPreferences sp;
private String userNameValue,passwordValue;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
// 初始化用户名、密码、记住密码、自动登录、登录按钮
username = (EditText) findViewById(R.id.username);
userpassword = (EditText) findViewById(R.id.userpassword);
remember = (CheckBox) findViewById(R.id.remember);
autologin = (CheckBox) findViewById(R.id.autologin);
login = (Button) findViewById(R.id.login);
sp = getSharedPreferences("userInfo", );
String name=sp.getString("USER_NAME", "");
String pass =sp.getString("PASSWORD", "");
boolean choseRemember =sp.getBoolean("remember", false);
boolean choseAutoLogin =sp.getBoolean("autologin", false);
// Toast.makeText(this, name, Toast.LENGTH_SHORT).show();
//如果上次选了记住密码,那进入登录页面也自动勾选记住密码,并填上用户名和密码
if(choseRemember){
username.setText(name);
userpassword.setText(pass);
remember.setChecked(true);
}
//如果上次登录选了自动登录,那进入登录页面也自动勾选自动登录
if(choseAutoLogin){
autologin.setChecked(true);
}
login.setOnClickListener(new OnClickListener() {
// 默认可登录帐号tinyphp,密码123
@Override
public void onClick(View arg0) {
userNameValue = username.getText().toString();
passwordValue = userpassword.getText().toString();
SharedPreferences.Editor editor =sp.edit();
// TODO Auto-generated method stub
if (userNameValue.equals("tinyphp")
&& passwordValue.equals("")) {
Toast.makeText(LoginActivity.this, "登录成功",
Toast.LENGTH_SHORT).show();
//保存用户名和密码
editor.putString("USER_NAME", userNameValue);
editor.putString("PASSWORD", passwordValue);
//是否记住密码
if(remember.isChecked()){
editor.putBoolean("remember", true);
}else{
editor.putBoolean("remember", false);
}
//是否自动登录
if(autologin.isChecked()){
editor.putBoolean("autologin", true);
}else{
editor.putBoolean("autologin", false);
}
editor.commit();
//跳转
Intent intent =new Intent(LoginActivity.this,SuccessActivity.class);
startActivity(intent);
} else {
Toast.makeText(LoginActivity.this, "用户名或密码错误,请重新登录!",
Toast.LENGTH_SHORT).show();
}
}
});
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp" > <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="用户名:" /> <EditText
android:id="@+id/username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName" >
</EditText> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="密码:" /> <EditText
android:id="@+id/userpassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPassword" >
</EditText> <CheckBox
android:id="@+id/remember"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="记住密码" /> <CheckBox
android:id="@+id/autologin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="自动登录" /> <Button
android:id="@+id/login"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="登录" /> </LinearLayout>
Android:利用SharedPreferences实现自动登录的更多相关文章
- SharedPreferences实现自动登录记住用户名密码
最近Android项目需要一个自动登录功能,完成之后,特总结一下,此功能依靠SharedPreferences进行实现. SharedPreferences简介 SharedPreferences ...
- [技术博客] 利用SharedPreferences来实现登录状态的记忆功能
[技术博客] 利用SharedPreferences来实现登录状态的记忆功能 一.SharedPreferences简介 SharedPreferences是Android平台上一个轻量级的存储辅助类 ...
- Android 记住密码和自动登录界面的实现(SharedPreferences 的用法)
原文:http://blog.csdn.net/liuyiming_/article/details/7704923 SharedPreferences介绍: SharedPreferences是An ...
- Android 利用SharedPreferences保存与删除 用户登录数据
创建SharedPreferences对象: SharedPreferences sharedPreferences = context.getSharedPreferences("user ...
- Android利用HttpURLConnection实现模拟登录
最近在做一个APP,需要模拟登录教务处,之前曾经用HttpClient做过,点这里,但是发现最新的Android SDK已经不支持Httpclient了,所以只好在琢磨一下HttpURLConnect ...
- 利用jquery实现自动登录
前提是需要引入jquery和jquery.cookie html 用户名:<input type="text" id="name"/><br ...
- Android --------- 利用SharedPreferences存取数据
//向SharedPreferences中存放数据 //1.定义SharedPreferences对象,通过getSharedPreferences方法得到 SharedPreferences sp ...
- shell中嵌套执行expect命令实例(利用expect实现自动登录)
expect是 #!/bin/bashpasswd='123456'/usr/bin/expect <<EOFset time 30spawn ssh root@192.168.76.10 ...
- Android记住密码自动登录的实现
我采用的是SharedPreferences来存取数据的,所以先简单的介绍一下SharedPreferences SharedPreferences是Android平台上一个轻量级的存储类,主要是保存 ...
随机推荐
- mslookup
Microsoft Windows [版本 6.1.7601]版权所有 (c) 2009 Microsoft Corporation.保留所有权利. C:\Users\Administrator> ...
- Python脚本控制的WebDriver 常用操作 <十七> 获取测试对象的属性及内容
测试用例场景 获取测试对象的内容是前端自动化测试里一定会使用到的技术.比如我们要判断页面上是否显示了一个提示,那么我们就需要找到这个提示对象,然后获取其中的文字,再跟我们的预期进行比较.在webdri ...
- Hiberante+jpa,注解生成32字符的Id
@Id @GenericGenerator(name = "id-generator", strategy = "uuid") @GeneratedValue( ...
- layout_weight体验(实现按比例显示)
在android开发中LinearLayout很常用,LinearLayout的内控件的android:layout_weight在某些场景显得非常重要,比如我们需要按比例显示.android并没用提 ...
- Linux环境下的Nodejs
最近在学习Node.js,在window下总是觉得不那么爽快.最简单而且环保的方法是在虚拟机中安装一个Linux. { 1.Linux:家中的Linux为Centos. 2.VirtuallyBox: ...
- vim插件介绍
代码补全 http://blog.sina.com.cn/s/blog_a6559d920101acv3.html这个牛逼.************************************** ...
- ios Camera学习笔记
检测设备的摄像头是否可用: - (BOOL) isCameraAvailable{ return [UIImagePickerController isSourceTypeAvailable: UII ...
- spoj 375 Query on a tree(树链剖分,线段树)
Query on a tree Time Limit: 851MS Memory Limit: 1572864KB 64bit IO Format: %lld & %llu Sub ...
- firefox同步ajax请求报错的问题 A parameter or an operation is not supported by the underlying object
今天在测试系统时,一个很正常的功能在firefox下报错,经过验证在ie和chrome浏览器中功能这个正常. 调试后发现: 请求比其他请求的特殊点在于同步请求. 经过firefox的控制台上测 ...
- [转载]实战Linux下VMware虚拟机根目录空间扩充
[转载]实战Linux下VMware虚拟机根目录空间扩充 (2011-07-31 21:34:34) 转载▼ 标签: 转载 原文地址:实战Linux下VMware虚拟机根目录空间扩充作者:shar ...