【问题标题】:Using SharedPreference to save user details使用 SharedPreference 保存用户详细信息
【发布时间】:2017-01-01 21:39:01
【问题描述】:

我了解保存值的最佳方法是使用 SharedPreferences,例如

SharedPreferences Savesettings = getSharedPreferences("settingFile", MODE_PRIVATE);
        SharedPreferences.Editor example = Savesettings.edit();
        example.putString("Name", name)
                .putInt("Age", age)
                .putInt("Score", score)
        example.apply();

但是,如果我希望我的程序在用户关闭和打开程序后记住某个按钮被禁用或启用,该怎么办?我已经尝试过 RegisterOnChangePreferanceListener 但是我没有运气,例如

SharedPreferences Preferences= PreferenceManager.getDefaultSharedPreferences(this);
        SharedPreferences.OnSharedPreferenceChangeListener Example =
                new SharedPreferences.OnSharedPreferenceChangeListener() {
                    public void onSharedPreferenceChanged(SharedPreferences preferance, String key) {

                        Name = name;//only an example not the main focus
                        Age = age;
                        Score = score;
                        enableBTN = false; //disables button
                        Name.setEnabled(false); //disables the edit text from further editing
}
                };
        Preferences.registerOnSharedPreferenceChangeListener(Example);

有没有办法做到这一点,这两种方法似乎都不适合我。

【问题讨论】:

标签: android sharedpreferences


【解决方案1】:

您需要从按钮的OnClickListener 中保存它。这样,每次单击按钮时,都可以保证保存按钮的状态。 button 是对 Button 视图对象的引用

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle bundle) {
        Button button = (Button)findViewById("this_button_view_id");
        EditText editText = (EditText)findViewById("this_edit_text_id");
        SharedPreferences Savesettings = getSharedPreferences("settingFile", MODE_PRIVATE);

        // If the Savesettings shared preferences above contains the "isButtonDisabled" key
        // It means the user clicked and disabled the button before
        // So we use that state instead
        // If it does does not contain that key
        // We set it to true so that the button is not disabled
        // Same for the edit text
        button.setEnabled(Savesettings.contains("isButtonDisabled") ? Savesettings.getBoolean("isButtonDisabled") : true);
        editText.setEnabled(Savesettings.contains("isEditTextDisabled") ? Savesettings.getBoolean("isEditTextDisabled") : true);

        button.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                // disable the edit text
                editText.setEnabled(false);

                // disable the button
                button.setEnabled(false);

                SharedPreferences.Editor example = Savesettings.edit();

                // Save the button state to the shared preferences retrieved above
                example.putBoolean("isButtonDisabled", true);

                // Save the edit text state to the shared preferences retrieved above
                example.putBoolean("isEditTextDisabled", true);
                example.apply();

            }
        });
    }
}

【讨论】:

  • 但是如果我想设置不同的背景颜色或禁用编辑文本怎么办?
  • 当按钮被点击时?我的意思是你可以在OnClickListener 中做任何你想要的逻辑
  • 在按钮单击中重新创建活动没有意义,对吧?你为什么要这样做?您可以关闭活动或启动新活动,这更有意义。共享首选项的保存绑定在 Savesettings.edit();example.apply(); 之间,不要在其中添加任何其他代码
  • 但是如何做到这一点,我可以举个例子吗?假设我想禁用名为“EDIT_name”的Edittext我该怎么做?我知道它的 EDIT_name.setEnabled(false);但是如何实现呢?
  • 我只是用 recreate(); 举例说明
猜你喜欢
  • 2017-03-08
  • 1970-01-01
  • 2018-11-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-08
  • 2018-11-23
相关资源
最近更新 更多