android - 使用 SharedPreferences 保存用户详细信息

标签 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();

但是没用

最佳答案

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

此外,如果您想在下次用户进入登录屏幕时从共享首选项中输入用户名,此代码将帮助您。

如果方向发生变化,我还包含了在 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();
    } 
}

希望对你有帮助。

关于android - 使用 SharedPreferences 保存用户详细信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26731813/

相关文章:

android - 错误 :failed to find Build Tools revision 23. 0.0 rc2 - Android Studio

android - 无法解析 Glide 上的 GlideApp :4. 2.0

android - 迁移到 Gradle Experimental 2.5 : no such method AndroidConfig 时出现问题

java - Apache 公地网 : Should I create a new FTPClient object on each connection or reuse one?

安卓触控问题

android - Misconfigured App 抱歉,myapp 尚未获准在 App Center 中显示。在安卓应用分享中

android - 我希望能够将数据从 kinect 发送到 android 应用程序

java - TextView 无法转换为 EditText

android - 无法解析 : com. facebook.android :facebook-android-sdk:4. 4.0

java - XML SAX 解析器不工作 - NullPointerException