android复选框记住密码注册,android – 添加“记住我”复选框

我刚刚将其构建到我的应用程序中,这里是基本代码和一些解释:

基本上这里的关键是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文件:

login.xml

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:background="#000"

android:padding="10dp" >

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" />

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" />

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" />

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" />

重要信息:在将密码存储在SharedPreferences之前,您可能需要加密密码.这个细节超出了这个问题的范围,但这里是我曾经做过的代码:http://www.androidsnippets.com/encryptdecrypt-strings.你也必须提出一些关键模式.

此代码已在Android 2.1,SDK 7上进行了测试.让我知道如何为您工作.

大卫

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值