0

remember me在android中工作,我将用户名和密码存储在存储类中,storage该类是我实现的类Shared preferences.

我做了什么

首先,我检查是否选中了复选框,如果选中,则我将值存储在存储类(共享首选项)中。在登录服务中,我使用密码调用用户名。

这是代码:

在 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);
    }
    }

谁能弄清楚我的错误。@谢谢

4

4 回答 4

1

您的代码似乎不正确,您正在保存凭据但没有在任何地方删除它们。另外,如果我更改了用户名和密码并且已经选中了记住我,那么您的代码不会保存凭据。去掉setOnCheckedChangeListener,没用。

应该是这样的过程:

  1. 当您输入登录活动时,您应该检查是否已经存储了任何凭据,如果是,您应该在您的TextViews(用户名和密码字段)中显示它们,并且默认情况下应该选中记住我复选框。

  2. 现在,如果以前没有存储凭据(例如第一次登录),您将不会显示任何内容,并且当用户按下登录按钮并且登录成功时

    2.1。如果选中了记住我,那么您应该保存凭据。

    2.2. 否则,如果未选中,请清除凭据

编辑:

消除setOnCheckedChangeListener

在 onCreate 方法之后

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, "");
}

删除应用程序并重新安装

于 2013-05-20T07:35:06.373 回答
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);
    }
    }
于 2013-05-20T07:05:21.053 回答
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();
                    }




                }
            });
于 2013-05-20T07:36:43.617 回答
0

COMPLETE REMEMBER ME FUNCTIONALITY USING SHARED PRFERENCES

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>

Login class:

    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();
        }

    }
}
于 2016-09-20T07:25:11.157 回答