SharedPreferences自动登录判断

登录页面

package com.bw.sharedpreferences;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;

public class LoginActivity extends Activity {
private EditText name_ed, pass_ed;
private CheckBox jz_check, zd_check;
private Button login;
private SharedPreferences preferences;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);

    // 1.获取资源ID
    name_ed = (EditText) findViewById(R.id.name_ed);
    pass_ed = (EditText) findViewById(R.id.pass_ed);
    jz_check = (CheckBox) findViewById(R.id.jz_check);
    zd_check = (CheckBox) findViewById(R.id.zd_check);
    login = (Button) findViewById(R.id.login);

    preferences = getSharedPreferences("User", MODE_PRIVATE);
    // 自动登录
    boolean isChecked = preferences.getBoolean("isChecked", false);
    if (isChecked) {
        String name = preferences.getString("name", null);
        String pass = preferences.getString("pass", null);
        name_ed.setText(name);
        pass_ed.setText(pass);
        jz_check.setChecked(true);
    }
    // 记住密码
    boolean zd_isChecked = preferences.getBoolean("zd_isChecked", false);
    if (zd_isChecked) {
        Intent intent = new Intent(LoginActivity.this, MainActivity.class);
        startActivity(intent);
        finish();
    }
    // 两者同时勾选
    zd_check.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView,
                boolean isChecked) {
            if (isChecked) {
                jz_check.setChecked(true);
            }
        }
    });

    // 记住密码取消勾选 自动登录也取消
    jz_check.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView,
                boolean isChecked) {
            if (isChecked == false) {
                zd_check.setChecked(false);
            }
        }
    });

    // 登录判断
    login.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            if (jz_check.isChecked()) {
                String name = name_ed.getText().toString();// 用户名
                String pass = pass_ed.getText().toString();// 密码
                Editor edit = preferences.edit();
                edit.putString("name", name);
                edit.putString("pass", pass);
                edit.putBoolean("isChecked", true);
                edit.commit();
            }
            if (zd_check.isChecked()) {
                Editor edit = preferences.edit();
                edit.putBoolean("zd_isChecked", true);
                edit.commit();
            }

            Intent intent = new Intent(LoginActivity.this, MainActivity.class);
            startActivity(intent);
            finish();
            }
        });
    }
}

布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<EditText
    android:id="@+id/name_ed"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="42dp"
    android:ems="10"
    android:hint="请输入账号" >
</EditText>

<EditText
    android:id="@+id/pass_ed"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/name_ed"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="40dp"
    android:ems="10"
    android:hint="请输入密码" >
</EditText>

<CheckBox
    android:id="@+id/jz_check"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/pass_ed"
    android:layout_centerVertical="true"
    android:text="记住密码" />

<CheckBox
    android:id="@+id/zd_check"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/jz_check"
    android:layout_alignRight="@+id/pass_ed"
    android:text="自动登录" />

<Button
    android:id="@+id/login"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="82dp"
    android:text="登录" />

</RelativeLayout>

主界面

package com.bw.sharedpreferences;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {

private Button button;
private SharedPreferences preferences;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    button = (Button) findViewById(R.id.button1);
    // 点击清空
    button.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            preferences = getSharedPreferences("User", MODE_PRIVATE);
            Editor edit = preferences.edit();
            edit.clear();
            edit.commit();

            Intent intent = new Intent(MainActivity.this,LoginActivity.class);
            startActivity(intent);
            finish();
            }
        });
    }
}

布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:text="登录成功"
    android:textSize="20sp" />

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textView1"
    android:layout_alignParentBottom="true"
    android:layout_marginBottom="57dp"
    android:text="注销" />

</RelativeLayout>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值