共享偏好? (非常简单的问题!?)

我只是试图将editText中的用户输入存储在共享首选项中,但它无法正常工作:

editText.setOnEditorActionListener(new TextView.OnEditorActionListener() { @Override public boolean onEditorAction(TextView v, int keycode, KeyEvent event) { Log.v(TAG, keyword.getString("keyword", "mDefault")); //IT LOGS OUT THE DEFAULT STRING EVEN **AFTER** STORING THE PREFERENCES BEFORE if (keycode == EditorInfo.IME_ACTION_SEND) { editText.setText(editText.getText().toString()); keywordEditor.putString("keyword", editText.getText().toString()); keywordEditor.commit(); Log.v(TAG, keyword.getString("keyword", "default")); //CORRECT! THIS LINE WORKS } } return true; }); 

当我第一次编辑文本时,我将首先得到“ mDefault ”的日志,这是正常的,因为共享首选项中没有存储任何内容。

然后,我将一些东西存储在共享首选项中,并确保它存储,我记录并记录我输入的内容。 这意味着存储了共享偏好数据WAS。

下面是问题:在我在共享首选项中存储了一些东西之后,我进入了一个不同的活动,然后我回来了,共享首选项中存储的所有数据都是GONE!

在浏览活动后,第一个日志仍然显示mDefault

问题是什么?

编辑:

这是我的实例:

onCreate

  keyword = PreferenceManager.getDefaultSharedPreferences(this); //Making a shared preferences keywordEditor = keyword.edit(); 

也许你没有保存在setOnEditorActionListener上。 当你去另一个活动时保存。 因为当它进入不同的活动时setOnEditorActionListener editText.getText()。toString()它返回null。

存储偏好:

  SharedPreferences pref = getSharedPreferences("MyPrefs",Context.MODE_PRIVATE); // We need an editor object to make changes SharedPreferences.Editor edit = pref.edit(); // Set/Store data edit.putString("username", "Rishabh"); edit.putString("password", "rishabh123"); // Commit the changes edit.commit(); 

检索偏好:

  SharedPreferences pref = getSharedPreferences("MyPrefs", Context.MODE_PRIVATE); String username = pref.getString("username", ""); String password= pref.getString("password", ""); Log.d(TAG, username); Log.d(TAG, password); 

如果您错过了关键组件,请添加此示例。 目前这对我有用:

 public class Main2Activity extends ActionBarActivity { private SharedPreferences keyword; private SharedPreferences.Editor keywordEditor; private String TAG = "TAG"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main2); keyword = PreferenceManager.getDefaultSharedPreferences(this); //Making a shared preferences keywordEditor = keyword.edit(); final EditText editText = (EditText) findViewById(R.id.et_text); findViewById(R.id.btn_launch).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(Main2Activity.this, Main22Activity.class); startActivity(intent); } }); editText.setOnEditorActionListener(new TextView.OnEditorActionListener() { @Override public boolean onEditorAction(TextView v, int keycode, KeyEvent event) { Log.v(TAG, "Initial: " + keyword.getString("keyword", "mDefault")); //IT LOGS OUT THE DEFAULT STRING EVEN **AFTER** STORING THE PREFERENCES BEFORE if (keycode == EditorInfo.IME_ACTION_SEND) { editText.setText(editText.getText().toString()); keywordEditor.putString("keyword", editText.getText().toString()); keywordEditor.commit(); Log.v(TAG, "Saving in prefs: " + keyword.getString("keyword", "default")); //CORRECT! THIS LINE WORKS } return true; } }); } } 

从全新安装:

我键入“test”并点击键盘上的发送按钮,因此调用了onEditorAction

然后点击启动新活动 – >点击后退按钮并输入“test2”并点击发送按钮。

注销如下:

 02-29 23:26:42.068 18105-18105/com.example.naveed.myapplication V/TAG: Initial: mDefault 02-29 23:26:42.136 18105-18105/com.example.naveed.myapplication V/TAG: Saving in prefs: test 02-29 23:26:53.281 18105-18105/com.example.naveed.myapplication V/TAG: Initial: test 02-29 23:26:53.338 18105-18105/com.example.naveed.myapplication V/TAG: Saving in prefs: test2 

正如你最初看到的那样是“mDefault”,然后保存了“test”。 我发起了一项新活动并回来了。 下次初始化为“test”,因为它上次保存,“test2”是保存的新值。

创建SharedPreferences

  public class SharedPreferenceClass { // Shared Preferences SharedPreferences pref; // Editor for Shared preferences SharedPreferences.Editor editor; // Context Context _context; // Shared pref mode int PRIVATE_MODE = 0; // Sharedpref file name private static final String PREF_NAME = "INTELLIJ_AMIYO"; public static final String KEY_SET_VALUE= "KEY_SET_VALUE"; public SharedPreferenceClass(Context context){ this._context = context; pref = _context.getSharedPreferences(PREF_NAME, 0); editor = pref.edit(); } public void setUserDATA(String data) { editor.remove(KEY_SET_VALUE); editor.putString(KEY_SET_VALUE, data); editor.commit(); } public String getUserDATA() { String getData= pref.getString(KEY_SET_VALUE, null); return getData; } } 

现在在全局的“活动”部分中调用它

 SharedPreferenceClass sharedPrefClassObj; 

&调用onCreate(Bundle savedInstanceState)部分

  sharedPrefClassObj = new SharedPreferenceClass(getApplicationContext()); 

现在加上这个

  editText.setOnEditorActionListener(new TextView.OnEditorActionListener() { @Override public boolean onEditorAction(TextView v, int keycode, KeyEvent event) { if (keycode == EditorInfo.IME_ACTION_SEND) { editText.setText(editText.getText().toString()); sharedPrefClassObj.setUserDATA(editText.getText().toString()); // Set data // Get data sharedPrefClassObj.getUserDATA(); } } return true; }); 

非常重要:您需要一个首选项名称 (例如:“MY_PREFS_NAME”)来设置和检索值:

 SharedPreferences.Editor keywordEditor = context.getSharedPreferences("MY_PREFS_NAME", MODE_PRIVATE).edit(); 

使用相同的常量首选项名称,它将在您的应用程序的任何位置为您提供相同的首选项。