[英]Save username and password using Preferences
我在使用首選項在我的應用程序中保存數據登錄時遇到問題。 我第一次已經保存了用戶名和密碼,但下次我不知道如何在填寫字段用戶名后保存自動填充/自動完成密碼。 有誰能夠幫助我。 非常感謝。
試試這個代碼,這會幫助你:)
// 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();
}
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.