使用SharedPreferences保存用户详细信息

时间:2014-11-04 09:36:40

标签: android android-studio

嗨我希望我的应用程序在用户登录后记住用户的登录详细信息,直到用户按下"退出"按钮。我正在使用这种风格

private CheckBox mCheckSavePassword;
    mCheckSavePassword.setChecked(preferences.getBoolean("prefSavePassword", false));
    SharedPreferences.Editor preferencesEditor = preferences.edit();
        preferencesEditor.putString("prefUserName", username);
            if(mCheckSavePassword.isChecked()) {
                    preferencesEditor.putString("prefPassword", password);
             }
             preferencesEditor.putBoolean("prefSavePassword", mCheckSavePassword.isChecked());
             preferencesEditor.commit();

但它不起作用

3 个答案:

答案 0 :(得分:1)

这是用户登录成功时保存用户详细信息的方式。

此外,如果您想在下次用户进入登录屏幕时从共享偏好设置中填充用户名,此代码将为您提供帮助。

如果方向已更改,我还包含了在EditText中填充共享偏好值的代码。

    //Form Variables
EditText userName; 
CheckBox chkRememberMe;

//SharedPrefernces Variables to save userName and password of User
SharedPreferences loginPreferences;
private static final String SPF_NAME = "vidslogin";
private static final String USERNAME = "username";


@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);

    //Getting Values from edit text fields
    userName = (EditText) findViewById(R.id.login_et_username);

    //Check box  to remember UN and PSW
    chkRememberMe = (CheckBox) findViewById(R.id.cb_rememberMe);

    //Read previously saved   userName from sharedPreferences  and populate in the editText
    loginPreferences = getSharedPreferences(SPF_NAME, Context.MODE_PRIVATE);
    userName.setText(loginPreferences.getString(USERNAME, ""));
}

//Saving Activity State
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
    super.onSaveInstanceState(savedInstanceState);
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);

    // Checks the orientation of the screen
    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
        //Read previously saved   userName & password from sharedPreferences 
        loginPreferences = getSharedPreferences(SPF_NAME, Context.MODE_PRIVATE);
        userName.setText(loginPreferences.getString(USERNAME, ""));

    } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
        //Read previously saved   userName & password from sharedPreferences 
        loginPreferences = getSharedPreferences(SPF_NAME, Context.MODE_PRIVATE);
        userName.setText(loginPreferences.getString(USERNAME, ""));

    }
}

// login function

public void login(){

//Check if he is authenticated
// if Authenticated and if the user checks "REMEMBER ME"-- Checkbox, save his details
//Saving userName into SharedPreferences to read for next time 

   String strUserName = userName.getText().toString().trim();
    if (chkRememberMe.isChecked()){
    loginPreferences = getSharedPreferences(SPF_NAME, Context.MODE_PRIVATE);
    loginPreferences.edit().putString(USERNAME, strUserName).commit();
    } 
}

希望它能解决。

答案 1 :(得分:0)

请参阅以下写作用法

    SharedPreferences sharedpreferences = getSharedPreferences("pref_file_name", Context.MODE_PRIVATE);
    Editor editor = sharedpreferences.edit();
    editor.putString("prefUserName", username);
    if (mCheckSavePassword.isChecked()) 
        editor.putString("prefPassword", password);
    editor.putBoolean("prefSavePassword", mCheckSavePassword.isChecked());
    editor.commit();

你必须把你的逻辑分开(上面只是把它写成偏好) 在屏幕初始化期间,只需读取首选项并初始化复选框和密码框的状态

答案 2 :(得分:0)

请尝试这种方式,希望这有助于您解决问题。

如何设置:

SharedPreferences sharedPreferences = getSharedPreferences(getString(R.string.app_name), MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean("prefUserName", username);
editor.putBoolean("prefPassword", mCheckSavePassword.isChecked()?password:"");
editor.commit();

如何获得:

SharedPreferences sharedPreferences = getSharedPreferences("yourSharePreferenceName", MODE_PRIVATE);
mCheckSavePassword.setChecked(sharedPreferences.getString("prefPassword","").trim().length()>0?true:false);