android自动登陆,Android自动登录功能的实现

登陆页面布局设计:

0066806d964e9ccfdc72579665a32ca6.png

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_marginTop="10dp"

android:orientation="horizontal" >

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/account" />

android:id="@+id/edtaccount"

android:layout_width="150dp"

android:layout_height="wrap_content"

android:inputType="number"

android:singleLine="true" />

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_marginTop="10dp"

android:orientation="horizontal" >

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/password" />

android:id="@+id/edtpassword"

android:layout_width="150dp"

android:layout_height="wrap_content"

android:inputType="textPassword"

android:singleLine="true" />

android:id="@+id/btnlogin"

android:layout_width="200dp"

android:layout_height="wrap_content"

android:layout_marginTop="10dp"

android:text="@string/login" />

注销页面布局设计:

d9c2e2a0f49dcf34421aea072fc0e336.png

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_marginTop="10dp"

android:text="@string/注销页面"

android:textSize="15sp" />

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();

}

});

}

}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值