自动登陆记住密码

来源:互联网 发布:网络语233是什么意思 编辑:程序博客网 时间:2024/05/19 05:41
package com.example.day09_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.view.Window;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {

    private SharedPreferences sharedPreferences;
    private EditText username;
    private EditText pwd;
    private CheckBox cb_pw;
    private Button login;
    private CheckBox cb_auto;
    private String name;
    private String password;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 去除标题
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
        // 获取控件
        username = (EditText) findViewById(R.id.et_zh);
        pwd = (EditText) findViewById(R.id.et_mima);
        cb_pw = (CheckBox) findViewById(R.id.cb_mima);
        cb_auto = (CheckBox) findViewById(R.id.cb_auto);
        login = (Button) findViewById(R.id.btn_login);
        //
        sharedPreferences = getSharedPreferences("userInfo", MODE_PRIVATE);

        // 判断记住密码多选框的状态
        if (sharedPreferences.getBoolean("ISCHECK", false)) {
            // 设置默认是记录密码的状态
            cb_pw.setChecked(true);
            // 设置框里的数据
            username.setText(sharedPreferences.getString("username", ""));
            pwd.setText(sharedPreferences.getString("pwd", ""));
            // 判断自动登陆的多选框的状态
            if (sharedPreferences.getBoolean("AUTO_ISCHECK", false)) {
                // 设置默认是自动登陆的状态
                cb_auto.setChecked(true);
                // 跳转页面
                Intent intent = new Intent(MainActivity.this,
                        Main2Activity.class);
                startActivity(intent);

            }

        }

        // 登陆的监听事件
        login.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                name = username.getText().toString();
                password = pwd.getText().toString();
                if (name.equals("sss") && password.equals("sss")) {
                    //
                    Toast.makeText(MainActivity.this, "登陆成功" + name + password,
                            0).show();
                    // 登陆成功和记录密码为选中状态才保存用户信息
                    if (cb_pw.isChecked()) {
                        // 记住用户名和密码
                        Editor edit = sharedPreferences.edit();
                        edit.putString("username", name);
                        edit.putString("pwd", password);
                        edit.commit();

                    }
                    // 跳转页面
                    Intent intent = new Intent(MainActivity.this,
                            Main2Activity.class);
                    startActivity(intent);

                } else {
                    Toast.makeText(MainActivity.this,
                            "用户名或密码错误,请重新登录" + name + "-----" + password, 0)
                            .show();

                }

            }
        });

        cb_pw.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView,
                    boolean isChecked) {

                if (cb_pw.isChecked()) {
                    sharedPreferences.edit().putBoolean("ISCHECK", true)
                            .commit();

                } else {
                    sharedPreferences.edit().putBoolean("ISCHEK", false)
                            .commit();

                }

            }
        });

        // 监听自动登陆多选框事件
        cb_auto.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView,
                    boolean isChecked) {
                if (cb_auto.isChecked()) {
                    sharedPreferences.edit().putBoolean("AUTO_ISCHECK", true)
                            .commit();

                } else {
                    sharedPreferences.edit().putBoolean("AUTO_ISCHECK", false)
                            .commit();

                }

            }
        });

    }
}