我想要一个复选框按钮来记住用户 ID 和密码。谁能指导我如何开始正确的方向?
最佳答案
我刚刚将它内置到我的应用程序中,这是基本代码和一些解释:
基本上这里的关键是 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() 方法只是加载另一个 Activity 。
这是登录页面的基本 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 上进行了测试。让我知道它是如何为您工作的。
大卫
关于android - 添加一个 "Remember me"复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9370293/