好的,所以我有一个包含许多图像按钮的应用程序,我目前正在工作的是所有onClick
侦听器,并且当用户单击图像时图像会发生变化。onSavedInstance()
我阅读并研究了我可以使用和保存这些图像按钮的状态onRestore()
。这就是我现在所拥有的,我只为一个图像按钮尝试这个,因为如果这不正确,我不想更改所有内容。
我也有让我在每个活动中移动的按钮,问题是当我移动到另一个活动然后移回图像按钮更改不会保存并且按钮图像返回到如果它从未被按下时。
我有一种感觉,我错过了一些非常简单的东西,因为我已经观看并阅读了一些教程,但仍然无法让它工作。如果能帮助我理解,我将不胜感激。
好的问题解决了我只使用共享首选项在我的一个图像按钮上尝试了它,并将其置于暂停和恢复状态。我现在可以按返回按钮转屏并退出应用程序而不会丢失数据。
public boolean isPressed;
private boolean isCandyPressed = false;
private static final String CANDY_PRESSED = "CandyPressed";
private SharedPreferences prefs;
private String PrefName = "MyPref";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_desserts);
vegetablesButton = (ImageButton) findViewById(R.id.vegetabesButton);
candy = (ImageButton) findViewById(R.id.candy);
chocolate = (ImageButton) findViewById(R.id.chocolate);
cookies = (ImageButton) findViewById(R.id.cookies);
cupcakes = (ImageButton) findViewById(R.id.cupcakes);
doughnuts = (ImageButton) findViewById(R.id.doughnuts);
iceCream = (ImageButton) findViewById(R.id.icecream);
jello = (ImageButton) findViewById(R.id.jello);
marshMellows = (ImageButton) findViewById(R.id.marshmellows);
muffins = (ImageButton) findViewById(R.id.muffins);
pudding = (ImageButton) findViewById(R.id.pudding);
yogurt = (ImageButton) findViewById(R.id.yogurt);
vegetablesButton = (ImageButton) findViewById(R.id.vegetabesButton);
ImageChanger(chocolate, R.drawable.buttongrabchocolate, R.drawable.chocolate);
ImageChanger(cookies, R.drawable.buttongrabcookies, R.drawable.cookies);
ImageChanger(cupcakes, R.drawable.buttongrabcupcakes, R.drawable.cupcake);
ImageChanger(doughnuts, R.drawable.buttongrabdoughnuts, R.drawable.doughnut);
ImageChanger(iceCream, R.drawable.buttongrabicecream, R.drawable.icecream);
ImageChanger(jello, R.drawable.buttongrabjello, R.drawable.jelly);
ImageChanger(marshMellows, R.drawable.buttongrabmarshmellows, R.drawable.marshmellows);
ImageChanger(muffins, R.drawable.buttongrabmuffins, R.drawable.muffin);
ImageChanger(pudding, R.drawable.buttongrabpudding, R.drawable.pudding);
ImageChanger(yogurt, R.drawable.buttongrabyogurt, R.drawable.yogurt);
setCandyImage();
candy.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (!isCandyPressed) {
candy.setBackgroundResource(R.drawable.buttongrabcandy);
isCandyPressed = true;
} else {
candy.setBackgroundResource(R.drawable.candy);
isCandyPressed = false;
}
}
});
vegetablesButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent myIntent = new Intent(Desserts.this, Items.class
);
Desserts.this.startActivity(myIntent);
}
});
}
protected void setCandyImage() {
if (isCandyPressed) {
candy.setBackgroundResource(R.drawable.buttongrabcandy);
} else {
candy.setBackgroundResource(R.drawable.candy);
}
}
void ImageChanger(final ImageButton a, final int b, final int c) {
a.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (isPressed) {
a.setBackgroundResource(b);
} else
a.setBackgroundResource(c);
isPressed = !isPressed;
}
});
}
@Override
protected void onPause() {
super.onPause();
prefs=getSharedPreferences(PrefName,MODE_PRIVATE);
SharedPreferences.Editor editor=prefs.edit();
editor.putBoolean(CANDY_PRESSED,isCandyPressed);
editor.commit();
}
@Override
protected void onResume() {
super.onResume();
prefs=getSharedPreferences(PrefName,MODE_PRIVATE);
isCandyPressed=prefs.getBoolean(CANDY_PRESSED,false);
setCandyImage();
}
}