tabhost 中的共享首选项不适用于按钮按下状态。
我正在按下状态下更改按钮的背景。但是当我重新启动(关闭和打开)手机时,共享首选项不会保存状态。
从这里裁判
tabhost 中按钮按下状态的共享首选项在重新启动时不起作用
在调试时,isclick 在 if-else 条件下显示为 true。我不明白这个问题。
当我第一次运行时,我得到了带有突出显示背景的按钮,即使在重新启动时我也得到了相同的突出显示背景。
任何帮助总是受到赞赏。
private boolean isclick ;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
seatdirnbtn = (Button) findViewById(R.id.seatdirnbtn);
prefs = this.getSharedPreferences(prefName, MODE_PRIVATE);
isclick = prefs.getBoolean("prefName", false);
System.out.println("bool? " + isclick);
if (isclick) {
seatdirnbtn.setBackgroundResource(R.drawable.icon4hlt);
} else if (!isclick) {
seatdirnbtn.setBackgroundResource(R.drawable.icon4);
}
}
@Override
public void onRestart() {
super.onRestart();
if (isclick) {
seatdirnbtn.setBackgroundResource(R.drawable.icon4);
} else if (!isclick) {
seatdirnbtn.setBackgroundResource(R.drawable.icon4hlt);
}
}
@Override
public void onStop() {
super.onStop();
prefs = this.getSharedPreferences(prefName, MODE_PRIVATE);
editor = prefs.edit();
editor.putBoolean("prefName", true);
editor.commit();
}
private View.OnClickListener listner1 = new View.OnClickListener() {
public void onClick(View v) {
if (isclick) {
seatdirnbtn.setBackgroundResource(R.drawable.icon4hlt);
editor = prefs.edit();
editor.clear();
editor.putBoolean("prefName", true);
editor.commit();
} else if (!isclick) {
seatdirnbtn.setBackgroundResource(R.drawable.icon4);
editor = prefs.edit();
editor.clear();
editor.putBoolean("prefName", false);
editor.commit();
}
isclick = !isclick;
}
};
编辑1:
private boolean isclick ;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
prefs = this.getSharedPreferences(prefName, MODE_PRIVATE);
isclick = prefs.getBoolean("prefName", true);
System.out.println("bool? " + isclick);
Log.i(TAG, " prefname");
if (isclick) {
seatdirnbtn.setBackgroundResource(R.drawable.icon4hlt);
} else if (!isclick) {
seatdirnbtn.setBackgroundResource(R.drawable.icon4);
}
public void onPause() {
super.onPause();
prefs = this.getSharedPreferences(prefName, MODE_PRIVATE);
// boolean isclick = false;
editor = prefs.edit();
// editor.clear();
editor.putBoolean("prefName", false);
Log.i(TAG, " prefname");
editor.commit();
}
@Override
public void onRestart() {
super.onRestart();
prefs = this.getSharedPreferences(prefName, MODE_PRIVATE);
isclick = prefs.getBoolean("prefName", !isclick);
}
@Override
public void onStop() {
super.onStop();
getApplicationContext().unbindService(this);
prefs = this.getSharedPreferences(prefName, MODE_PRIVATE); // boolean
isclick = false; editor = prefs.edit(); // editor.clear();
editor.putBoolean("prefName", !isclick);
editor.commit();
}
private View.OnClickListener listner1 = new View.OnClickListener() {
public void onClick(View v) {
if (isclick) {
seatdirnbtn.setBackgroundResource(R.drawable.icon4hlt);
editor = prefs.edit();
editor.clear();
editor.putBoolean("prefName", false);
editor.commit();
isclick = false;
} else if (!isclick) {
seatdirnbtn.setBackgroundResource(R.drawable.icon4);
editor = prefs.edit();
editor.clear();
editor.putBoolean("prefName", true);
editor.commit();
isclick = true;
}
}
};