tabhost内的共享首选项不适用于按下按钮状态。
我正在更改按下状态按钮的背景。但是,当我重新启动(关闭和打开)手机时,共享首选项不会保存状态。
变量
btn_state
以下行中的始终返回False
final boolean btn_state = prefs.getBoolean("BUTTON_STATE", isclick);
我们非常感谢任何帮助,谢谢
这是我的代码
private SharedPreferences prefs;
private String prefName = "MyPref";
private SharedPreferences.Editor editor;
private static final String BUTTON_STATE = "button_selected";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
prefs = PreferenceManager
.getDefaultSharedPreferences(getApplicationContext());
final boolean btn_state = prefs.getBoolean("BUTTON_STATE", isclick);
editor = prefs.edit();
if(btn_state == false){
seatdirnbtn.setBackgroundResource(R.drawable.icon4hlt);
}
else if(btn_state == true){
seatdirnbtn.setBackgroundResource(R.drawable.icon4);
}
}
@Override
public void onStop() {
super.onStop();
prefs = PreferenceManager
.getDefaultSharedPreferences(getApplicationContext());
editor = prefs.edit();
editor.putBoolean("BUTTON_STATE", isclick);
editor.commit();
}
public static boolean isclick = false;
private View.OnClickListener listner1 = new View.OnClickListener() {
public void onClick(View v) {
if (isclick) {
seatdirnbtn.setBackgroundResource(R.drawable.icon4hlt);
isclick = true;
prefs = PreferenceManager
.getDefaultSharedPreferences(getApplicationContext());
editor = prefs.edit();
editor.putBoolean("BUTTON_STATE", isclick);
editor.commit();
} else {
seatdirnbtn.setBackgroundResource(R.drawable.icon4);
isclick = false;
}
isclick = !isclick;
}
修改
private boolean isclick ;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState == null) { isclick = false; }
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;
}
};
答案 0 :(得分:0)
我会使用getSharedPreferences()而不是getDefaultSharedPreferences()。根据我在StackOverflow上看到的一些问题,例如this one,默认首选项方法可能会出现问题。
您可以使用getSharedPreferences()更明确地指定首选项文件,这可能有助于您解决此问题。查看Android开发者Shared Preferences guide以获取有用的代码示例和解释,如果您想尝试它的话。
我列出的代码中有一些我不理解的内容。 你有2个布尔变量,onclick和btn_state。为什么不用一个呢? btn_state被声明为final。为什么? 您已声明了BUTTON_STATE字符串,但在引用您的prefs时不使用它,您使用字符串“BUTTON_STATE”。