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平台上一个轻量级的存储类,主要是保存 ...
随机推荐
- 使用GDataXML解析XML文档
转载自:http://blog.csdn.net/tangren03/article/details/7868246 在IOS平台上进行XML文档的解析有很多种方法,在SDK里面有自带的解析方法,但是 ...
- select标签用法
<select name="type" class="textarea" onchange='bbbb(this.value)' > <opt ...
- vs2012用wpf制作透明窗口中报错的解决方案
在开发wpf项目时,需要调用外部com组件,同时需要制作透明窗口,于是问题出现了,当我们在设置 AllowsTransparency="True"后,com组件显示不出来了,只有透 ...
- 1096. Consecutive Factors (20)
Among all the factors of a positive integer N, there may exist several consecutive numbers. For exam ...
- 栈帧%ebp,%esp详解
首先应该明白,栈是从高地址向低地址延伸的.每个函数的每次调用,都有它自己独立的一个栈帧,这个栈帧中维持着所需要的各种信息.寄存器ebp指向当前的栈帧的底部(高地址),寄存器esp指向当前的栈帧的顶部( ...
- 在Linux下写一个简单的驱动程序
本文首先描述了一个可以实际测试运行的驱动实例,然后由此去讨论Linux下驱动模板的要素,以及Linux上应用程序到驱动的执行过程.相信这样由浅入深.由具体实例到抽象理论的描述更容易初学者入手Linux ...
- 【nodejs】 npm 注意事项
官网:https://www.npmjs.com/ 1.安装时要切换到nodejs根目录, 否则就会安装到安装时所在的目录 2.要有管理员权限(win),如需指定版本,如npm install ex ...
- 【nodejs】关于 alert 和 document
Microsoft Windows [版本 6.1.7601] 版权所有 (c) 2009 Microsoft Corporation.保留所有权利. C:\Windows\system32>n ...
- 只是一个用EF写的一个简单的分页方法而已
只是一个用EF写的一个简单的分页方法而已 慢慢的写吧.比如,第一步,先把所有数据查询出来吧. //第一步. public IQueryable<UserInfo> LoadPagesFor ...
- java集合类(六)About Queue
接上篇“java集合类(五)About Map” 终于来到了java集合类的尾声,太兴奋了,不是因为可以休息一阵了,而是因为又到了开启新知识的时刻,大家一起加油打气!!Come on...Fighti ...