我在使用偏好设置来保存我的应用程序中的数据登录时遇到了麻烦。我已经保存了用户名和密码,在第一次,但下一次我不知道如何保存,使自动填充utocomplete密码后填写字段用户名。任何人都可以帮助我。Thanx so much.
试试这个代码,这将帮助你:)
// 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();
}
});