我在android中的remember me
函数中工作,我在存储类中存储了用户名和密码,storage
类是我实现Shared preferences.
我做了什么
首先我要检查是否选中了复选框,如果是缺陷,那么我将值存储在存储类(共享首选项)中。在登录服务中,我使用密码调用用户名。
以下是代码:
Inside OnCreate
boolean isChecked = true;
login_submit_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
LoginService();
}
});
chkRememberMe.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
{
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
storage.saveUserName(Login.this, mUserName);
storage.savepass(Login.this, mPassword);
}
}
});
}
外:
protected void LoginService() {
// TODO Auto-generated method stub
mUserName = login_email_edit.getText().toString().trim();
mPassword = login_password_edit.getText().toString().trim();
if (chkRememberMe.isChecked()) {
mUserName=storage.getUserName(Login.this);
mPassword=storage.getpass(Login.this);
}
if (mUserName.length() == 0) {
AlertDialog(" Enter Email ", Login.this);
} else if (isContainsEmpty(mUserName)) {
login_submit_btn.setEnabled(true);
AlertDialog("Valid Email Address", Login.this);
}
else if(mPassword.length() == 0){
AlertDialog("Password should not be blank", Login.this);
}
}
任何人都能弄明白我的错误。@谢谢
答案 0 :(得分:1)
您的代码似乎不对,您保存凭据但不在任何地方删除它们。此外,如果我更改用户名和密码并且已经检查过记住我,那么您的代码不会保存凭据。 删除setOnCheckedChangeListener,没用。
这应该是一个过程:
当您输入登录活动时,您应检查是否已存储任何凭据,如果是,您应在TextViews
(用户名和密码字段)中显示这些凭据,并且记住我应该是默认情况下已选中。
现在,如果之前没有存储凭据(例如,首次登录),则不会显示任何内容,当用户按下登录按钮并且登录成功时
2.1。如果记住了我,那么你应该保存凭证。
2.2。否则,如果未选中,请清除凭据
编辑:
删除setOnCheckedChangeListener
email=storage.getUserName(Login.this);
password=storage.getpass(Login.this);
放
login_email_edit.setText(email);
login_password_edit.setText(password);
现在在登录服务中
if(chkRememberMe.isChecked()) {
storage.saveUserName(Login.this, mUserName);
storage.savepass(Login.this, mPassword);
}else
{
storage.saveUserName(Login.this, "");
storage.savepass(Login.this, "");
}
删除应用并重新安装
答案 1 :(得分:0)
我也做同样的功能。
这是我的代码供参考。看看这是否可以帮到你:D
SharedPreferences mSharedPreferences = getSharedPreferences("myapp_uid", 0);
SharedPreferencesEditor mSharedPreferencesEditor = mSharedPreferences.edit();
App.mSharedPreferencesEditor.putString("index", "value");
if(!App.mSharedPreferencesEditor.commit()){
// Error message here
}
此外,我将它作为静态方法存储在类中。
public static boolean setPref(String index, String value){
App.mSharedPreferencesEditor.putString("index", "value");
return App.mSharedPreferencesEditor.commit();
}
public static String getPref(String index){
.....
}
::已编辑
内
boolean isChecked = true;
login_submit_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
LoginService();
}
});
}
外
protected void LoginService() {
// TODO Auto-generated method stub
mUserName = login_email_edit.getText().toString().trim();
mPassword = login_password_edit.getText().toString().trim();
if (chkRememberMe.isChecked()) {
storage.saveUserName(Login.this, mUserName);
storage.savepass(Login.this, mPassword);
} else {
storage.saveUserName(Login.this, "");
storage.savepass(Login.this, "");
}
if (mUserName.length() == 0) {
AlertDialog(" Enter Email ", Login.this);
} else if (isContainsEmpty(mUserName)) {
login_submit_btn.setEnabled(true);
AlertDialog("Valid Email Address", Login.this);
}
else if(mPassword.length() == 0){
AlertDialog("Password should not be blank", Login.this);
}
}
答案 2 :(得分:0)
我使用Sharedpreference来实现相同的功能,
EditText login_email_edit=(EditText)findViewById(R.id.editText1);
Button login_submit_btn=(Button)findViewById(R.id.button1);
final CheckBox chkRememberMe=(CheckBox)findViewById(R.id.checkBox1);
SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", 0);
editor = pref.edit();
if(pref.getString("key_username", null)!=null)
{
text.setText(pref.getString("key_username", null));//Here we retrieve username from shared preference and set it to edittext.
}
login_submit_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(chkRememberMe.isChecked())
{
editor.putString("key_username", text.getText().toString());
editor.commit();
LoginService();//Now your username is stored in shared preference
}
else
{
//Username is not stored.because remember me is unchecked
LoginService();
}
}
});
答案 3 :(得分:0)
使用共享参考完全记住我的功能
<强> XML:强>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:id="@+id/login_form"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="vertical">
<EditText
android:id="@+id/etUserName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:hint="Username"
android:padding="10dp" />
<EditText
android:id="@+id/etPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Password"
android:padding="10dp" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/etPassword"
android:orientation="horizontal">
<ImageView
android:id="@+id/ivRememberMe"
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_centerVertical="true"
android:src="@drawable/holo_circle" />
<TextView
android:id="@+id/tvRememberMe"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="6dp"
android:layout_toRightOf="@+id/ivRememberMe"
android:text="Remember Me"
android:textAppearance="?android:attr/textAppearanceSmall" />
</RelativeLayout>
<Button
android:id="@+id/btnSignIn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:text="Sign in"
android:textColor="@android:color/white" />
</LinearLayout>
</RelativeLayout>
登录课程:
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private ImageView ivRememberMe;
private Button btnLogin;
private EditText etUserName, etPassword;
private String userName, password;
private static boolean rememberMe;
String USERNAME = "username";
String PASSWORD = "password";
String REMEMBER_ME = "remember me";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initValues();
}
private void initValues() {
try {
btnLogin = (Button) findViewById(R.id.btnSignIn);
btnLogin.setOnClickListener(this);
etUserName = (EditText) findViewById(R.id.etUserName);
etPassword = (EditText) findViewById(R.id.etPassword);
//Persist credentials, if remember me is checked
SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this);
if (pref.contains(USERNAME) && pref.contains(PASSWORD)) {
etUserName.setText(pref.getString(USERNAME, ""));
etPassword.setText(pref.getString(PASSWORD, ""));
if (pref.getBoolean(REMEMBER_ME, true)) {
((ImageView) findViewById(R.id.ivRememberMe)).setImageResource(R.drawable.checkbox_marked_circle);
}
}
ivRememberMe = (ImageView) findViewById(R.id.ivRememberMe);
ivRememberMe.setOnClickListener(this);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void onClick(View v) {
try {
switch (v.getId()) {
case R.id.btnSignIn:
validateCredentials();
onLogin();
break;
case R.id.ivRememberMe:
rememberMe = !rememberMe;
((ImageView) findViewById(R.id.ivRememberMe)).setImageResource(rememberMe ? R.drawable.checkbox_marked_circle : R.drawable.holo_circle);
checkPersistCredentials(etUserName.getText().toString().trim(), etPassword.getText().toString());
break;
default:
break;
}
} catch (Exception e) {
e.printStackTrace();
}
}
private void validateCredentials() {
try {
userName = etUserName.getText().toString();
password = etPassword.getText().toString();
} catch (Exception e) {
e.printStackTrace();
}
}
public void onLogin() {
//implement your login logic
}
private void checkPersistCredentials(String userName, String password) {
rememberMe(userName, password, this);
}
public void rememberMe(final String username, String password, Context context) {
try {
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
SharedPreferences.Editor edit = sharedPreferences.edit();
if (rememberMe) {
if (username.isEmpty() && password.isEmpty()) {
} else {
edit.putString(USERNAME, username);
edit.putString(PASSWORD, password);
edit.putBoolean(REMEMBER_ME, true);
edit.commit();
}
} else {
edit.clear();
edit.commit();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}