0

我正在尝试Preferences通过编写一个短程序来存储ted密钥board然后将值加载到boardstr. 当我使用getString加载值时,值boardstr不会改变。

boardstr= new String();      
boardstr="fred";
// set the prefrence to ted
this.getPreferences(MODE_PRIVATE).edit().putString("board","ted");
// kload the prfrence in boardstr
this.getPreferences(MODE_PRIVATE).getString("board",boardstr);
// boardstr stil equals fred, not ted 
4

1 回答 1

4

您需要其中一个commit()apply()更改才能保存并生效。

例如:

// set the prefrence to ted
this.getPreferences(MODE_PRIVATE).edit().putString("board","ted");
this.getPreferences(MODE_PRIVATE).edit().apply();
于 2011-01-25T03:47:14.057 回答