【问题标题】:Save username and password using Preferences使用首选项保存用户名和密码
【发布时间】:2020-04-28 15:19:53
【问题描述】:

我在使用首选项在我的应用中保存数据登录时遇到了问题。我第一次已经保存了用户名和密码,但下次我不知道如何在填写字段用户名后保存自动填充/自动完成密码。有谁能够帮我。非常感谢。

【问题讨论】:

    标签: android autocomplete preferences


    【解决方案1】:

    试试这个代码,这对你有帮助:)

    // 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();
            }
        });
    

    【讨论】:

    • tks 你的代码。但这不是我想要的。按照您的代码,编辑器刚刚在第一次保存。在我登录其他帐户并使用此代码后,我再次登录该帐户作为第一次,我必须填写所有用户名和密码。我需要什么,我只需要填写字段用户名和密码,当它们之前已经保存时,它们将自动填充/自动完成。你知道我的意思。不过,当我在我的主题中写清楚含义时,我很抱歉。
    • 好的,但我很遗憾地说这在 SharedPreferences 中是不可能的,您需要使用数据库,因为 sharedprefrence 只处理一个每次都会覆盖的键值。
    • 例如,请参阅此链接以获取您的解决方案codeofaninja.com/2013/11/…
    猜你喜欢
    • 2018-02-08
    • 2018-03-27
    • 1970-01-01
    • 2012-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-04
    相关资源
    最近更新 更多