记住密码+自动登录

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.gaoyue20180521.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/et_username"
        android:layout_alignBottom="@+id/et_username"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginLeft="21dp"
        android:layout_marginStart="21dp"
        android:text="手机号"
        android:id="@+id/textView2" />

    <EditText
        android:id="@+id/et_username"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="40dp"
        android:ems="10"
        android:hint="请输入手机号"
        />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/et_password"
        android:layout_alignBottom="@+id/et_password"
        android:layout_alignLeft="@+id/textView2"
        android:layout_alignStart="@+id/textView2"
        android:text="密码" />

    <EditText
        android:id="@+id/et_password"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/et_username"
        android:layout_alignStart="@+id/et_username"
        android:layout_below="@+id/et_username"
        android:layout_marginTop="47dp"
        android:ems="10"
        android:hint="请输入密码"
        android:password="true"
        />

    <CheckBox
        android:id="@+id/ck_pwd"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView"
        android:layout_alignStart="@+id/textView"
        android:layout_below="@+id/et_password"
        android:layout_marginTop="44dp"
        android:text="记住密码" />

    <CheckBox
        android:id="@+id/ck_login"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/ck_pwd"
        android:layout_alignBottom="@+id/ck_pwd"
        android:layout_alignEnd="@+id/et_password"
        android:layout_alignRight="@+id/et_password"
        android:text="自动登录" />

    <Button
        android:id="@+id/btn_login"
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:layout_below="@+id/ck_pwd"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="71dp"
        android:text="提交" />
</RelativeLayout>

MainActivity:

package com.example.gaoyue20180521;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    private EditText et_username;
    private EditText et_password;
    private CheckBox ck_pwd;
    private CheckBox ck_login;
    private Button btn_login;
    private SharedPreferences sp;
    private String userNameValue;
    private String passwordValue;

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

        sp = this.getSharedPreferences("userInfo", Context.MODE_WORLD_READABLE);
        et_username = (EditText) findViewById(R.id.et_username);
        et_password = (EditText) findViewById(R.id.et_password);
        ck_pwd = (CheckBox) findViewById(R.id.ck_pwd);
        ck_login = (CheckBox) findViewById(R.id.ck_login);
        btn_login = (Button) findViewById(R.id.btn_login);

        //判断记住密码多选框的状态
        if (sp.getBoolean("ISCHECK", false)) {
            //设置默认是记录密码状态
            ck_pwd.setChecked(true);
            et_username.setText(sp.getString("USER_NAME", ""));
            et_password.setText(sp.getString("PASSWORD", ""));
            //判断自动登陆多选框状态
            if (sp.getBoolean("AUTO_ISCHECK", false)) {
                //设置默认是自动登录状态
                ck_login.setChecked(true);
                //跳转界面
                Intent intent = new Intent(MainActivity.this, Main2Activity.class);
                MainActivity.this.startActivity(intent);
                finish();
            }
        }

        // 登录监听事件  现在默认为用户名为:liu 密码:123
        btn_login.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                userNameValue = et_username.getText().toString();
                passwordValue = et_password.getText().toString();

                if (userNameValue.equals("13800138000") && passwordValue.equals("123456")) {
                    Toast.makeText(MainActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
                    //登录成功和记住密码框为选中状态才保存用户信息
                    if (ck_pwd.isChecked()) {
                        //记住用户名、密码、
                        SharedPreferences.Editor editor = sp.edit();
                        editor.putString("USER_NAME", userNameValue);
                        editor.putString("PASSWORD", passwordValue);
                        editor.commit();
                    }
                    //跳转界面
                    Intent intent = new Intent(MainActivity.this, Main2Activity.class);
                    MainActivity.this.startActivity(intent);
                    finish();

                } else {

                    Toast.makeText(MainActivity.this, "用户名或密码错误,请重新登录", Toast.LENGTH_LONG).show();
                }

            }
        });

        //监听记住密码多选框按钮事件
        ck_pwd.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (ck_pwd.isChecked()) {

                    System.out.println("记住密码已选中");
                    sp.edit().putBoolean("ISCHECK", true).commit();

                } else {

                    System.out.println("记住密码没有选中");
                    sp.edit().putBoolean("ISCHECK", false).commit();

                }

            }
        });

        //监听自动登录多选框事件
        ck_login.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (ck_login.isChecked()) {
                    System.out.println("自动登录已选中");
                    sp.edit().putBoolean("AUTO_ISCHECK", true).commit();

                } else {
                    System.out.println("自动登录没有选中");
                    sp.edit().putBoolean("AUTO_ISCHECK", false).commit();
                }
            }
        });
    }
}

阅读更多
想对作者说点什么? 我来说一句

asp.net 记住密码 自动登录

2015年05月26日 2.06MB 下载

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭