问题描述
我有一个用户名,密码和复选框(旁边的文字记住我)。
I have a username, password, and checkbox (next to the text 'remember me').
我如何实现一个记得我功能,以保持用户名和密码的数据??
How do I to implement a remember me function to keep username and password data??
任何帮助将是AP preciated。
Any help would be appreciated.
推荐答案
可以的的保存与应用程序相关的值。
You can save values associated with your application using Preferences.
定义一些静态存储preference文件名和密钥,你要使用:
Define some statics to store the preference file name and the keys you're going to use:
public static final String PREFS_NAME = "MyPrefsFile";
private static final String PREF_USERNAME = "username";
private static final String PREF_PASSWORD = "password";
您会然后保存用户名和密码,如下所示:
You'd then save the username and password as follows:
getSharedPreferences(PREFS_NAME,MODE_PRIVATE)
.edit()
.putString(PREF_USERNAME, username)
.putString(PREF_PASSWORD, password)
.commit();
所以,你会找回它们是这样的:
So you would retrieve them like this:
SharedPreferences pref = getSharedPreferences(PREFS_NAME,MODE_PRIVATE);
String username = pref.getString(PREF_USERNAME, null);
String password = pref.getString(PREF_PASSWORD, null);
if (username == null || password == null) {
//Prompt for username and password
}
另外,如果你不想preferences文件,你可以使用默认的命名:
Alternatively, if you don't want to name a preferences file you can just use the default:
SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this);
这篇关于如何实现在Android活性的“记住我”功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!