登陆页面布局设计:

 <LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="horizontal" > <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/account" /> <EditText
android:id="@+id/edtaccount"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:inputType="number"
android:singleLine="true" />
</LinearLayout> <LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="horizontal" > <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/password" /> <EditText
android:id="@+id/edtpassword"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:singleLine="true" />
</LinearLayout> <Button
android:id="@+id/btnlogin"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="@string/login" />

注销页面布局设计:

    <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="@string/注销页面"
android:textSize="15sp" /> <Button
android:id="@+id/btncancel"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:text="@string/cancel" />

LoginActivity.java:

package com.xiaoyan.autologin;

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.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast; public class LoginActivity extends Activity { // 定义组件
private EditText edtAccount;
private EditText edtPassword;
private Button btnLogin; // 用于记录帐号和密码
private String strAccount = "";
private String strPassword = ""; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login_main); // 设置标题
setTitle("Login"); // 获取sharedpreferences对象
SharedPreferences share = getSharedPreferences("Login",
Context.MODE_PRIVATE);
strAccount = share.getString("Account", "");
strPassword = share.getString("Password", ""); // 判断是否是之前有登录过
if (share == null) {
init();
} else {
// 判断是否刚注销
if (share.getBoolean("LoginBool", false)) {
// 跳转到注销页面并销毁当前activity
Intent intent = new Intent(LoginActivity.this,
CancelActivity.class);
startActivity(intent);
finish();
} else { init();
}
} } private void init() { // 初始化组件
edtAccount = (EditText) findViewById(R.id.edtaccount);
edtPassword = (EditText) findViewById(R.id.edtpassword);
btnLogin = (Button) findViewById(R.id.btnlogin); edtAccount.setText(strAccount);
edtPassword.setText(strPassword); // 监听按钮
btnLogin.setOnClickListener(new View.OnClickListener() { @Override
public void onClick(View arg0) {
// 判断帐号和密码是输入是否为空
if (edtAccount.getText().toString().equals("")
|| edtPassword.getText().toString().equals("")) {
Toast.makeText(LoginActivity.this, "帐号或密码不能为空",
Toast.LENGTH_SHORT).show();
} else {
// 创建SharedPreferences对象用于储存帐号和密码,并将其私有化
SharedPreferences share = getSharedPreferences("Login",
Context.MODE_PRIVATE);
// 获取编辑器来存储数据到sharedpreferences中
Editor editor = share.edit();
editor.putString("Account", edtAccount.getText().toString());
editor.putString("Password", edtPassword.getText()
.toString());
editor.putBoolean("LoginBool", true);
// 将数据提交到sharedpreferences中
editor.commit(); // 跳转到注销页面并销毁当前activity
Intent intent = new Intent(LoginActivity.this,
CancelActivity.class);
startActivity(intent);
finish();
} }
});
} }

CancelActivity.java:

package com.xiaoyan.autologin;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button; public class CancelActivity extends Activity { // 定义组件
private Button btnCancel; @Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.cancel_activity); // 设置标题
setTitle("Cancel");
// 初始化页面
init(); } private void init() {
// 初始化组件
btnCancel = (Button) findViewById(R.id.btncancel); // 监听注销按钮
btnCancel.setOnClickListener(new View.OnClickListener() { @Override
public void onClick(View arg0) {
// TODO Auto-generated method stub // 注销帐号并销毁当前页面
SharedPreferences share = getSharedPreferences("Login",
Context.MODE_PRIVATE);
share.edit().putBoolean("LoginBool", false).commit(); Intent intent = new Intent(CancelActivity.this,
LoginActivity.class);
startActivity(intent);
finish();
}
});
}
}

Android自动登录功能的实现的更多相关文章

  1. yii2.0自动登录功能的实现方法

    参考地址:http://www.kuitao8.com/20150518/3747.shtml 自动登录的原理很简单.主要就是利用cookie来实现的在第一次登录的时候,如果登录成功并且选中了下次自动 ...

  2. 自己Cookie写的自动登录功能 包含BASE64 和MD5的使用

    sql表 username  password字段 User类 有 id username password等字段 Service有一函数 @Override public User findUser ...

  3. spring security实现记住我下次自动登录功能

    目录 spring security实现记住我下次自动登录功能 一.原理分析 二.实现方式 2.1 简单实现方式 2.2 数据库实现方式 三.区分是密码登录还是rememberme登录 spring ...

  4. cookie理解与实践【实现简单登录以及自动登录功能】

    cookie理解 Cookie是由W3C组织提出,最早由netscape社区发展的一种机制 http是无状态协议.当某次连接中数据提交完,连接会关闭,再次访问时,浏览器与服务器需要重新建立新的连接: ...

  5. 二十 Filter&自动登录功能

    Filter过滤器 过滤器,其实就是对客户端发出来的请求进行过滤,浏览器发出,然后服务器用Servelt处理.在中间就可以过滤,起到的是拦截的作用. 不仅仅作用于客户端请求,而且过滤服务器响应 作用: ...

  6. Vue+Vuex 实现自动登录功能

    刚刚实现了Vue+Vuex的自动登录功能,在实现的时候遇到了一些问题,这里记录一下: 因为这个还不够完善,在写完下列代码后,又进行了补充,可以从https://www.cnblogs.com/xiao ...

  7. 使用token实现在有效期内APP自动登录功能

    实现此功能的场景是在当下用户对手机APP体验要求高,并且相对安全前提的推动下诞生:当你下载了一个QQ,微信第一次进行了账号和密码的登录,你从此以后打开应用免去了你每日打开应用都要输入账号跟密码的痛苦过 ...

  8. Android自动登录与记住密码

    // 获取实例对象 sp = this.getSharedPreferences("userInfo", Context.MODE_WORLD_READABLE); rem_pw ...

  9. java代码实现自动登录功能

    通常我们登录某网站,会有选择保存几天,或者是几个星期不用登录,之后输入该网站地址无需登录直接进入主页面,那么这就叫做自动登录,怎么实现呢,下面我以一个小例子来演示一下 登录页面:login.jsp & ...

随机推荐

  1. [android] 采用pull解析xml文件

    /***********2016年5月6日 更新**********************/ 知乎:Android 中有哪几种解析 xml 的类,官方推荐哪种 ? 以及它们的原理和区别? 刘吉财: ...

  2. Java高并发 -- J.U.C.组件扩展

    Java高并发 -- J.U.C.组件扩展 主要是学习慕课网实战视频<Java并发编程入门与高并发面试>的笔记 FutureTask Future模式,核心思想是异步调用.和同步调用的区别 ...

  3. 向Spring容器中注册组件的方法汇总小结

    1.通过xml定义 <bean class=""> <property name="" value=""></ ...

  4. vue单页应用添加百度统计

    前言 申请百度统计后,会得到一段JS代码,需要插入到每个网页中去,在Vue.js项目首先想到的可能就是,把统计代码插入到index.html入口文件中,这样就全局插入,每个页面就都有了;这样做就涉及到 ...

  5. jQuery效果之雪花飘落

    实现思路 1.在一定的频率下在页面中生成一定数目的雪花从上往下飘落: 2.在指定的时间内飘落后移除页面: 3.可设置雪花的大小,在一定范围内随机雪花大小: 4.什么时间后清除生成雪花,停止函数. js ...

  6. POJ1201 Intervals(差分约束)

    Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 28416   Accepted: 10966 Description You ...

  7. TS学习随笔(一)->安装和基本数据类型

    去年学过一段时间的TS,但由于在工作中不常用.就生疏了,最近项目要求用TS,那我就再回去搞搞TS,写一篇记录一下自己学习TS的进度以及TS知识点 首先,关于TS的定义我就不在这描述了,想看百度一下你就 ...

  8. Human Motion Analysis with Wearable Inertial Sensors——阅读3

    Human Motion Analysis with Wearable Inertial Sensors——阅读3 四元数方向滤波器 之前的研究开发了一种自适应增益互补滤波器,并结合高斯 - 牛顿优化 ...

  9. vue2 设置网页title的问题

    好东东,没个标题看着多难受 看到1文章  http://blog.csdn.net/qishuixian/article/details/72912368 推荐使用 vue-wechat-title插 ...

  10. View在测量时的MeasureSpec由什么决定?

    我们都知道系统要确定View的大小,首先得先获得MeasureSpec,再通过MeasureSpec来决定View的大小. MeasureSpec(32为int值)由两部分组成: SpecMode(高 ...