登陆页面布局设计:

 <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. swoole扩展实现真正的数据库连接池

    php的数据库连接池一直以来都是一个难题,很多从php语言转向java的项目,大多数原因可能都是因为java有更好的连接池实现.php的mysql扩展提供了长连接的API,但在php机器数量较多,规模 ...

  2. JVM虚拟机深入理解+GC回收+类加载

    旭日Follow_24 的CSDN 博客 ,全文地址请点击: https://blog.csdn.net/xuri24/article/details/81455449 一,前言 本文章是读了“深入理 ...

  3. PHP按符号截取字符串的指定部分

    字符串截取在php开发中是比较常用的:而且对于截取的需求也有很多种:就比如说对url链接的操作:http://baijunyao.com/article/12 有时我们想截取最后一个斜杠'/'后面的数 ...

  4. 年会抽奖 抽奖系统 抽奖软件 C# Winform

    年会抽奖软件: Q.Q 358189777 C#.  数据库Access: 1.系统启动,自动全屏展示. 2.背景随心切换. 3.快捷键方便自如: F1:弹出设置界面 F2:查询人员名单.中奖名单 F ...

  5. 洛谷P5245 【模板】多项式快速幂(多项式ln 多项式exp)

    题意 题目链接 Sol \(B(x) = \exp(K\ln(A(x)))\) 做完了... 复杂度\(O(n\log n)\) // luogu-judger-enable-o2 // luogu- ...

  6. 2018-11-16 中文代码示例之Programming in Scala笔记第四五六章

    续前文: 中文代码示例之Programming in Scala学习笔记第二三章. 同样仅节选有意思的例程部分作演示之用. 源文档仍在: program-in-chinese/Programming_ ...

  7. Html5 和 CSS的简单应用

    本文是利用几个简单的小例子,来实现html+css的简单应用. 菱形链接菜单 本例是采用html5+css3.0设置的菜单链接.其中主要用到了以下几个方面: CSS3.0中的2D变换,如:旋转tran ...

  8. 2059-authentication plugin 'caching_sha2_password"cnnot bt loaded :mysql8.0数据库链接不上:

    问题:最近数据库出了问题,就重新安装了数据库8.0,8.0建立数据库时出现问题,错误提示: 2059-authentication plugin 'caching_sha2_password" ...

  9. seajs的原理以及基本使用

    seajs模块化开发 模块化开发,把整个文件分割成一个一个小文件. 使用方法 使用方法特别简单,首先在官网中下载sea.js,然后在页面中引入. index.html // 1.路径 // 2.回调 ...

  10. .net工具基础

    MSIL Disassembler(ildasm.exe) -- 将C#程序或类库反汇编处理,显示C#编译器生成的CIL代码 C:\Program Files\Microsoft SDKs\Windo ...