添加"记住我"复选框 [英] Add a "Remember me" checkbox

查看:172
本文介绍了添加"记住我"复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道已经有大量的文章,在这里,介绍如何添加一个记住我功能的Andr​​oid应用程序,但是,我仍然无法很搞清楚3小时研究和实验后。

I know there are already plenty of articles on here that describe how to add a "Remember me" function on the Android App but, I still cant quite figure it out after 3 hours of researching and experimenting.

基本上,我是一个begginner在Android开发。我想有一个复选框按钮来记住用户名和密码。任何人都可以请指导我在正确的方向,如何得到它开始了吗?

Basically, I am a begginner at Android development. 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?

感谢。

推荐答案

我刚刚建立了这个为我的应用程序,这里的基本code和一些说明:

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

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

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

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()方法简单地加载其它活动。

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

下面的登录页面一个基本的XML文件:

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

login.xml

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>

重要提示:您很可能希望将密码存放在共享preferences之前加密。对于细节超出了这个问题的范围,但这里是code我以前做的是:<一href="http://www.androidsnippets.com/encryptdecrypt-strings">http://www.androidsnippets.com/encryptdecrypt-strings.你必须拿出一些关键的架构太。

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.

这code已经过测试,在Android 2.1,SDK 7。让我知道它是如何为你工作。

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

大卫

这篇关于添加&QUOT;记住我&QUOT;复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆