Android的:共享preferences不保存按钮的状态 [英] Android: Shared preferences not saving the state of the buttons
问题描述
我有一个制表主机三项活动,并在每个活动我的按钮。
在每个按钮preSS我改变背景绘制资源
所以,现在我怎么能保存在所有三个子活动pssed每个按钮的状态$ P $,这样当我移动从一个活动到另一个,按钮pressed状态将搬回来体现。第一项活动 - >所有2个按钮pressed - >去第2个活动 - >回来的第一项活动 - >在第一个活动的所有按钮应该在pressed状态与绘制资源变
我有两个背景图片。
按钮preSS - >反白标明的背景图片 - >第二次preSS相同的按钮 - >更改为正常的背景图像
有关当我运行应用程序,如果我preSS的按钮,背景图像得到改变的第一次。如果我去第二个孩子的活动和回来使按键与改变绘制资源pressed状态。所以它的精细
第二次如果我preSS的相同的按钮,并从第二活动回来的相同的活性。在这里它不工作
第一子活动 - > preSS按钮 - >高燃点的背景图像的变化 - >去第二tabhost子活动 - >回来的第一个孩子活动 - >按钮显示后pressing相同的背景图片(这是好的)
问题是
现在你preSS在第一个孩子的活动相同的按钮 - >背景图像更改为正常 - >去第二tabhost子活动 - >回来的第一个孩子活动 - >而是在强调按钮背景资源,而不是正常
即使我运行应用程序再次按钮显示highligted背景图片
下面code,我用我的第一个孩子活动
EDITED
公共无效onResume(){
super.onResume();
共享preferences共享preferences = this.getShared preferences(
共享preferences,MODE_PRIVATE); INT这=共享preferences.getInt(共享preferences,1);
开关(所)
{
情况1:
seatdirnbtn.setBackgroundResource(R.drawable.icon4);打破;
案例2:
seatdirnbtn.setBackgroundResource(R.drawable.icon4hlt);打破;
默认:
seatdirnbtn.setBackgroundResource(R.drawable.icon4);打破;
}
}公共无效的onPause(){
super.onPause();
布尔isclick = FALSE;
共享preferences共享preferences = this.getShared preferences(
共享preferences,MODE_PRIVATE);
共享preferences.Editor编辑=共享preferences.edit(); 如果(isclick){
seatdirnbtn.setBackgroundResource(R.drawable.icon4hlt); editor.putInt(共享preferences,2).commit();
}
}
EDIT2
公共静态布尔isclick = FALSE;
私人View.OnClickListener listner1 =新View.OnClickListener(){ 公共无效的onClick(视图v){ 如果(isclick){
seatdirnbtn.setBackgroundResource(R.drawable.icon4hlt); }其他{
seatdirnbtn.setBackgroundResource(R.drawable.icon4);
} !isclick = isclick;
}};
下面就是我的意思是:
布尔[] = btnBool新的布尔[2]; //按键的数量私人View.OnClickListener listner1 =新View.OnClickListener(){
公共无效的onClick(视图v){
toggleBtn(0); //零是指在数组中键按钮的指数
setDrawables();
}};公共无效onResume(){
super.onResume();
toggleAllBtns();
setDrawables();
}私人无效toggleBtn(INT指数){
如果(btnBool [指数]){
btnBool [指数] = FALSE;
}其他{
btnBool [指数] =真;
}
}私人无效toggleAllBtns(){
//第一切换数组的值
的for(int i = 0; I< btnBool.length;我++){
如果(btnBool [I]){
btnBool [I] = FALSE;
}其他{
btnBool [I] =真;
}
}
}私人无效setDrawables(){
//然后根据设定值可绘制
如果(btnBool [0]){
seatdirnbtn.setBackgroundResource(R.drawable.icon4);
}其他{
seatdirnbtn.setBackgroundResource(R.drawable.icon4hlt);
}
}
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;
}
};
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);
}
}
这篇关于Android的:共享preferences不保存按钮的状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!