【问题标题】:Add a "Remember me" checkbox添加“记住我”复选框
【发布时间】:2021-09-18 00:07:15
【问题描述】:

我想要一个复选框按钮来记住用户 ID 和密码。谁能指导我如何开始正确的方向?

【问题讨论】:

  • 除了记住我功能之外,您是否已经构建了完整的登录功能?

标签: android


【解决方案1】:

看到这个答案,我会考虑我想用这些数据做什么。如果您想将此数据保存在本地存储中或异步执行此操作。

loginPrefsEditor.commit();

  • 将您的数据保存在本地存储中并阻止您的 UI 主线程。

loginPrefsEditor.apply();

  • 在这种情况下进行异步工作,它不会阻塞您的主线程。

【讨论】:

    【解决方案2】:

    我刚刚将它内置到我的应用程序中,这是基本代码和一些解释:

    基本上这里的关键是 SharedPreferences。您将设置一个 SharedPreferences 对象并在用户输入后存储您的用户名和密码。然后,当他们再次运行应用程序时,首选项将存储他们的数据,然后重新填充登录字段。

    LoginActivity.java

    package com.realsimpleapps.LoginTesting;
    
    import android.app.Activity;
    import android.content.Context;
    import android.content.Intent;
    import android.content.SharedPreferences;
    import android.os.Bundle;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.view.inputmethod.InputMethodManager;
    import android.widget.Button;
    import android.widget.CheckBox;
    import android.widget.EditText;
    
    public class LoginActivity extends Activity implements OnClickListener {
    
        private String username,password;
        private Button ok;
        private EditText editTextUsername,editTextPassword;
        private CheckBox saveLoginCheckBox;
        private SharedPreferences loginPreferences;
        private SharedPreferences.Editor loginPrefsEditor;
        private Boolean saveLogin;
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.login);
    
            ok = (Button)findViewById(R.id.buttonLogin);
            ok.setOnClickListener(this);
            editTextUsername = (EditText)findViewById(R.id.editTextUsername);
            editTextPassword = (EditText)findViewById(R.id.editTextPassword);
            saveLoginCheckBox = (CheckBox)findViewById(R.id.saveLoginCheckBox);
            loginPreferences = getSharedPreferences("loginPrefs", MODE_PRIVATE);
            loginPrefsEditor = loginPreferences.edit();
    
            saveLogin = loginPreferences.getBoolean("saveLogin", false);
            if (saveLogin == true) {
                editTextUsername.setText(loginPreferences.getString("username", ""));
                editTextPassword.setText(loginPreferences.getString("password", ""));
                saveLoginCheckBox.setChecked(true);
            }
        }
    
        public void onClick(View view) {
            if (view == ok) {
                InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(editTextUsername.getWindowToken(), 0);
    
                username = editTextUsername.getText().toString();
                password = editTextPassword.getText().toString();
    
                if (saveLoginCheckBox.isChecked()) {
                    loginPrefsEditor.putBoolean("saveLogin", true);
                    loginPrefsEditor.putString("username", username);
                    loginPrefsEditor.putString("password", password);
                    loginPrefsEditor.commit();
                } else {
                    loginPrefsEditor.clear();
                    loginPrefsEditor.commit();
                }
    
                doSomethingElse();
            }
        }
    
        public void doSomethingElse() {
            startActivity(new Intent(LoginActivity.this, MainActivity.class));
            LoginActivity.this.finish();
        }
    }
    

    最后的方法 doSomethingElse() 是您进入应用程序下一步的占位符。我的 doSomethingElse() 方法只是加载另一个活动。

    这是登录页面的基本 xml 文件:

    登录.xml

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="#000"
        android:padding="10dp" >
    
        <EditText
            android:id="@+id/editTextUsername"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_below="@+id/imageView1"
            android:hint="Username"
            android:inputType="textNoSuggestions"
            android:imeOptions="actionNext" />
    
        <EditText
            android:id="@+id/editTextPassword"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_below="@+id/editTextUsername"
            android:hint="Password"
            android:inputType="textPassword"
            android:imeOptions="actionDone" />
    
        <CheckBox
            android:id="@+id/saveLoginCheckBox"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_below="@+id/editTextPassword"
            android:text="Save Login?"
            android:textColor="#FFF" />
    
        <Button
            android:id="@+id/buttonLogin"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_below="@+id/saveLoginCheckBox"
            android:layout_marginTop="40dp"
            android:text="Login" />
    
    </RelativeLayout>
    

    重要提示:您可能希望在将密码存储到 SharedPreferences 之前对其进行加密。详细信息超出了此问题的范围,但这是我用来执行此操作的代码:http://www.androidsnippets.com/encryptdecrypt-strings。你也必须想出某种关键模式。

    此代码已在 Android 2.1、SDK 7 上进行了测试。让我知道它是如何为您工作的。

    大卫

    【讨论】:

    • 非常感谢大卫的所有帮助!我花了大约 40 分钟才明白。我现在已经启动并运行了:)
    • @dbDev,进行了一些调整,但效果很好。谢谢!
    • 谢谢伙计!这完美!
    猜你喜欢
    • 2014-02-04
    • 2017-11-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多