Android案例:通过SharePreferences实现记住密码功能

SharedPreferences:
SharedPreferences是Android平台上一个轻量级的存储器,主要用于存储一些应用程序的配置参数。SharedPreferences中存储的数据是以key/value键值对的形式保存在xml文件中,该文件位于data/data//shared_prefs文件夹中。
1、设计界面
界面采用相对布局放置了两个EditText用来输入账号和密码,下方又放了一个线性布局LinearKayout,里面存放了一个ChechBox和一个Button,用来选择是否记住密码和点击登录。
这里写图片描述

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#A4D3EE"
    tools:context="com.example.login123.LoginActivity">

    <include layout="@layout/login_top"></include>

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/deer"
        android:id="@+id/imageView"
        android:layout_alignParentBottom="true" />

</RelativeLayout>

login_top.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="32dp">
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/etName"
        android:drawableLeft="@drawable/icon_user"
        android:drawablePadding="10dp"
        android:ems="10"
        android:hint="请输入账号"
        android:background="#ffffff">

        <requestFocus/>
    </EditText>
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/etPassword"
        android:layout_below="@id/etName"
        android:drawableLeft="@drawable/icon_pass"
        android:drawablePadding="10dp"
        android:ems="10"
        android:hint="请输入密码"
        android:background="#ffffff"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/etPassword">
        <CheckBox
            android:text="记住密码"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:id="@+id/cbIsRememberPass"
            android:textSize="20sp"
            android:layout_weight="1"/>
        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="#6495ED"
            android:onClick="login"
            android:text="登录"/>
    </LinearLayout>

</RelativeLayout>

2、界面功能实现
在LoginActivity中,当用户输入完账号和密码后,选择记住密码,点击“登录”按钮时将信息保存到SharedPreferences中。相应代码如下:

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_main);
            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);
            }
        }

        public 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,TwoActivity.class);
                startActivity(intent);
                finish();
            }else {
                Toast.makeText(this,"账号或密码错误",Toast.LENGTH_LONG);
            }

        }
    }

输入账号密码:
这里写图片描述
进入欢迎界面:
这里写图片描述
点击记住密码登陆之后,关闭APP,再次进入同样显示有账号密码。如若没有记住密码则需要再次输入。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值