Add a “Remember me” checkbox

前端 未结 2 474
感情败类
感情败类 2020-11-29 02:20

I would like to have a Checkbox button to Remember User Id and Password. Can anyone please guide me in the right direction as to how to get it started?

相关标签:
2条回答
  • 2020-11-29 02:57

    Seeing this answer i will consider what i want to do with this data. If you want to persist this data in local storage or doing this asyncronously.

    loginPrefsEditor.commit();

    • Persists your data in local storage and block your UI Main Thread.

    or

    loginPrefsEditor.apply();

    • In this case makes an asyncronous work and it doesn´t block you main thread.
    0 讨论(0)
  • 2020-11-29 03:00

    I just built this into my app, here's the basic code and some explanation:

    Basically the key here is SharedPreferences. You'll setup a SharedPreferences object and store your username and password after the user has entered them. Then, when they run the application again, the preferences will have their data stored and will then repopulate the login fields.

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

    The method at end, doSomethingElse() is your placeholder to go to the next step for your application. My doSomethingElse() method simply loads another activity.

    Here's a basic xml file for the login page:

    login.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>
    

    IMPORTANT: You'll likely want to encrypt the password before storing it in SharedPreferences. Details for that are beyond the scope of this question, but here is the code I used to do that: http://www.androidsnippets.com/encryptdecrypt-strings. You'll have to come up with some kind of key schema too.

    This code has been tested on Android 2.1, SDK 7. Let me know how it works for you.

    David

    0 讨论(0)
提交回复
热议问题