登陆页面布局设计:

 <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. NPM测试模块之rewire教程

    摘要:有了rewire模块,再也不用担心测试私有函数了. 在玩转Node.js单元测试,我介绍了3个用于编写测试代码的NPM模块:Mocha, Should以及SuperTest.为了怂恿大家写单元测 ...

  2. PHP微信H5支付

    今天项目用到了微信新出的h5支付直接去官网 https://pay.weixin.qq.com/wiki/doc/api/index.html找dome去了找了之后才发现没有一脸懵逼,一开始以为和公众 ...

  3. thinkphp5引入公共部分header、footer等

    由于用惯了tp3.2,改用tp5有些还是感觉别扭的 直接上问题:项目中需要用到引入公共导航.头部.底部.右边部分等等 首先要弄清楚thinkphp5的配置项是哪个文件,众所周知:config.php, ...

  4. react-conponent-hellocynthia

    <!DOCTYPE html> <html> <head> <script src="../../build/react.js">& ...

  5. @RequestParam加与不加的区别

    最简单的两种写法,加或不加@RequestParam注解 @RequestMapping("/list") public String test(int userId) { ret ...

  6. MongoDB 通过配置文件启动及注册服务

    1.配置mongodb环境变量,配置完成之后就可以直接执行mong.mongod等常用命令,不用每次都到mongodb安装目录bin下去执行: 2.通过命令启动mongo服务 mongod --dbp ...

  7. jQuery计算文本宽度和input标签根据输入字符动态自适应宽度的实现

    jQuery计算文本宽度的原理是利用html提供的<pre>标签,向dom中动态添加<pre>标签,标签里的内容就是要测试长度的文本,获取完长度之后再删除刚才添加的<pr ...

  8. 如何查看程序所占端口号和IP

    如何查看程序所占端口号和IP 一个软件可能占用多个端口拥有多个目标IP,下面以FQ工具Lantern为例,说明端口查看方法: 1.借助第三方软件查看 如果你电脑上安装了360等优化工具,可能会自带查看 ...

  9. oracle测试环境表空间清理

    测试场景下,使用的oralce遇到表空间的占用超大,可以采用如下的方式进行空间的清理 首先使用sqlplus连接数据库sqlplus sys/password@orcl as sysdba 之类进行数 ...

  10. 前后端分离djangorestframework——限流频率组件

    频率限制 什么是频率限制 目前我们开发的都是API接口,且是开房的API接口.传给前端来处理的,也就是说,只要有人拿到这个接口,任何人都可以通过这个API接口获取数据,那么像网络爬虫的,请求速度又快, ...