0
我编写了一个应用程序,可以继续擦除我的共享首选项。始终我的理解是,当您使用SharedPreferences时,它们会持续存在,直到您卸载应用程序或以编程方式清除偏好设置(我可能是错的)。发生什么事是我在一个文件中有一个sharedpreference的字符串值。然后在运行if语句的单独文件中调用该值,并在该变量已存储时将imageview设置为特定图形。它似乎运行良好,一切工作就像它应该直到我关闭应用程序并杀死它在电话设置或使用“高级任务杀手”应用程序。我用相同的方式使用共享首选项编写了其他应用程序(不使用if语句),我似乎没有这个问题,所以这使我认为if语句存在问题。从逻辑上讲,我无法弄清楚发生了什么问题。任何帮助深表感谢。文件中的一个代码:SharedPreferences保持擦除
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.guessaquaman);
final Button guessbutton = (Button) findViewById(R.id.guess_button);
guessbutton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
guess=(EditText)findViewById(R.id.guess_edittext);
String myguess = guess.getText().toString();
String correctanswer = "aquaman";
if(myguess.equals(correctanswer)){
Toast.makeText(getApplicationContext(), "Correct", Toast.LENGTH_SHORT).show();
SharedPreferences sharedPreferences = getSharedPreferences("MY_SHARED_PREF", MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("aquamanticked", "on");
editor.commit();
Intent myintent1 = new Intent(AquamanGuess.this,PlayActivity.class);
startActivity(myintent1);
guessbutton.setEnabled(false);
}
}
});
第二个文件:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.play);
SharedPreferences sharedPreferences = getSharedPreferences("MY_SHARED_PREF", MODE_PRIVATE);
String aquamanticker = sharedPreferences.getString("aquamanticked", "");
String aman= "on";
ImageView aquaman = (ImageView) findViewById(R.id.aquaman);
aquaman.setImageResource(R.drawable.aquaman_small_empty);
if (aquamanticker == aman){
aquaman.setImageResource(R.drawable.aquaman_small_filled); }
就是这样!非常感谢。我很惊讶,要解决这个问题有多简单。 – chris
没问题,它的这些小小的代码可能很容易错过代码的一瞥,特别是如果你已经写了它,并破坏你的头多年试图找出:) Upvote这个答案,以及你的代表也增加了: ) – t0mm13b