问题
I have a tab-host with three activities and in each activity I have buttons.
On each button press I am changing the background drawable resource
So now How can I save the pressed state of each button in all three child activities so that when I move from one activity to the other, the button pressed state will be reflected on moving back. first activity -> all 2 buttons pressed -> go to 2nd activity -> come back to first activity -> all buttons in first activity should be in pressed state with the drawable resource changed
I have two background Images.
button press -> highlited background image -> second time press same button ->changes to normal background Image
For the first time when I run the application and If I press the button the background image is getting changed. If I go to 2nd child activity and coming back so the button is in pressed state with changed drawable resource. so Its fine
for the second time If I press the same button and coming back to the same activity from second activity. here its not working
First child activity -> press button ->high lighted background image changes ->go to second tabhost child activity -> come back to first child activity -> button is showing the same background image after pressing(this is fine)
the problem is
now you press the same button in first child activity -> background image changes to normal ->go to second tabhost child activity -> come back to first child activity -> but the button background resource is in highlighted instead of normal
and even If I run the application once again the button is showing the highligted background image
the below code I am using in my first child activity
EDITED
public void onResume() {
super.onResume();
SharedPreferences sharedPreferences = this.getSharedPreferences(
"sharedPreferences", MODE_PRIVATE);
int which = sharedPreferences.getInt("sharedPreferences", 1);
switch (which)
{
case 1:
seatdirnbtn.setBackgroundResource(R.drawable.icon4); break;
case 2:
seatdirnbtn.setBackgroundResource(R.drawable.icon4hlt); break;
default:
seatdirnbtn.setBackgroundResource(R.drawable.icon4); break;
}
}
public void onPause() {
super.onPause();
boolean isclick = false;
SharedPreferences sharedPreferences = this.getSharedPreferences(
"sharedPreferences", MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
if (isclick) {
seatdirnbtn.setBackgroundResource(R.drawable.icon4hlt);
editor.putInt("sharedPreferences", 2).commit();
}
}
EDIT2
public static boolean isclick = false;
private View.OnClickListener listner1 = new View.OnClickListener() {
public void onClick(View v) {
if (isclick) {
seatdirnbtn.setBackgroundResource(R.drawable.icon4hlt);
} else {
seatdirnbtn.setBackgroundResource(R.drawable.icon4);
}
isclick = !isclick;
}
};
回答1:
Try this change:
if (isclick) {
seatdirnbtn.setBackgroundResource(R.drawable.icon4hlt);
editor.clear();
editor.putInt("sharedPreferences", 2).commit();
}
else {
seatdirnbtn.setBackgroundResource(R.drawable.icon4);
editor.clear();
editor.putInt("sharedPreferences", 1).commit();
}
回答2:
Here's what i meant:
boolean[] btnBool = new boolean[2]; // number of buttons
private View.OnClickListener listner1 = new View.OnClickListener() {
public void onClick(View v) {
toggleBtn(0); //zero refers to which index of the array for button button
setDrawables();
}
};
public void onResume() {
super.onResume();
toggleAllBtns();
setDrawables();
}
private void toggleBtn(int index) {
if(btnBool[index]) {
btnBool[index] = false;
} else {
btnBool[index] = true;
}
}
private void toggleAllBtns() {
// first switch the values of the array
for (int i = 0; i < btnBool.length; i++) {
if(btnBool[i]) {
btnBool[i] = false;
} else {
btnBool[i] = true;
}
}
}
private void setDrawables() {
// then set drawables based on values
if (btnBool[0]) {
seatdirnbtn.setBackgroundResource(R.drawable.icon4);
} else {
seatdirnbtn.setBackgroundResource(R.drawable.icon4hlt);
}
}
来源:https://stackoverflow.com/questions/13832888/android-shared-preferences-not-saving-the-state-of-the-buttons