任务3——使用SharedPreferences实现登录界面记住密码功能

一、实验效果图

这里写图片描述
这里写图片描述

二 、实验目的及目标

使用SharedPreferences是 当用户选中了记住密码复选框后,并且成功登录一次后,这个时候如果再重新启动登录界面,之前输入的用户名和密码就会显示在文本框中。

三 、代码

布局文件:
activity_login.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:id="@+id/activity_login"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:background="@drawable/loginbg"
    tools:context="com.edu.bzu.caselogin.LoginActivity">

    <include layout="@layout/login_top"
        android:id="@+id/include"></include>

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:srcCompat="@drawable/deer"
        android:id="@+id/imageView"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />

    <CheckBox
        android:text="记住密码"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/checkBox"
        android:layout_below="@+id/include"
        android:layout_toLeftOf="@+id/imageView"
        android:layout_toStartOf="@+id/imageView" />

</RelativeLayout>

login_top.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="@dimen/activity_horizontal_margin"
    android:background="@drawable/logtop_roundbg"
    >


    <EditText
        android:id="@+id/etName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="textPersonName"
        android:background="@android:drawable/edit_text"
        android:ems="10"
        android:drawableLeft="@drawable/icon_user"
        android:drawablePadding="10dp"
        android:hint="@string/etName"
        >
    <requestFocus/>
     </EditText>
    <EditText
        android:id="@+id/etPassword"
        android:layout_below="@id/etName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="textPassword"
        android:background="@android:drawable/edit_text"
        android:ems="10"
        android:drawableLeft="@drawable/icon_pass"
        android:drawablePadding="10dp"
        android:hint="@string/etPassword"
        >
        <requestFocus/>
    </EditText>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/etPassword">

        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@drawable/btnselect"
            android:text="登录"
            android:onClick="login"/>
        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@drawable/btnselect"
            android:layout_marginLeft="10dp"
            android:text="注册"/>
    </LinearLayout>

</RelativeLayout>

welcome.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:text="Welcome"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="232dp"
        android:id="@+id/textView"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginLeft="149dp"
        android:layout_marginStart="149dp" />
</RelativeLayout>

java代码片
LoginActivity.java

package com.edu.bzu.caselogin;

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.EditText;
import android.widget.Toast;

public class LoginActivity extends AppCompatActivity {
    CheckBox checkBox;
    EditText edit1;
    EditText edit2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

         checkBox = (CheckBox)findViewById(R.id.checkBox);
         edit1 = (EditText)findViewById(R.id.etName);
         edit2 = (EditText)findViewById(R.id.etPassword);

        SharedPreferences sharedPreferences = getSharedPreferences("data",MODE_PRIVATE);

            edit1.setText(sharedPreferences.getString("name",""));
            edit2.setText( sharedPreferences.getString("pass",""));
            checkBox.setChecked(sharedPreferences.getBoolean("check",false));

    }
    public void login(View view){
        if(checkBox.isChecked()){
            if(edit1.getText().toString().equals("admin")&&edit2.getText().toString().equals("123456")){
                SharedPreferences sharedPreferences = getSharedPreferences("data",MODE_PRIVATE);
                //Context 类提供的方法 可直接调用
                SharedPreferences.Editor editor = sharedPreferences.edit();
                editor.putString("name","admin");
                editor.putString("pass","123456");
                editor.putBoolean("check",true);
                editor.commit();
                Intent intent = new Intent();
                intent.setClass(LoginActivity.this,welcome.class);
                startActivity(intent);
            }else
            {
                Toast.makeText(this,"用户名或密码错误!",Toast.LENGTH_LONG).show();
            }
        } else {
            if(edit1.getText().toString().equals("admin")&&edit2.getText().toString().equals("123456")){
                SharedPreferences sharedPreferences = getSharedPreferences("data",MODE_PRIVATE);
                SharedPreferences.Editor editor = sharedPreferences.edit();
                editor.clear();
                editor.commit();
                Intent intent = new Intent();
                intent.setClass(LoginActivity.this,welcome.class);
                startActivity(intent);
            }else{
                Toast.makeText(this,"用户名或密码错误!",Toast.LENGTH_LONG).show();
            }

        }
    }


}

welcome.java

package com.edu.bzu.caselogin;

import android.app.Activity;
import android.os.Bundle;

/**
 * Created by Administrator on 2017/4/7.
 */

public class welcome extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.welcome);
    }
}

程序已在模拟器中调试通过!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值