用户登录记住密码(SharedPreferences的使用)

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

SharedPreferences是安卓平台上一个轻量级的存储类,主要用于存储一些应用程序的配置参数,

例如用户名,密码,自定义参数的设置等。


一、LoginActivity

package cn.edu.bzu.case_login;

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

public class LoginActivity extends AppCompatActivity {
    private EditText etName;
    private EditText etPassword;
    private CheckBox cbIsRememberPass;
    private  SharedPreferences sharedPreferences;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initViews();
        sharedPreferences= getSharedPreferences("rememberpassword",Context.MODE_PRIVATE);
         boolean isRemember=sharedPreferences.getBoolean("rememberpassword",false);
        if (isRemember ) {
            String name=sharedPreferences.getString("name","");
            String password=sharedPreferences.getString("password","");
            etName.setText(name);
            etPassword.setText(password);
            cbIsRememberPass.setChecked(true);

        }
    }

    private void initViews() {
        etName = (EditText) findViewById(R.id.etName);
        etPassword = (EditText) findViewById(R.id.etPassword);
        cbIsRememberPass=(CheckBox) findViewById(R.id.cbIsRememberPass);
    }

    public void login(View view) {
        String name = etName.getText().toString();
        String password = etPassword.getText().toString();
        if ("admin".equals(name) && "123456".equals(password)) {
            SharedPreferences.Editor editor=sharedPreferences.edit();
            if(cbIsRememberPass.isChecked()){
                editor.putBoolean("rememberpassword",true);
                editor.putString("name",name);
                editor.putString("password",password);

            }else{
                editor.clear();
            }
            editor.commit();
            Intent intent=new Intent(this,MainActivity.class);
            startActivity(intent);
            finish();
        }else{
            Toast.makeText(this,"账号或密码有误",Toast.LENGTH_LONG).show();
        }
    }
}
二、MainActivity

package cn.edu.bzu.case_login;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}
三、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/logintop_roundbg">

    <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/cbIsRememberPass"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:textSize="20sp"
            android:text="记住密码" />

        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@drawable/btn_select"
            android:text="登录"
            android:onClick="login" />

    </LinearLayout>
</RelativeLayout>
四、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="cn.edu.bzu.case_login.MainActivity">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Welcome you"
        android:layout_marginTop="263dp"
        android:layout_centerHorizontal="true"
        android:layout_alignParentTop="true"
        android:textSize="40sp" />
</RelativeLayout>

五、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="cn.edu.bzu.case_login.LoginActivity">

    <include layout="@layout/login_top"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"></include>

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

</RelativeLayout>
六、btn_select.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/btn_shape" android:state_pressed="false"></item>
    <item android:drawable="@drawable/btn_shape_after" android:state_pressed="true"></item>
</selector>

七、btn_shape.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/btn_shape" android:state_pressed="false"></item>
    <item android:drawable="@drawable/btn_shape_after" android:state_pressed="true"></item>
</selector>

八、btn_shape_after.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/btn_shape" android:state_pressed="false"></item>
    <item android:drawable="@drawable/btn_shape_after" android:state_pressed="true"></item>
</selector>
九、loginbg.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <gradient  
        android:startColor="#FFACDAE5"
        android:endColor="#FF72CAE1"
        android:angle="45"
         />
</shape>
十、logintop_roundbg.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <gradient  
        android:startColor="#FFACDAE5"
        android:endColor="#FF72CAE1"
        android:angle="45"
         />
</shape>
 


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值