说说在 Android 中如何实现记住密码功能

我们使用 SharedPreferences 技术来实现记住密码功能。

我们在 说说在 Android 中如何实现强制下线功能 项目的基础上,修改登录界面的布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_horizontal"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:orientation="horizontal">

        <TextView
            android:layout_width="90dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:text="账号:"
            android:textSize="18sp" />

        <EditText
            android:id="@+id/account"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_weight="1" />
    </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:layout_gravity="center_vertical"
            android:text="密码:"
            android:textSize="18sp" />

        <EditText
            android:id="@+id/password"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_weight="1"
            android:inputType="textPassword" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <CheckBox
            android:id="@+id/remember_pwd"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="记住密码"
            android:textSize="18sp" />

    </LinearLayout>

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

这里添加了 CheckBox 控件来勾选【记住密码】,接着修改 LoginActivity :

ublic class LoginActivity extends BaseActivity {

    private static final String TAG = "LoginActivity";

    final String REMEMBER_PWD_PREF = "rememberPwd";
    final String ACCOUNT_PREF = "account";
    final String PASSWORD_PREF = "password";

    /**
     * AES 工具
     */
    private static AES aes = new AES("12345abcdef67890", "1234567890abcdef");

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

        final EditText accountEditText = (EditText) findViewById(R.id.account);
        final EditText passwordEditText = (EditText) findViewById(R.id.password);


        //从 SharedPreferences 中获取【是否记住密码】参数
        final SharedPreferences preference = PreferenceManager.getDefaultSharedPreferences(this);
        boolean isRemember = preference.getBoolean(REMEMBER_PWD_PREF, false);
        final CheckBox rememberPwd = (CheckBox) findViewById(R.id.remember_pwd);

        if (isRemember) {//设置【账号】与【密码】到文本框,并勾选【记住密码】
            accountEditText.setText(preference.getString(ACCOUNT_PREF, ""));
            passwordEditText.setText(aes.decode(preference.getString(PASSWORD_PREF, "")));
            rememberPwd.setChecked(true);
        }

        Button loginBtn = (Button) findViewById(R.id.login);
        loginBtn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                String account = accountEditText.getText().toString();
                String password = passwordEditText.getText().toString();

                if (account.equals("admin") && password.equals("1")) {//登录成功

                    SharedPreferences.Editor editor = preference.edit();
                    if (rememberPwd.isChecked()) {//记住账号与密码
                        editor.putBoolean(REMEMBER_PWD_PREF, true);
                        editor.putString(ACCOUNT_PREF, account);
                        editor.putString(PASSWORD_PREF, aes.encode(password));
                    } else {//清空数据
                        editor.clear();
                    }
                    editor.apply();

                    startActivity(new Intent(LoginActivity.this, MainActivity.class));
                    finish();
                } else {//登录失败,弹出提示
                    Toast.makeText(LoginActivity.this, "账号或密码不正确", Toast.LENGTH_SHORT).show();
                }
            }
        });

    }
}

这里调用 CheckBox 的 isChecked() 方法来获取勾选状态。如果选中,则表示用户想让应用记住密码,我们就把用户的账号与密码都存入 SharedPreferences 文件;如果没有选中,则直接清除 SharedPreferences 文件中的数据即可。

这里采用 AES 对称加密算法,因为保存在 SharedPreferences 文件中的密码必须加密,否则可能存在安全隐患。之所以必须是对称加密,因为当应用打开登录页时,必须把 SharedPreferences 文件中的密码解密出来,才能真正使用它。

关于 AES 算法的具体实现,请参见这里:
说说如何使用 Java 的原生方法实现 AES 算法


引入 AES 后,可能会出现找不到 sun.misc.BASE64Decodersun.misc.BASE64Encoder
的问题。

因为 Android 没有把这个加入到基础包中,请在这里下载 BASE64Encoder.jar,并引入到 Android Studio 的项目依赖中(File → Project Structure → app → Dependencies)。

加入 BASE64Encoder.jar 的依赖包


运行程序:

记住密码功能演示

第一次点击【记住密码】,登录后再退出。下一次登录就会记住密码啦 O(∩_∩)O~

打开 preferences 文件,可以看到我们的密码已经是加密过的咯:

<?xml version='1.0' encoding='utf-8' standalone='yes' ?>
<map>
    <string name="password">IXi1FhgynLxu8hzlfB4L4Q==</string>
    <string name="account">admin</string>
    <boolean name="rememberPwd" value="true" />
</map>

  • 6
    点赞
  • 54
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
Android Studio实现记住账号密码功能可以通过以下步骤: 1. 在登录页面添加两个CheckBox,一个用于记住账号,一个用于记住密码。 2. 在用户登录成功后,判断是否勾选了记住账号和记住密码,如果勾选了,则将账号和密码保存到SharedPreferences。 3. 在登录页面的onCreate()方法,读取SharedPreferences保存的账号和密码,并将其显示在EditText。 4. 在应用启动时,判断SharedPreferences是否保存了账号和密码,如果保存了,则直接跳转到主界面。 下面是一个示例代码: ```java // 保存账号和密码到SharedPreferences SharedPreferences.Editor editor = getSharedPreferences("user_info", MODE_PRIVATE).edit(); if (rememberAccount.isChecked()) { editor.putString("account", etAccount.getText().toString().trim()); } if (rememberPassword.isChecked()) { editor.putString("password", etPassword.getText().toString().trim()); } editor.apply(); // 从SharedPreferences读取账号和密码 SharedPreferences prefs = getSharedPreferences("user_info", MODE_PRIVATE); String account = prefs.getString("account", ""); String password = prefs.getString("password", ""); etAccount.setText(account); etPassword.setText(password); // 判断是否保存了账号和密码 SharedPreferences prefs = getSharedPreferences("user_info", MODE_PRIVATE); String account = prefs.getString("account", ""); String password = prefs.getString("password", ""); if (!TextUtils.isEmpty(account) && !TextUtils.isEmpty(password)) { // 直接跳转到主界面 } ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值