使用偏好设置保存用户名和密码

问题描述 投票:3回答:2

我在使用偏好设置来保存我的应用程序中的数据登录时遇到了麻烦。我已经保存了用户名和密码,在第一次,但下一次我不知道如何保存,使自动填充utocomplete密码后填写字段用户名。任何人都可以帮助我。Thanx so much.

android autocomplete preferences
2个回答
6
投票

试试这个代码,这将帮助你:)

// Get SharedPreferences

 SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());

// set UI

  EditText username = (EditText) findViewById(R.id.username);
  EditText password = (EditText) findViewById(R.id.username);

// get value from existing preference


 strusername = sharedPreferences.getString("username", "");
 strpassword = sharedPreferences.getString("password", "");

// set existing value
username.setText(strusername);
password.setText(strpassword);

findViewById(R.id.login).setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
            // put value in preference on button click
            Editor editor = sharedPreferences.edit();
            editor.putString("username", username.getText().toString());
            editor.putString("password", password.getText().toString());
            editor.commit();
        }
    });
© www.soinside.com 2019 - 2024. All rights reserved.