我使用此代码在 SharedPreference 中创造价值。
SharedPreferences sharedpreferences;
sharedpreferences = getSharedPreferences("user", Context.MODE_PRIVATE);
Editor editor = sharedpreferences.edit();
editor.putInt("ans1",1).commit();
这是从 SharedPreference 中获取值的代码。
SharedPreferences sharedpreferences=getApplicationContext().getSharedPreferences("user", Activity.MODE_PRIVATE);
int ans = sharedpreferences.getInt("ans1",7);
ans 的值始终为 7。