如何使用SharedPreferences值填充EditText?

时间:2016-05-31 16:52:18

标签: android android-edittext sharedpreferences storage

我想知道如何使用SharedPreferences的值填充EditText字段。我的代码如下:

 protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(this);
        sharedPreferences = getSharedPreferences(getString(R.string.preference_file_key),MODE_PRIVATE);
        textViewCheck();
    }

    public void textViewCheck()
    {
        EditText sil_key = (EditText)findViewById(R.id.silent_key);
        String silent_mode_key = sil_key.getText().toString();
        EditText gen_key = (EditText)findViewById(R.id.general_key);
        String general_mode_key = gen_key.getText().toString();
        EditText vib_key = (EditText)findViewById(R.id.vibrate_key);
        String vibrate_mode_key = vib_key.getText().toString();
        SharedPreferences.Editor editor = sharedPreferences.edit();
        if((sharedPreferences.contains("silent")) && (sharedPreferences.contains("general")) && (sharedPreferences.contains("vibrate")))
        {
            sil_key.setText(sharedPreferences.getString("silent","silent1"));
            gen_key.setText(sharedPreferences.getString("general","general1"));
            vib_key.setText(sharedPreferences.getString("vibrate","vibrate1"));
        }
        editor.putString("silent",silent_mode_key);
        editor.putString("general",general_mode_key);
        editor.putString("vibrate",vibrate_mode_key);
        editor.apply();
    }

即使在EditText字段中更改后,我仍然保持相同的值。我想知道为什么我一直这样做以及如何克服这个问题。 (我很抱歉使用getString()方法之前的错误,没有注意到。请澄清这一点)

3 个答案:

答案 0 :(得分:0)

我认为你在创建字符串时可能会搞乱

EditText sil_key = (EditText)findViewById(R.id.silent_key);
String silent_mode_key = sil_key.toString();

我认为你的意思是从编辑文本的内容中创建一个字符串 像这样

    String silent_mode_key = sil_key.getText().toString();

试试这个

    EditText sil_key = (EditText)findViewById(R.id.silent_key);
    String silent_mode_key = sil_key.getText().toString();
    EditText gen_key = (EditText)findViewById(R.id.general_key);
    String general_mode_key = gen_key.getText().toString();
    EditText vib_key = (EditText)findViewById(R.id.vibrate_key);
    String vibrate_mode_key = vib_key.getText().toString();
    SharedPreferences.Editor editor = sharedPreferences.edit();
    if((sharedPreferences.contains("silent")) && (sharedPreferences.contains("general")) && (sharedPreferences.contains("vibrate")))
    {
        sil_key.setText(sharedPreferences.getString("silent","silent"));
        gen_key.setText(sharedPreferences.getString("general","general"));
        vib_key.setText(sharedPreferences.getString("vibrate","vibrate"));
    }
    editor.putString("silent",silent_mode_key);
    editor.putString("general",general_mode_key);
    editor.putString("vibrate",vibrate_mode_key);
    editor.apply();

答案 1 :(得分:0)

尝试为所有字符串添加getText(),如下所示:

EditText sil_key = (EditText)findViewById(R.id.silent_key);
    String silent_mode_key = sil_key.getText().toString();

答案 2 :(得分:0)

我认为你必须在sharedPreferences.getString

之前添加它
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);