用户登录记住密码

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

这里写代码片
Login Activity:
package cn.edu.bzu.case_login;


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


public class LoginActivity extends AppCompatActivity {
    private CheckBox CBPass;
    private EditText Name;
    private EditText Password;


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

    }


    public void save(View view) {

        Name = (EditText) findViewById(R.id.etName);
        Password = (EditText) findViewById(R.id.etPassword);
        CBPass = (CheckBox) findViewById(R.id.cbPass);

        String name = Name.getText().toString();
        String password = Password.getText().toString();
        Boolean cbpass = CBPass.isChecked();

        if (name.equals("admin") && password.equals("123456")) {

            SharedPreferences sp = getSharedPreferences("data", MODE_PRIVATE);
            SharedPreferences.Editor editor = sp.edit();
            editor.putString("etName", name);
            editor.putString("etPassword", password);
            editor.putBoolean("Click", cbpass);
            editor.commit();
            Intent intent = new Intent(LoginActivity.this, WelActivity.class);
            startActivity(intent);
        } else {
            Toast.makeText(LoginActivity.this, "用户名或密码错误", Toast.LENGTH_SHORT).show();
        }
    }

    public void read() {
        Name = (EditText) findViewById(R.id.etName);
        Password = (EditText) findViewById(R.id.etPassword);
        CBPass = (CheckBox) findViewById(R.id.cbPass);
        SharedPreferences sp = getSharedPreferences("data", MODE_PRIVATE);
        String name = sp.getString("etName", "");
        String password = sp.getString("etPassword", "");
        boolean cbpass = sp.getBoolean("Click", false);

        if (cbpass == true) {
            Password.setText(password);
            Name.setText(name);
            CBPass.setChecked(cbpass);

        } else {
            Password.setText("");
            Name.setText(name);
            CBPass.setChecked(cbpass);

        }
    }

}
这里写代码片
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:background="@drawable/loginbg"
    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"
    tools:context="cn.edu.bzu.case_login.LoginActivity">

    <include layout="@layout/login_top"></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" />

</RelativeLayout>
这里写代码片
activity_wel.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 You"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="208dp"
        android:textSize="25sp"
        android:id="@+id/welcome" />

</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:background="@drawable/logintop_roungbg"
    android:padding="@dimen/activity_horizontal_margin">

    <EditText
        android:id="@+id/etName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:drawable/edit_text"
        android:drawableLeft="@drawable/icon_user"
        android:drawablePadding="10dp"
        android:ems="10"
        android:hint="@string/etName">

        <requestFocus />
    </EditText>

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

        <requestFocus />
    </EditText>

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

        <CheckBox
            android:id="@+id/cbPass"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_weight="0.26"
            android:text="@string/btnSave"
            android:textSize="20sp" />


        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_weight="1"
            android:background="@drawable/btn_select"
            android:onClick="save"
            android:text="@string/btnLogin" />

    </LinearLayout>
</RelativeLayout>

用户登录界面

这里写图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值