android 使用SharedPreferences 实现登录时记住密码功能

android 使用SharedPreferences 实现登录时记住密码功能

由于最近一段时间项目比较赶,好久没有更新博客了,还望粉丝们多多谅解。今天给大家实现下使用SharedPreferences 实现登录时记住密码功能。话不多说,来上代码。

这是需要chexbox的布局文件。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:orientation="horizontal">
        <TextView
            android:layout_width="90dp"
            android:layout_height="wrap_content"
            android:text="User Name"
            android:textSize="18sp"
            android:layout_gravity="center_vertical"/>
        <EditText
            android:id="@+id/account"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:layout_gravity="center_vertical"
            />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:orientation="horizontal">
        <TextView
            android:layout_width="90dp"
            android:layout_height="wrap_content"
            android:text="Password"
            android:textSize="18sp"
            android:layout_gravity="center_vertical"/>
        <EditText
            android:id="@+id/password"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:layout_gravity="center_vertical"
            android:inputType="textPassword"/>
    </LinearLayout>
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <CheckBox
            android:id="@+id/remember_pass"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="18sp"
            android:text="Remember Password"/>
    </LinearLayout>

    <Button
        android:id="@+id/login"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:text="Login"/>

</LinearLayout>

接下来看代码:

package com.example.zsh.rememberpsw_test;

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

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private EditText mAccount;
    private EditText mPassword;
    private CheckBox mCheckbox;
    private Button mLogin;
    private SharedPreferences pref;
    private boolean isFirst = true;//用于判断是否是第一次运行,运行后变为false
    private SharedPreferences.Editor editor;

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


    private void initView() {
        mAccount = (EditText) findViewById(R.id.account);
        mPassword = (EditText) findViewById(R.id.password);
        mCheckbox = (CheckBox) findViewById(R.id.remember_pass);
        mLogin = (Button) findViewById(R.id.login);

    }


    private void getData() {

        pref = getSharedPreferences("savaLogin", MODE_PRIVATE);//创建SharedPreferences对象
        editor = pref.edit();//创建SharedPreferences的编辑器
        String userName = pref.getString("userName", "");//获取用户名,没有则用空代替
        String userPass = pref.getString("userPass", "");
        if (userName.equals("")) {
            //如果为空,代表前一次为选择记住密码,则这次显示记住密码多选框不打勾
            mCheckbox.setChecked(false);
        } else {
            mCheckbox.setChecked(true);
            mAccount.setText(userName);
            //将获取到的值设置为text
            mPassword.setText(userPass);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            default:
                break;
            case R.id.login:

                String userName = mAccount.getText().toString().trim();
                //获取EditText中输入的值,并去掉空格			
                String userPass = mPassword.getText().toString().trim();
                if ("admin".equals(userName) && "123456".equals(userPass)) {
                    if (mCheckbox.isChecked()) {
                        editor.putString("userName", userName);
                        editor.putString("userPass", userPass);
                        editor.commit();//提交数据			
                    } else {
                        //若没有选择记住密码	
                        editor.remove("userPass");//删除密码				
                        editor.commit();
                    }
                    Toast.makeText(MainActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(MainActivity.this, "用户名或密码错误", Toast.LENGTH_SHORT).show();
                }

        }

    }

    private void initListener() {

        mLogin.setOnClickListener(this);
    }
}
    

猜你喜欢

转载自blog.csdn.net/MrZhao_PerfectCode/article/details/82969564